Class: WCC::Contentful::SimpleClient::SyncResponse

Inherits:
Response
  • Object
show all
Defined in:
lib/wcc/contentful/simple_client/response.rb

Instance Attribute Summary

Attributes inherited from Response

#client, #raw_response, #request

Attributes included from Instrumentation

#_instrumentation

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Response

#assert_ok!, #body, #error_message, #first, #includes, #items, #page_items, #raw, #skip, #total

Methods included from Instrumentation

#_instrumentation_event_prefix, instrument

Constructor Details

#initialize(response, memoize: false) ⇒ SyncResponse

Returns a new instance of SyncResponse.



113
114
115
116
# File 'lib/wcc/contentful/simple_client/response.rb', line 113

def initialize(response, memoize: false)
  super(response.client, response.request, response.raw_response)
  @memoize = memoize
end

Class Method Details

.parse_sync_token(url) ⇒ Object



167
168
169
170
171
# File 'lib/wcc/contentful/simple_client/response.rb', line 167

def self.parse_sync_token(url)
  url = URI.parse(url)
  q = CGI.parse(url.query)
  q['sync_token']&.first
end

Instance Method Details

#countObject

Raises:

  • (NotImplementedError)


162
163
164
165
# File 'lib/wcc/contentful/simple_client/response.rb', line 162

def count
  raise NotImplementedError,
    'Sync does not return an accurate total.  Use #items.count instead.'
end

#each_page(&block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/wcc/contentful/simple_client/response.rb', line 147

def each_page(&block)
  if block_given?
    super do |page|
      @last_sync_token = page.next_sync_token

      yield page
    end
  else
    super.map do |page|
      @last_sync_token = page.next_sync_token
      page
    end
  end
end

#next_pageObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/wcc/contentful/simple_client/response.rb', line 122

def next_page
  return unless next_page?
  return @next_page if @next_page

  url = raw['nextPageUrl']
  next_page =
    _instrument 'page', url: url do
      @client.get(url)
    end

  next_page = SyncResponse.new(next_page)
  next_page.assert_ok!
  @next_page = next_page if @memoize
  next_page
end

#next_page?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/wcc/contentful/simple_client/response.rb', line 118

def next_page?
  raw['nextPageUrl'].present?
end

#next_sync_tokenObject



138
139
140
141
142
143
144
145
# File 'lib/wcc/contentful/simple_client/response.rb', line 138

def next_sync_token
  # If we have iterated some pages, return the sync token of the final
  # page that was iterated.  Do this without maintaining a reference to
  # all the pages.
  return @last_sync_token if @last_sync_token

  SyncResponse.parse_sync_token(raw['nextPageUrl'] || raw['nextSyncUrl'])
end