Class: Plotlyrb::Response
- Inherits:
-
Struct
- Object
- Struct
- Plotlyrb::Response
- Defined in:
- lib/plotlyrb/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#msg ⇒ Object
Returns the value of attribute msg.
-
#success ⇒ Object
Returns the value of attribute success.
Class Method Summary collapse
- .all_have_key?(hs, key) ⇒ Boolean
- .fail(msg) ⇒ Object
- .from_http_response(rsp) ⇒ Object
-
.get_errors(s) ⇒ Object
s is a Net::HTTP::Response body we expect to contain JSON map with a list of errors.
Instance Method Summary collapse
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
2 3 4 |
# File 'lib/plotlyrb/response.rb', line 2 def body @body end |
#errors ⇒ Object
Returns the value of attribute errors
2 3 4 |
# File 'lib/plotlyrb/response.rb', line 2 def errors @errors end |
#msg ⇒ Object
Returns the value of attribute msg
2 3 4 |
# File 'lib/plotlyrb/response.rb', line 2 def msg @msg end |
#success ⇒ Object
Returns the value of attribute success
2 3 4 |
# File 'lib/plotlyrb/response.rb', line 2 def success @success end |
Class Method Details
.all_have_key?(hs, key) ⇒ Boolean
34 35 36 |
# File 'lib/plotlyrb/response.rb', line 34 def self.all_have_key?(hs, key) hs.all? { |h| h.has_key?(key) } end |
.fail(msg) ⇒ Object
18 19 20 |
# File 'lib/plotlyrb/response.rb', line 18 def self.fail(msg) new(false, '', msg, [msg]) end |
.from_http_response(rsp) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/plotlyrb/response.rb', line 3 def self.from_http_response(rsp) case rsp.code when '200' new(true, rsp.body, '200 - OK', []) when '201' new(true, rsp.body, '201 - CREATED', []) when '400' new(false, rsp.body, '400 - BAD REQUEST', get_errors(rsp.body)) when '404' new(false, rsp.body, '404 - NOT FOUND', ['Appears we got an endpoint wrong']) else new(false, rsp.body, "#{rsp.code} - UNHANDLED", ['Unhandled error']) end end |
.get_errors(s) ⇒ Object
s is a Net::HTTP::Response body we expect to contain JSON map with a list of errors
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/plotlyrb/response.rb', line 23 def self.get_errors(s) msg_key = 'message' h = JSON.parse(s) es = h['errors'] if es.nil? || !es.is_a?(Array) || !all_have_key?(es, msg_key) return ['Failed to parse plotly error response - check raw body'] end es.map { |e| e.fetch(msg_key) } end |
Instance Method Details
#to_s ⇒ Object
38 39 40 |
# File 'lib/plotlyrb/response.rb', line 38 def to_s "#{success ? 'Succeeded' : 'Failed'}: #{msg}" end |