Class: WCC::JsonAPI::BaseClient::BaseResponse

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/wcc/json_api/base_client/base_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request, raw_response) ⇒ BaseResponse

Returns a new instance of BaseResponse.



24
25
26
27
28
29
# File 'lib/wcc/json_api/base_client/base_response.rb', line 24

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



13
14
15
# File 'lib/wcc/json_api/base_client/base_response.rb', line 13

def client
  @client
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



12
13
14
# File 'lib/wcc/json_api/base_client/base_response.rb', line 12

def raw_body
  @raw_body
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



11
12
13
# File 'lib/wcc/json_api/base_client/base_response.rb', line 11

def raw_response
  @raw_response
end

#requestObject (readonly)

Returns the value of attribute request.



14
15
16
# File 'lib/wcc/json_api/base_client/base_response.rb', line 14

def request
  @request
end

Instance Method Details

#assert_ok!Object

Raises:



78
79
80
81
82
# File 'lib/wcc/json_api/base_client/base_response.rb', line 78

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

  raise ApiError[code], self
end

#bodyObject Also known as: to_json



19
20
21
# File 'lib/wcc/json_api/base_client/base_response.rb', line 19

def body
  @body ||= ::JSON.parse(raw_body)
end

#collection_response?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/wcc/json_api/base_client/base_response.rb', line 47

def collection_response?
  data.is_a?(Array) ? true : false
end

#countObject



35
36
37
# File 'lib/wcc/json_api/base_client/base_response.rb', line 35

def count
  body.dig('meta', 'total')
end

#dataObject



39
40
41
# File 'lib/wcc/json_api/base_client/base_response.rb', line 39

def data
  body['data']
end

#each_page(&block) ⇒ Object

This method has a bit of complexity that is better kept in one location

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/wcc/json_api/base_client/base_response.rb', line 85

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

  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



51
52
53
54
55
56
57
58
59
# File 'lib/wcc/json_api/base_client/base_response.rb', line 51

def error_message
  parsed_message =
    begin
      body.dig('error', 'message') || body.dig('message')
    rescue ::JSON::ParserError
      nil
    end
  parsed_message || "#{code}: #{raw_response.body}"
end

#firstObject

Raises:

  • (ArgumentError)


112
113
114
115
116
# File 'lib/wcc/json_api/base_client/base_response.rb', line 112

def first
  raise ArgumentError, 'Not a collection response' unless collection_response?

  page_items.first
end

#itemsObject



106
107
108
109
110
# File 'lib/wcc/json_api/base_client/base_response.rb', line 106

def items
  return unless collection_response?

  each_page.flat_map(&:page_items)
end


43
44
45
# File 'lib/wcc/json_api/base_client/base_response.rb', line 43

def links
  @links ||= OpenStruct.new(body['links'])
end

#next_pageObject



68
69
70
71
72
73
74
75
76
# File 'lib/wcc/json_api/base_client/base_response.rb', line 68

def next_page
  return unless next_page?

  @next_page ||= @client.get(
    @request[:url],
    (@request[:query] || {}).merge(next_page_query)
  )
  @next_page.assert_ok!
end

#next_page?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/wcc/json_api/base_client/base_response.rb', line 61

def next_page?
  return false unless collection_response?
  return false if count.nil?

  page_items.length + skip < count
end

#next_page_queryObject



118
119
120
121
122
123
124
# File 'lib/wcc/json_api/base_client/base_response.rb', line 118

def next_page_query
  return unless collection_response?

  {
    skip: page_items.length + skip
  }
end

#skipObject



31
32
33
# File 'lib/wcc/json_api/base_client/base_response.rb', line 31

def skip
  body.dig('meta', 'skip')
end