class WCC::Contentful::SimpleClient::Response

Attributes

client[R]
raw_response[R]
request[R]

Public Class Methods

new(client, request, raw_response) click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 64
def initialize(client, request, raw_response)
  @client = client
  @request = request
  @raw_response = raw_response
  @body = raw_response.body.to_s
end

Public Instance Methods

assert_ok!() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 71
def assert_ok!
  return self if status >= 200 && status < 300

  raise ApiError[status], self
end
body() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 17
def body
  @body ||= raw_response.body.to_s
end
count() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 106
def count
  total
end
each_page(&block) click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 77
def each_page(&block)
  raise ArgumentError, 'Not a collection response' unless page_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
error_message() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 26
def error_message
  parsed_message =
    begin
      raw.dig('message')
    rescue JSON::ParserError
      nil
    end
  parsed_message || "#{code}: #{raw_response.body}"
end
first() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 110
def first
  raise ArgumentError, 'Not a collection response' unless page_items

  page_items.first
end
includes() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 116
def includes
  @includes ||=
    raw.dig('includes')&.each_with_object({}) do |(_t, entries), h|
      entries.each { |e| h[e.dig('sys', 'id')] = e }
    end || {}

  return @includes unless @next_page

  # This could be more efficient - maybe not worth worrying about
  @includes.merge(@next_page.includes)
end
items() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 98
def items
  each_page.flat_map(&:page_items)
end
next_page() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 50
def next_page
  return unless next_page?
  return @next_page if @next_page

  query = (@request[:query] || {}).merge({
    skip: page_items.length + skip
  })
  np =
    _instrument 'page', url: @request[:url], query: query do
      @client.get(@request[:url], query)
    end
  @next_page = np.assert_ok!
end
next_page?() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 44
def next_page?
  return unless raw.key? 'items'

  page_items.length + skip < total
end
page_items() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 102
def page_items
  raw['items']
end
raw() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 21
def raw
  @raw ||= JSON.parse(body)
end
Also aliased as: to_json
skip() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 36
def skip
  raw['skip']
end
to_json()
Alias for: raw
total() click to toggle source
# File lib/wcc/contentful/simple_client/response.rb, line 40
def total
  raw['total']
end