Class: PYR::Response
- Inherits:
-
Object
- Object
- PYR::Response
- Defined in:
- lib/pyr/response.rb
Overview
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns a hash of the Raw JSON response.
-
#code ⇒ Object
readonly
Returns an integer of the HTTP status code.
-
#controller ⇒ Object
readonly
Returns a symbol representing the Responding API controller.
-
#headers ⇒ Object
readonly
Returns a hash of the response HTTP headers.
-
#objects ⇒ Object
readonly
Returns a collection of PYR::ResponseObjects representing API objects.
-
#path ⇒ Object
readonly
Returns a string of the URI path.
-
#reason_phrase ⇒ Object
readonly
Returns a string of the response HTTP reason phrase.
Instance Method Summary collapse
- #assign_url_and_controller(uri, resource, response_object) ⇒ Object
- #fetch_and_parse_payload ⇒ Object
-
#initialize(uri: nil, resource: nil, response_object: nil) ⇒ Response
constructor
A new instance of Response.
- #parse_objects ⇒ Object
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
#body ⇒ Object (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 |
#code ⇒ Object (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 |
#controller ⇒ Object (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 |
#headers ⇒ Object (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 |
#objects ⇒ Object (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 |
#path ⇒ Object (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_phrase ⇒ Object (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_payload ⇒ Object
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 |