Class: WCC::API::RestClient::AbstractResponse

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/wcc/api/rest_client/response.rb

Direct Known Subclasses

DefaultResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request, raw_response) ⇒ AbstractResponse

Returns a new instance of AbstractResponse.


24
25
26
27
28
29
# File 'lib/wcc/api/rest_client/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/api/rest_client/response.rb', line 13

def client
  @client
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body


12
13
14
# File 'lib/wcc/api/rest_client/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/api/rest_client/response.rb', line 11

def raw_response
  @raw_response
end

#requestObject (readonly)

Returns the value of attribute request


14
15
16
# File 'lib/wcc/api/rest_client/response.rb', line 14

def request
  @request
end

Instance Method Details

#assert_ok!Object

Raises:


74
75
76
77
78
# File 'lib/wcc/api/rest_client/response.rb', line 74

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/api/rest_client/response.rb', line 19

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

#collection_response?Boolean

Returns:

  • (Boolean)

39
40
41
# File 'lib/wcc/api/rest_client/response.rb', line 39

def collection_response?
  page_items.nil? ? false : true
end

#countObject


35
36
37
# File 'lib/wcc/api/rest_client/response.rb', line 35

def count
  throw new NotImplementedError, 'Please implement "count" parsing in response class'
end

#each_page(&block) ⇒ Object

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

Raises:

  • (ArgumentError)

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wcc/api/rest_client/response.rb', line 81

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


47
48
49
50
51
52
53
54
55
# File 'lib/wcc/api/rest_client/response.rb', line 47

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)

108
109
110
111
112
# File 'lib/wcc/api/rest_client/response.rb', line 108

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

  page_items.first
end

#itemsObject


102
103
104
105
106
# File 'lib/wcc/api/rest_client/response.rb', line 102

def items
  return unless collection_response?

  each_page.flat_map(&:page_items)
end

#next_pageObject


64
65
66
67
68
69
70
71
72
# File 'lib/wcc/api/rest_client/response.rb', line 64

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)

57
58
59
60
61
62
# File 'lib/wcc/api/rest_client/response.rb', line 57

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

  page_items.length + skip < count
end

#next_page_queryObject


114
115
116
117
118
119
120
# File 'lib/wcc/api/rest_client/response.rb', line 114

def next_page_query
  return unless collection_response?

  {
    skip: page_items.length + skip
  }
end

#page_itemsObject


43
44
45
# File 'lib/wcc/api/rest_client/response.rb', line 43

def page_items
  throw new NotImplementedError, 'Please implement "page_items" parsing in response class'
end

#skipObject


31
32
33
# File 'lib/wcc/api/rest_client/response.rb', line 31

def skip
  throw new NotImplementedError, 'Please implement "skip" parsing in response class'
end