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

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

Direct Known Subclasses

SyncResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request, raw_response) ⇒ Response

Returns a new instance of Response.



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

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)



6
7
8
# File 'lib/wcc/contentful/simple_client/response.rb', line 6

def client
  @client
end

#raw_responseObject (readonly)



5
6
7
# File 'lib/wcc/contentful/simple_client/response.rb', line 5

def raw_response
  @raw_response
end

#requestObject (readonly)



7
8
9
# File 'lib/wcc/contentful/simple_client/response.rb', line 7

def request
  @request
end

Instance Method Details

#assert_ok!Object

Raises:



50
51
52
53
54
# File 'lib/wcc/contentful/simple_client/response.rb', line 50

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

  raise ApiError[code], self
end

#bodyObject



12
13
14
# File 'lib/wcc/contentful/simple_client/response.rb', line 12

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

#countObject



83
84
85
# File 'lib/wcc/contentful/simple_client/response.rb', line 83

def count
  raw['total']
end

#each_page(&block) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wcc/contentful/simple_client/response.rb', line 56

def each_page(&block)
  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

#error_messageObject



21
22
23
# File 'lib/wcc/contentful/simple_client/response.rb', line 21

def error_message
  raw.dig('message') || "#{code}: #{raw_response.message}"
end

#firstObject

Raises:

  • (ArgumentError)


87
88
89
90
91
# File 'lib/wcc/contentful/simple_client/response.rb', line 87

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

  raw['items'].first
end

#includesObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wcc/contentful/simple_client/response.rb', line 93

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

#itemsObject



77
78
79
80
81
# File 'lib/wcc/contentful/simple_client/response.rb', line 77

def items
  each_page.flat_map do |page|
    page.raw['items']
  end
end

#next_pageObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wcc/contentful/simple_client/response.rb', line 31

def next_page
  return unless next_page?

  @next_page ||= @client.get(
    @request[:url],
    (@request[:query] || {}).merge({
      skip: raw['items'].length + raw['skip']
    })
  )
  @next_page.assert_ok!
end

#next_page?Boolean

Returns:

  • (Boolean)


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

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

  raw['items'].length + raw['skip'] < raw['total']
end

#rawObject Also known as: to_json



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

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