Function bodies 314 total
normalize_collection_association_reference method · ruby · L24-L26 (3 LOC)lib/pack_api/mapping/normalized_api_attribute.rb
def normalize_collection_association_reference(attribute_name)
:"#{attribute_name.to_s.delete_suffix(PLURAL_IDS)}s"
endid_for_collection_association? method · ruby · L28-L30 (3 LOC)lib/pack_api/mapping/normalized_api_attribute.rb
def id_for_collection_association?(attribute_name)
api_attribute_names.exclude?(attribute_name) && attribute_name.to_s.end_with?(PLURAL_IDS) # && api_attribute_names.include?(it_without_id_suffix)
endnormalize_resource_association_reference method · ruby · L32-L34 (3 LOC)lib/pack_api/mapping/normalized_api_attribute.rb
def normalize_resource_association_reference(attribute_name)
attribute_name.to_s.delete_suffix(SINGULAR_ID).to_sym
endid_for_resource_association? method · ruby · L36-L38 (3 LOC)lib/pack_api/mapping/normalized_api_attribute.rb
def id_for_resource_association?(attribute_name)
api_attribute_names.exclude?(attribute_name) && attribute_name.to_s.end_with?(SINGULAR_ID) # && api_attribute_names.include?(it_without_id_suffix)
endNullTransformer class · ruby · L4-L8 (5 LOC)lib/pack_api/mapping/null_transformer.rb
class NullTransformer < AbstractTransformer
def execute
nil
end
endexecute method · ruby · L5-L7 (3 LOC)lib/pack_api/mapping/null_transformer.rb
def execute
nil
endValueObjectFactory class · ruby · L4-L17 (14 LOC)lib/pack_api/mapping/value_object_factory.rb
class ValueObjectFactory
class << self
attr_reader :attribute_map_registry, :value_object_attributes
def set_attribute_map_registry(registry)
@attribute_map_registry = registry
@value_object_attributes ||= {}
end
def model_attributes_containing_value_objects(*attributes, model_class:)
@value_object_attributes ||= {}
@value_object_attributes[model_class] = attributes
end
endRepobility (the analyzer behind this table) · https://repobility.com
set_attribute_map_registry method · ruby · L8-L11 (4 LOC)lib/pack_api/mapping/value_object_factory.rb
def set_attribute_map_registry(registry)
@attribute_map_registry = registry
@value_object_attributes ||= {}
endmodel_attributes_containing_value_objects method · ruby · L13-L16 (4 LOC)lib/pack_api/mapping/value_object_factory.rb
def model_attributes_containing_value_objects(*attributes, model_class:)
@value_object_attributes ||= {}
@value_object_attributes[model_class] = attributes
endcreate_object method · ruby · L19-L29 (11 LOC)lib/pack_api/mapping/value_object_factory.rb
def create_object(model:, optional_attributes: nil)
return nil if model.blank?
options = attribute_map_options_cache.compute_if_absent(optional_attributes) { { optional_attributes: } }
attribute_map = attribute_map(model.class, model, options)
attribute_map.api_type.new(attribute_map.attributes)
rescue Dry::Struct::Error => e
model_id = model.respond_to?(:id) ? "(id #{model.id})" : ''
raise PackAPI::InternalError, "Unable to convert #{model.class.name} #{model_id} to value object (#{e.message})"
endcreate_collection method · ruby · L31-L35 (5 LOC)lib/pack_api/mapping/value_object_factory.rb
def create_collection(models:, optional_attributes: nil)
return [] if models.blank?
models.filter_map { |model| create_object(model:, optional_attributes:) }
endcreate_errors method · ruby · L37-L39 (3 LOC)lib/pack_api/mapping/value_object_factory.rb
def create_errors(model:)
attribute_map(model.class, model.errors).attributes
endattribute_map method · ruby · L43-L47 (5 LOC)lib/pack_api/mapping/value_object_factory.rb
def attribute_map(klass, data_source, options = nil)
attribute_map_cache.compute_if_absent(klass) { create_attribute_map(klass) }.tap do |map|
map.data_source = data_source
map.options = options
endconvert function · ruby · L50-L54 (5 LOC)lib/pack_api/mapping/value_object_factory.rb
def convert(value, optional_attributes: nil)
value_is_collection?(value) ?
create_collection(models: value.to_a, optional_attributes:) :
create_object(model: value, optional_attributes:)
endvalue_is_collection? function · ruby · L56-L58 (3 LOC)lib/pack_api/mapping/value_object_factory.rb
def value_is_collection?(value)
value.is_a?(ActiveRecord::Associations::CollectionProxy) || value.is_a?(Array)
endRepobility — the code-quality scanner for AI-generated software · https://repobility.com
create_attribute_map function · ruby · L60-L66 (7 LOC)lib/pack_api/mapping/value_object_factory.rb
def create_attribute_map(model_class)
attribute_map_class = self.class.attribute_map_registry.new.attribute_map_class(model_class)
convert_proc = method(:convert).to_proc
attribute_map_class.new.tap do |attribute_map|
self.class.value_object_attributes.fetch(model_class, []).each do |attribute|
attribute_map.register_transformation_from_model_attribute(attribute, convert_proc)
endattribute_map_cache function · ruby · L70-L72 (3 LOC)lib/pack_api/mapping/value_object_factory.rb
def attribute_map_cache
object_cache.compute_if_absent(:attribute_maps) { Concurrent::Map.new }
endobject_cache function · ruby · L74-L76 (3 LOC)lib/pack_api/mapping/value_object_factory.rb
def object_cache
@object_cache ||= Concurrent::Map.new
endattribute_map_options_cache function · ruby · L78-L80 (3 LOC)lib/pack_api/mapping/value_object_factory.rb
def attribute_map_options_cache
object_cache.compute_if_absent(:attribute_map_options) { Concurrent::Map.new }
endOpaqueTokenV2 class · ruby · L6-L18 (13 LOC)lib/pack_api/pagination/opaque_token_v2.rb
class OpaqueTokenV2
def self.create(unencoded)
Base64.strict_encode64(Brotli.deflate(unencoded.to_json))
end
def self.parse(encoded)
raise JSON::ParserError if encoded.nil?
decoded = Base64.strict_decode64(encoded)
decompressed = Brotli.inflate(decoded)
JSON.parse(decompressed, symbolize_names: true)
end
endcreate method · ruby · L7-L9 (3 LOC)lib/pack_api/pagination/opaque_token_v2.rb
def self.create(unencoded)
Base64.strict_encode64(Brotli.deflate(unencoded.to_json))
endparse method · ruby · L11-L17 (7 LOC)lib/pack_api/pagination/opaque_token_v2.rb
def self.parse(encoded)
raise JSON::ParserError if encoded.nil?
decoded = Base64.strict_decode64(encoded)
decompressed = Brotli.inflate(decoded)
JSON.parse(decompressed, symbolize_names: true)
endPaginatorBuilder class · ruby · L27-L74 (48 LOC)lib/pack_api/pagination/paginator_builder.rb
class PaginatorBuilder
DEFAULT_SORT = 'id asc'
attr_accessor :paginator
def self.build
builder = new
yield(builder)
builder.paginator
end
def initialize
@paginator = Paginator.new
end
def set_cursor(cursor:, per_page: nil, sort: nil)
cursor_params = PaginatorCursor.parse(cursor)
effective_per_page = per_page.presence || cursor_params[:per_page]
effective_per_page = :all if effective_per_page.to_s == 'all'
@paginator.query = cursor_params[:query]
@paginator.total_items = cursor_params[:total_items]
@paginator.per_page = effective_per_page
@paginator.sort = sort.presence || cursor_params[:sort]
@paginator.offset = effective_offset(cursor_params, sort)
@paginator.metadata = cursor_params[:metadata]
end
###
# @param [Proc<Hash>|Hash] query The query parameters used to define the recordset.
# @param [String|Symbol|Hash|Arel] sort The ordering used to define the rRepobility analyzer · published findings · https://repobility.com
build method · ruby · L32-L36 (5 LOC)lib/pack_api/pagination/paginator_builder.rb
def self.build
builder = new
yield(builder)
builder.paginator
endinitialize method · ruby · L38-L40 (3 LOC)lib/pack_api/pagination/paginator_builder.rb
def initialize
@paginator = Paginator.new
endset_cursor method · ruby · L42-L53 (12 LOC)lib/pack_api/pagination/paginator_builder.rb
def set_cursor(cursor:, per_page: nil, sort: nil)
cursor_params = PaginatorCursor.parse(cursor)
effective_per_page = per_page.presence || cursor_params[:per_page]
effective_per_page = :all if effective_per_page.to_s == 'all'
@paginator.query = cursor_params[:query]
@paginator.total_items = cursor_params[:total_items]
@paginator.per_page = effective_per_page
@paginator.sort = sort.presence || cursor_params[:sort]
@paginator.offset = effective_offset(cursor_params, sort)
@paginator.metadata = cursor_params[:metadata]
endset_params method · ruby · L62-L68 (7 LOC)lib/pack_api/pagination/paginator_builder.rb
def set_params(query: nil, sort: nil, total_items: nil, per_page: nil, offset: nil, metadata: nil)
@paginator.query ||= {}
if query.present?
original_query = @paginator.query
@paginator.query = @paginator.query.deep_merge(resolve_query_params(query).deep_symbolize_keys)
@recordset_changed = original_query.to_json != @paginator.query.to_json
endeffective_offset function · ruby · L99-L105 (7 LOC)lib/pack_api/pagination/paginator_builder.rb
def effective_offset(cursor_params, sort)
sort_changed = sort.to_json != cursor_params[:sort].to_json
sort_override = sort.present? && sort_changed
sort_override ?
0 :
cursor_params[:offset]
endresolve_query_params function · ruby · L107-L111 (5 LOC)lib/pack_api/pagination/paginator_builder.rb
def resolve_query_params(query)
query.is_a?(Proc) ?
query.call :
query
endPaginatorCursor class · ruby · L4-L80 (77 LOC)lib/pack_api/pagination/paginator_cursor.rb
class PaginatorCursor
MAX_LENGTH = 2048
CACHE_KEY_PREFIX = 'paginator_cursor'
CACHE_EXPIRES_IN = 8.hours
class << self
def create(query:, sort:, total_items:, offset:, per_page:, metadata: nil)
sort_ready_to_serialize = SqlLiteralSerializer.serialize(sort)
cursor_params = { query:, sort: sort_ready_to_serialize, total_items:, offset:, per_page:, metadata: }
token = OpaqueTokenV2.create(cursor_params)
return token if token.size <= MAX_LENGTH
cache_key = generate_cache_key
Rails.cache.write(cache_key, cursor_params, expires_in: CACHE_EXPIRES_IN)
OpaqueTokenV2.create(cache_key)
end
def parse(encoded)
decoded = parse_opaque_token(encoded)
decoded = decode_cache_key(decoded) if decoded.is_a?(String)
decoded[:sort] = deserialize_sort_args(decoded)
decoded
end
private
def parse_opaque_token(encoded)
OpaqueTokenV2.parse(encoded)
rescuecreate method · ruby · L10-L19 (10 LOC)lib/pack_api/pagination/paginator_cursor.rb
def create(query:, sort:, total_items:, offset:, per_page:, metadata: nil)
sort_ready_to_serialize = SqlLiteralSerializer.serialize(sort)
cursor_params = { query:, sort: sort_ready_to_serialize, total_items:, offset:, per_page:, metadata: }
token = OpaqueTokenV2.create(cursor_params)
return token if token.size <= MAX_LENGTH
cache_key = generate_cache_key
Rails.cache.write(cache_key, cursor_params, expires_in: CACHE_EXPIRES_IN)
OpaqueTokenV2.create(cache_key)
endGenerated by Repobility's multi-pass static-analysis pipeline (https://repobility.com)
parse method · ruby · L21-L26 (6 LOC)lib/pack_api/pagination/paginator_cursor.rb
def parse(encoded)
decoded = parse_opaque_token(encoded)
decoded = decode_cache_key(decoded) if decoded.is_a?(String)
decoded[:sort] = deserialize_sort_args(decoded)
decoded
endparse_opaque_token method · ruby · L30-L34 (5 LOC)lib/pack_api/pagination/paginator_cursor.rb
def parse_opaque_token(encoded)
OpaqueTokenV2.parse(encoded)
rescue ArgumentError, Brotli::Error, JSON::ParserError => e
raise_error(e.message)
endraise_error method · ruby · L36-L38 (3 LOC)lib/pack_api/pagination/paginator_cursor.rb
def raise_error(message)
raise(PackAPI::InternalError, "un-parsable paginator cursor: #{message}")
enddecode_cache_key method · ruby · L40-L45 (6 LOC)lib/pack_api/pagination/paginator_cursor.rb
def decode_cache_key(cache_key)
data = Rails.cache.read(cache_key)
raise(PackAPI::InternalError, "no data found in cache for key #{cache_key}") if data.nil?
data
endgenerate_cache_key method · ruby · L47-L49 (3 LOC)lib/pack_api/pagination/paginator_cursor.rb
def generate_cache_key
"#{CACHE_KEY_PREFIX}:#{SecureRandom.uuid}"
enddeserialize_sort_args method · ruby · L51-L55 (5 LOC)lib/pack_api/pagination/paginator_cursor.rb
def deserialize_sort_args(cursor)
SqlLiteralSerializer.deserialize(cursor[:sort])
rescue TypeError => e
raise_error(e.message)
endSqlLiteralSerializer class · ruby · L59-L69 (11 LOC)lib/pack_api/pagination/paginator_cursor.rb
class SqlLiteralSerializer
def self.serialize(args)
return { sql_literal: { raw_sql: args.to_s } } if args.is_a?(Arel::Nodes::SqlLiteral)
return args unless args.is_a?(Hash)
args.map.with_index do |entry, index|
next entry unless entry[0].is_a?(Arel::Nodes::SqlLiteral)
["sql_literal_#{index + 1}", { raw_sql: entry[0].to_s, hash_value: entry[1] }]
end.to_h
endserialize method · ruby · L60-L68 (9 LOC)lib/pack_api/pagination/paginator_cursor.rb
def self.serialize(args)
return { sql_literal: { raw_sql: args.to_s } } if args.is_a?(Arel::Nodes::SqlLiteral)
return args unless args.is_a?(Hash)
args.map.with_index do |entry, index|
next entry unless entry[0].is_a?(Arel::Nodes::SqlLiteral)
["sql_literal_#{index + 1}", { raw_sql: entry[0].to_s, hash_value: entry[1] }]
end.to_hRepobility (the analyzer behind this table) · https://repobility.com
deserialize method · ruby · L71-L79 (9 LOC)lib/pack_api/pagination/paginator_cursor.rb
def self.deserialize(args)
return args unless args.is_a?(Hash)
return Arel.sql(args[:sql_literal][:raw_sql]) if args.key?(:sql_literal)
args.to_h do |key, value|
next [key, value] unless key.start_with?('sql_literal_')
[Arel.sql(value[:raw_sql]), value[:hash_value]]
endPaginator class · ruby · L24-L153 (130 LOC)lib/pack_api/pagination/paginator.rb
class Paginator
###
# @param [Hash] query The query parameters used to define the recordset.
# @param [String|Symbol|Hash|Arel] sort The ordering used to define the recordset
# @param [Integer|:all] per_page The count of items to include on each result page, or :all for single page results
# @param [Integer] offset The starting index of the next result page
# @param [Integer] total_items The count of items in the result set
# @param [Any|nil] metadata optional, extra data structure to be passed along with the cursor
attr_accessor :query, :sort, :total_items, :per_page, :offset, :metadata
DEFAULT_PER_PAGE = 20
###
# The range of items included in the results.
def item_range
return 0..0 if per_page != :all && per_page.zero?
lower_bound = offset + 1
upper_bound = per_page == :all ?
total_items :
[(offset + per_page), total_items].min
lower_bound..upper_bound
end
item_range method · ruby · L38-L46 (9 LOC)lib/pack_api/pagination/paginator.rb
def item_range
return 0..0 if per_page != :all && per_page.zero?
lower_bound = offset + 1
upper_bound = per_page == :all ?
total_items :
[(offset + per_page), total_items].min
lower_bound..upper_bound
endrecordset_cursor method · ruby · L50-L52 (3 LOC)lib/pack_api/pagination/paginator.rb
def recordset_cursor
make_cursor(recordset_cursor_params)
endcurrent_page_cursor method · ruby · L56-L58 (3 LOC)lib/pack_api/pagination/paginator.rb
def current_page_cursor
make_cursor(current_page_cursor_params)
endnext_page_cursor method · ruby · L62-L64 (3 LOC)lib/pack_api/pagination/paginator.rb
def next_page_cursor
make_cursor(next_page_params)
endprevious_page_cursor method · ruby · L68-L70 (3 LOC)lib/pack_api/pagination/paginator.rb
def previous_page_cursor
make_cursor(previous_page_params)
endfirst_page_cursor method · ruby · L74-L76 (3 LOC)lib/pack_api/pagination/paginator.rb
def first_page_cursor
make_cursor(first_page_params)
endRepobility — the code-quality scanner for AI-generated software · https://repobility.com
last_page_cursor method · ruby · L80-L82 (3 LOC)lib/pack_api/pagination/paginator.rb
def last_page_cursor
make_cursor(last_page_params)
endlimit method · ruby · L84-L88 (5 LOC)lib/pack_api/pagination/paginator.rb
def limit
return nil if per_page == :all
per_page
endmore_pages? method · ruby · L92-L96 (5 LOC)lib/pack_api/pagination/paginator.rb
def more_pages?
return false if per_page == :all || per_page.zero?
offset + per_page < total_items
end