Class: PYR::Response

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

Overview

The object returned by a request call to the API.

Example

response = PYR.call :reps, 'S000033'
# => #<PYR::Response:0x007fe8f90829f8 ... >

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri: nil, resource: nil, response_object: nil) ⇒ Response

Returns a new instance of Response.



41
42
43
44
45
# File 'lib/pyr/response.rb', line 41

def initialize(uri: nil, resource: nil, response_object: nil)
  assign_url_and_controller(uri, resource, response_object)
  fetch_and_parse_payload
  parse_objects
end

Instance Attribute Details

#bodyObject (readonly)

Returns a hash of the Raw JSON response

response.body # => { ... }


12
13
14
# File 'lib/pyr/response.rb', line 12

def body
  @body
end

#codeObject (readonly)

Returns an integer of the HTTP status code

response.code # => 200


20
21
22
# File 'lib/pyr/response.rb', line 20

def code
  @code
end

#controllerObject (readonly)

Returns a symbol representing the Responding API controller

response.controller # => :reps


39
40
41
# File 'lib/pyr/response.rb', line 39

def controller
  @controller
end

#headersObject (readonly)

Returns a hash of the response HTTP headers

response.headers # => {"server"=>"Cowboy", "connection"=>"close", ... }


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

def headers
  @headers
end

#objectsObject (readonly)

Returns a collection of PYR::ResponseObjects representing API objects

response.objects
# => #<PYR::RepRelation [#<PYR::Rep ... >]>
response.objects.first.office_locations
# => #<PYR::OfficeLocationRelation [#<PYR::OfficeLocation ... >]>


35
36
37
# File 'lib/pyr/response.rb', line 35

def objects
  @objects
end

#pathObject (readonly)

Returns a string of the URI path

response.path # => 'reps/S000033'


16
17
18
# File 'lib/pyr/response.rb', line 16

def path
  @path
end

#reason_phraseObject (readonly)

Returns a string of the response HTTP reason phrase

response.reason_phrase # => "OK"


24
25
26
# File 'lib/pyr/response.rb', line 24

def reason_phrase
  @reason_phrase
end

Instance Method Details

#assign_url_and_controller(uri, resource, response_object) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pyr/response.rb', line 47

def assign_url_and_controller(uri, resource, response_object)
  if resource
    @controller = resource.controller
    @path       = resource.to_s
  elsif uri
    @path       = uri.sub(API_BASE_URI, '')
    @controller = @path.split('/').first
  elsif response_object
    @controller = response_object.controller
    @path       = response_object.self.sub(API_BASE_URI, '')
  end
end

#fetch_and_parse_payloadObject



60
61
62
63
64
65
66
# File 'lib/pyr/response.rb', line 60

def fetch_and_parse_payload
  response       = API_CONN.get path
  @body          = response.body
  @code          = response.status
  @reason_phrase = response.reason_phrase
  @headers       = response.headers
end

#parse_objectsObject



68
69
70
# File 'lib/pyr/response.rb', line 68

def parse_objects
  @objects = Parser.parse(body, controller)
end