Class: HornOfPlenty::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/horn_of_plenty/response.rb

Direct Known Subclasses

Adapters::Github::Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_response:) ⇒ Response



9
10
11
# File 'lib/horn_of_plenty/response.rb', line 9

def initialize(raw_response:)
  self.raw_response = raw_response
end

Instance Attribute Details

#raw_responseObject

Returns the value of attribute raw_response.



7
8
9
# File 'lib/horn_of_plenty/response.rb', line 7

def raw_response
  @raw_response
end

Class Method Details

.parse(raw_response) ⇒ Object



22
23
24
25
26
# File 'lib/horn_of_plenty/response.rb', line 22

def self.parse(raw_response)
  new(raw_response: raw_response).tap do |response|
    fail response.error unless response.successful?
  end
end

Instance Method Details

#bodyObject



28
29
30
# File 'lib/horn_of_plenty/response.rb', line 28

def body
  @body ||= JSON.parse(raw_response.body) || ''
end

#itemsObject



18
19
20
# File 'lib/horn_of_plenty/response.rb', line 18

def items
  @items ||= body.is_a?(Array) ? body : [body]
end

#resultObject



13
14
15
16
# File 'lib/horn_of_plenty/response.rb', line 13

def result
  @result ||= ::HornOfPlenty::Collection.new(items:  items,
                                             parser: parser_class)
end

#status_codeObject



32
33
34
# File 'lib/horn_of_plenty/response.rb', line 32

def status_code
  @status_code ||= raw_response.status
end

#status_messageObject



36
37
38
# File 'lib/horn_of_plenty/response.rb', line 36

def status_message
  @status_message ||= body['message']
end

#successful?Boolean



40
41
42
# File 'lib/horn_of_plenty/response.rb', line 40

def successful?
  (200..299).cover? status_code
end