Class: WCC::Contentful::SimpleClient::Response

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

Direct Known Subclasses

SyncResponse

Instance Attribute Summary collapse

Attributes included from Instrumentation

#_instrumentation

Instance Method Summary collapse

Methods included from Instrumentation

#_instrumentation_event_prefix, instrument

Constructor Details

#initialize(client, request, raw_response) ⇒ Response

Returns a new instance of Response.



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

def initialize(client, request, raw_response)
  @client = client
  @request = request
  @raw_response = raw_response
  @body = raw_response.body.to_s
end

Instance Attribute Details

#clientObject (readonly)



9
10
11
# File 'lib/wcc/contentful/simple_client/response.rb', line 9

def client
  @client
end

#raw_responseObject (readonly)



9
10
11
# File 'lib/wcc/contentful/simple_client/response.rb', line 9

def raw_response
  @raw_response
end

#requestObject (readonly)



9
10
11
# File 'lib/wcc/contentful/simple_client/response.rb', line 9

def request
  @request
end

Instance Method Details

#assert_ok!Object

Raises:



68
69
70
71
72
# File 'lib/wcc/contentful/simple_client/response.rb', line 68

def assert_ok!
  return self if status >= 200 && status < 300

  raise ApiError[status], self
end

#bodyObject



15
16
17
# File 'lib/wcc/contentful/simple_client/response.rb', line 15

def body
  @body ||= raw_response.body.to_s
end

#countObject



94
95
96
# File 'lib/wcc/contentful/simple_client/response.rb', line 94

def count
  total
end

#each_page(&block) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wcc/contentful/simple_client/response.rb', line 74

def each_page(&block)
  raise ArgumentError, 'Not a collection response' unless page_items

  ret = PaginatingEnumerable.new(self)

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

#error_messageObject



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

def error_message
  parsed_message =
    begin
      raw['message']
    rescue JSON::ParserError
      nil
    end
  parsed_message || "#{code}: #{raw_response.body}"
end

#firstObject

Raises:

  • (ArgumentError)


98
99
100
101
102
# File 'lib/wcc/contentful/simple_client/response.rb', line 98

def first
  raise ArgumentError, 'Not a collection response' unless page_items

  page_items.first
end

#includesObject



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

def includes
  @includes ||=
    raw['includes']&.each_with_object({}) do |(_t, entries), h|
      entries&.each { |e| h[e.dig('sys', 'id')] = e }
    end || {}
end

#itemsObject



86
87
88
# File 'lib/wcc/contentful/simple_client/response.rb', line 86

def items
  each_page.flat_map(&:page_items)
end

#next_pageObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wcc/contentful/simple_client/response.rb', line 48

def next_page
  return unless 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
  np.assert_ok!
end

#next_page?Boolean

Returns:

  • (Boolean)


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

def next_page?
  return unless raw.key? 'items'

  page_items.length + skip < total
end

#page_itemsObject



90
91
92
# File 'lib/wcc/contentful/simple_client/response.rb', line 90

def page_items
  raw['items']
end

#rawObject Also known as: to_json



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

def raw
  @raw ||= JSON.parse(body)
end

#skipObject



34
35
36
# File 'lib/wcc/contentful/simple_client/response.rb', line 34

def skip
  raw['skip']
end

#totalObject



38
39
40
# File 'lib/wcc/contentful/simple_client/response.rb', line 38

def total
  raw['total']
end