class WCC::Contentful::Store::CDNAdapter::Query
Public Class Methods
new(store, client:, relation:, options: nil, **extra)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 80 def initialize(store, client:, relation:, options: nil, **extra) raise ArgumentError, 'Client cannot be nil' unless client.present? raise ArgumentError, 'content_type must be provided' unless relation[:content_type].present? @store = store @client = client @relation = relation @options = options || {} @extra = extra || {} end
Public Instance Methods
apply(filter, context = nil)
click to toggle source
Called with a filter object by {Base#find_by} in order to apply the filter.
# File lib/wcc/contentful/store/cdn_adapter.rb, line 92 def apply(filter, context = nil) filter.reduce(self) do |query, (field, value)| if value.is_a?(Hash) if op?(k = value.keys.first) query.apply_operator(k.to_sym, field.to_s, value[k], context) else query.nested_conditions(field, value, context) end else query.apply_operator(:eq, field.to_s, value) end end end
apply_operator(operator, field, expected, context = nil)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 106 def apply_operator(operator, field, expected, context = nil) op = operator == :eq ? nil : operator param = parameter(field, operator: op, context: context, locale: true) self.class.new( @store, client: @client, relation: @relation.merge(param => expected), options: @options, **@extra ) end
nested_conditions(field, conditions, context)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 119 def nested_conditions(field, conditions, context) base_param = parameter(field) conditions.reduce(self) do |query, (ref, value)| query.apply({ "#{base_param}.#{parameter(ref)}" => value }, context) end end
to_enum()
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 74 def to_enum return response.items unless @options[:include] response.items.map { |e| resolve_includes(e, @options[:include]) } end
Private Instance Methods
field_reference(field)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 191 def field_reference(field) return field if nested?(field) "fields.#{field}" end
id?(field)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 143 def id?(field) field.to_sym == :id end
locale(context)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 183 def locale(context) ".#{(context || {}).fetch(:locale, 'en-US')}" end
nested?(field)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 197 def nested?(field) field.to_s.include?('.') end
op?(key)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 135 def op?(key) WCC::Contentful::Store::Query::Interface::OPERATORS.include?(key.to_sym) end
op_param(operator)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 187 def op_param(operator) operator ? "[#{operator}]" : '' end
parameter(field, operator: nil, context: nil, locale: false)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 173 def parameter(field, operator: nil, context: nil, locale: false) if sys?(field) "#{field}#{op_param(operator)}" elsif id?(field) "sys.#{field}#{op_param(operator)}" else "#{field_reference(field)}#{locale(context) if locale}#{op_param(operator)}" end end
resolve_includes(entry, depth)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 158 def resolve_includes(entry, depth) return entry unless entry && depth && depth > 0 WCC::Contentful::LinkVisitor.new(entry, :Link, :Asset, depth: depth - 1).map! do |val| resolve_link(val) end end
resolve_link(val)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 166 def resolve_link(val) return val unless val.is_a?(Hash) && val.dig('sys', 'type') == 'Link' return val unless included = response.includes[val.dig('sys', 'id')] included end
response()
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 147 def response @response ||= if @relation[:content_type] == 'Asset' @client.assets( { locale: '*' }.merge!(@relation.reject { |k| k == :content_type }).merge!(@options) ) else @client.entries({ locale: '*' }.merge!(@relation).merge!(@options)) end end
sys?(field)
click to toggle source
# File lib/wcc/contentful/store/cdn_adapter.rb, line 139 def sys?(field) field.to_s =~ /sys\./ end