Class: WCC::Contentful::SimpleClient::Cdn

Inherits:
WCC::Contentful::SimpleClient show all
Defined in:
lib/wcc/contentful/simple_client/cdn.rb

Overview

The CDN SimpleClient accesses ‘cdn.contentful.com’ to get raw JSON responses. It exposes methods to query entries, assets, and content_types. The responses are instances of WCC::Contentful::SimpleClient::Response which handles paging automatically.

Direct Known Subclasses

Preview

Constant Summary

Constants inherited from WCC::Contentful::SimpleClient

ADAPTERS

Instance Attribute Summary

Attributes inherited from WCC::Contentful::SimpleClient

#api_url, #space

Attributes included from Instrumentation

#_instrumentation

Instance Method Summary collapse

Methods inherited from WCC::Contentful::SimpleClient

#get, load_adapter

Methods included from Instrumentation

#_instrumentation_event_prefix, instrument

Constructor Details

#initialize(space:, access_token:, **options) ⇒ Cdn

Returns a new instance of Cdn.



10
11
12
13
14
15
16
17
# File 'lib/wcc/contentful/simple_client/cdn.rb', line 10

def initialize(space:, access_token:, **options)
  super(
    api_url: options[:api_url] || 'https://cdn.contentful.com/',
    space: space,
    access_token: access_token,
    **options
  )
end

Instance Method Details

#asset(key, query = {}) ⇒ Object

Gets an asset by ID



42
43
44
45
46
47
48
# File 'lib/wcc/contentful/simple_client/cdn.rb', line 42

def asset(key, query = {})
  resp =
    _instrument 'entries', type: 'Asset', id: key, query: query do
      get("assets/#{key}", query)
    end
  resp.assert_ok!
end

#assets(query = {}) ⇒ Object

Queries assets with optional query parameters



51
52
53
54
55
56
57
# File 'lib/wcc/contentful/simple_client/cdn.rb', line 51

def assets(query = {})
  resp =
    _instrument 'entries', type: 'Asset', query: query do
      get('assets', query)
    end
  resp.assert_ok!
end

#client_typeObject



19
20
21
# File 'lib/wcc/contentful/simple_client/cdn.rb', line 19

def client_type
  'cdn'
end

#content_types(query = {}) ⇒ Object

Queries content types with optional query parameters



60
61
62
63
64
65
66
# File 'lib/wcc/contentful/simple_client/cdn.rb', line 60

def content_types(query = {})
  resp =
    _instrument 'content_types', query: query do
      get('content_types', query)
    end
  resp.assert_ok!
end

#entries(query = {}) ⇒ Object

Queries entries with optional query parameters



33
34
35
36
37
38
39
# File 'lib/wcc/contentful/simple_client/cdn.rb', line 33

def entries(query = {})
  resp =
    _instrument 'entries', type: 'Entry', query: query do
      get('entries', query)
    end
  resp.assert_ok!
end

#entry(key, query = {}) ⇒ Object

Gets an entry by ID



24
25
26
27
28
29
30
# File 'lib/wcc/contentful/simple_client/cdn.rb', line 24

def entry(key, query = {})
  resp =
    _instrument 'entries', id: key, type: 'Entry', query: query do
      get("entries/#{key}", query)
    end
  resp.assert_ok!
end

#sync(sync_token: nil, **query, &block) ⇒ Object

Accesses the Sync API to get a list of items that have changed since the last sync. Accepts a block that receives each changed item, and returns the next sync token.

If ‘sync_token` is nil, an initial sync is performed.

Examples:

my_sync_token = storage.get('sync_token')
my_sync_token = client.sync(sync_token: my_sync_token) do |item|
  storage.put(item.dig('sys', 'id'), item) }
end
storage.put('sync_token', my_sync_token)

Returns:

  • String the next sync token parsed from nextSyncUrl



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/wcc/contentful/simple_client/cdn.rb', line 81

def sync(sync_token: nil, **query, &block)
  return sync_old(sync_token: sync_token, **query) unless block_given?

  query = {
    # override default locale for sync queries
    locale: nil
  }.merge(
    if sync_token
      { sync_token: sync_token }
    else
      { initial: true }
    end
  ).merge(query)

  _instrument 'sync', sync_token: sync_token, query: query do
    resp = get('sync', query)
    resp = SyncResponse.new(resp)
    resp.assert_ok!

    resp.each_page do |page|
      page.page_items.each(&block)
      sync_token = resp.next_sync_token
    end
  end

  sync_token
end