lib/wcc/contentful/store/cdn_adapter.rb in wcc-contentful-0.1.0 vs lib/wcc/contentful/store/cdn_adapter.rb in wcc-contentful-0.2.0

- old
+ new

@@ -2,11 +2,14 @@ module WCC::Contentful::Store class CDNAdapter attr_reader :client + # Intentionally not implementing write methods + def initialize(client) + super() @client = client end def find(key) entry = @@ -14,45 +17,36 @@ client.entry(key, locale: '*') rescue WCC::Contentful::SimpleClient::NotFoundError client.asset(key, locale: '*') end entry&.raw + rescue WCC::Contentful::SimpleClient::NotFoundError + nil end - def find_all - raise ArgumentError, 'use find_by content type instead' + def find_by(content_type:, filter: nil) + # default implementation - can be overridden + q = find_all(content_type: content_type) + q = q.apply(filter) if filter + q.first end - def find_by(content_type:) + def find_all(content_type:) Query.new(@client, content_type: content_type) end - class Query + class Query < Base::Query delegate :count, to: :resolve - def first - resolve.items.first - end - - def map(&block) - resolve.items.map(&block) - end - def result - raise ArgumentError, 'Not Implemented' + resolve.items end def initialize(client, relation) raise ArgumentError, 'Client cannot be nil' unless client.present? raise ArgumentError, 'content_type must be provided' unless relation[:content_type].present? @client = client @relation = relation - end - - def apply(filter, context = nil) - return eq(filter[:field], filter[:eq], context) if filter[:eq] - - raise ArgumentError, "Filter not implemented: #{filter}" end def eq(field, expected, context = nil) locale = context[:locale] if context.present? locale ||= 'en-US'