Class: Stretchr::Response
- Inherits:
-
Object
- Object
- Stretchr::Response
- Defined in:
- lib/stretchr/response.rb
Overview
Response parser for stretchr
Parameters
response
- The raw response from stretchr options[:api_version]
- The version of the api we’re talking to, tells us how to parse
Instance Attribute Summary collapse
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#changes ⇒ Object
readonly
Returns the value of attribute changes.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#parsed ⇒ Object
readonly
Returns the value of attribute parsed.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(response, options = {}) ⇒ Response
constructor
A new instance of Response.
-
#success? ⇒ Boolean
Returns whether or not the request was successful.
Constructor Details
#initialize(response, options = {}) ⇒ Response
Returns a new instance of Response.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/stretchr/response.rb', line 9 def initialize(response, = {}) @api_version = [:api_version] || Stretchr.config["api_version"] #api version is used to determine how to parse response @raw = response @parsed = JSON.parse(response) begin @data = @parsed[Stretchr.config[@api_version]["data"]] #stretchr data object @errors = @parsed[Stretchr.config[@api_version]["errors"]] #errors @changes = @parsed[Stretchr.config[@api_version]["changes"]] #changes (POST/PUT/PATCH/DELETE) @status = @parsed[Stretchr.config[@api_version]["status"]] #request status if @data @items = @data[Stretchr.config[@api_version]["items"]] end rescue end end |
Instance Attribute Details
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
8 9 10 |
# File 'lib/stretchr/response.rb', line 8 def api_version @api_version end |
#changes ⇒ Object (readonly)
Returns the value of attribute changes.
8 9 10 |
# File 'lib/stretchr/response.rb', line 8 def changes @changes end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/stretchr/response.rb', line 8 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/stretchr/response.rb', line 8 def errors @errors end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
8 9 10 |
# File 'lib/stretchr/response.rb', line 8 def items @items end |
#parsed ⇒ Object (readonly)
Returns the value of attribute parsed.
8 9 10 |
# File 'lib/stretchr/response.rb', line 8 def parsed @parsed end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
8 9 10 |
# File 'lib/stretchr/response.rb', line 8 def raw @raw end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
8 9 10 |
# File 'lib/stretchr/response.rb', line 8 def status @status end |
Instance Method Details
#success? ⇒ Boolean
Returns whether or not the request was successful
26 27 28 |
# File 'lib/stretchr/response.rb', line 26 def success? @status >= 200 && @status <= 299 end |