Class: FastBound::Response
- Inherits:
-
Object
- Object
- FastBound::Response
- Defined in:
- lib/fastbound-ruby/response.rb
Instance Attribute Summary collapse
-
#success ⇒ Object
Returns the value of attribute success.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #body ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #success? ⇒ Boolean
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fastbound-ruby/response.rb', line 6 def initialize(response) @response = response case @response when Net::HTTPUnauthorized raise FastBound::Error::NotAuthorized.new(@response.body) when Net::HTTPNotFound raise FastBound::Error::NotFound.new(@response.body) when Net::HTTPOK, Net::HTTPSuccess, Net::HTTPNoContent, Net::HTTPCreated self.success = true _data = (JSON.parse(@response.body) if @response.body.present?) @data = case when _data.is_a?(Hash) _data.deep_symbolize_keys when _data.is_a?(Array) _data.map(&:deep_symbolize_keys) end else if FastBound.config.full_debug? puts "-- DEBUG: #{self}: RequestError: #{@response.inspect}" end = begin JSON.parse(@response.body)['errors'].map { |error| error['message'].chomp('.') }.join('. ') rescue [@response., @response.body].reject(&:blank?).join(" | ") end raise FastBound::Error::RequestError.new() end end |
Instance Attribute Details
#success ⇒ Object
Returns the value of attribute success.
4 5 6 |
# File 'lib/fastbound-ruby/response.rb', line 4 def success @success end |
Instance Method Details
#[](key) ⇒ Object
39 40 41 |
# File 'lib/fastbound-ruby/response.rb', line 39 def [](key) @data[key] end |
#body ⇒ Object
43 44 45 |
# File 'lib/fastbound-ruby/response.rb', line 43 def body @data end |
#fetch(key) ⇒ Object
47 48 49 |
# File 'lib/fastbound-ruby/response.rb', line 47 def fetch(key) @data.fetch(key) end |
#success? ⇒ Boolean
51 52 53 |
# File 'lib/fastbound-ruby/response.rb', line 51 def success? !!success end |