Class: WCC::Contentful::Store::CDNAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/contentful/store/cdn_adapter.rb

Defined Under Namespace

Classes: Query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ CDNAdapter

Intentionally not implementing write methods



9
10
11
12
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 9

def initialize(client)
  super()
  @client = client
end

Instance Attribute Details

#clientObject (readonly)



5
6
7
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 5

def client
  @client
end

Instance Method Details

#find(key, hint: nil, **options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 14

def find(key, hint: nil, **options)
  options = { locale: '*' }.merge!(options || {})
  entry =
    if hint
      client.public_send(hint.underscore, key, options)
    else
      begin
        client.entry(key, options)
      rescue WCC::Contentful::SimpleClient::NotFoundError
        client.asset(key, options)
      end
    end
  entry&.raw
rescue WCC::Contentful::SimpleClient::NotFoundError
  nil
end

#find_all(content_type:, options: nil) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 38

def find_all(content_type:, options: nil)
  Query.new(
    store: self,
    client: @client,
    relation: { content_type: content_type },
    options: options
  )
end

#find_by(content_type:, filter: nil, options: nil) ⇒ Object



31
32
33
34
35
36
# File 'lib/wcc/contentful/store/cdn_adapter.rb', line 31

def find_by(content_type:, filter: nil, options: nil)
  # default implementation - can be overridden
  q = find_all(content_type: content_type, options: { limit: 1 }.merge!(options || {}))
  q = q.apply(filter) if filter
  q.first
end