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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Response

#assert_ok!, #body, #error_message, #first, #includes, #items, #raw

Constructor Details

#initialize(response) ⇒ SyncResponse

Returns a new instance of SyncResponse.



107
108
109
# File 'lib/wcc/contentful/simple_client/response.rb', line 107

def initialize(response)
  super(response.client, response.request, response.raw_response)
end

Class Method Details

.parse_sync_token(url) ⇒ Object



157
158
159
160
161
# File 'lib/wcc/contentful/simple_client/response.rb', line 157

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)


152
153
154
155
# File 'lib/wcc/contentful/simple_client/response.rb', line 152

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

#each_pageObject

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/wcc/contentful/simple_client/response.rb', line 131

def each_page
  raise ArgumentError, 'Not a collection response' unless raw['items']

  ret =
    Enumerator.new do |y|
      y << self

      if next_page?
        next_page.each_page.each do |page|
          y << page
        end
      end
    end

  if block_given?
    ret.map(&block)
  else
    ret.lazy
  end
end

#next_pageObject



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

def next_page
  return unless next_page?

  @next_page ||= SyncResponse.new(@client.get(raw['nextPageUrl']))
  @next_page.assert_ok!
end

#next_page?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/wcc/contentful/simple_client/response.rb', line 111

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

#next_sync_tokenObject



122
123
124
125
126
127
128
129
# File 'lib/wcc/contentful/simple_client/response.rb', line 122

def next_sync_token
  # If we haven't grabbed the next page yet, then our next "sync" will be getting
  # the next page.  We could just as easily call sync again with that token.
  @next_page&.next_sync_token ||
    @next_sync_token ||= SyncResponse.parse_sync_token(
      raw['nextPageUrl'] || raw['nextSyncUrl']
    )
end