Class: EML::Response
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #body ⇒ Object
- #error ⇒ Object
- #headers ⇒ Object
- #http_status ⇒ Object
-
#initialize(response, id: nil) ⇒ Response
constructor
A new instance of Response.
- #success? ⇒ Boolean
- #url ⇒ Object
Constructor Details
#initialize(response, id: nil) ⇒ Response
Returns a new instance of Response.
17 18 19 20 21 22 23 |
# File 'lib/eml/response.rb', line 17 def initialize(response, id: nil) @response = T.let(response, HTTP::Response) @body = T.let(nil, T.nilable(String)) @http_status = T.let(nil, T.nilable(Integer)) @url = T.let(nil, T.nilable(String)) raise_error(id: id) unless http_status == 200 end |
Class Method Details
.field(field_name) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/eml/response.rb', line 9 def self.field(field_name) define_method(field_name) do string_name = field_name.to_s T.unsafe(self).body[string_name] end end |
Instance Method Details
#body ⇒ Object
28 29 30 31 32 |
# File 'lib/eml/response.rb', line 28 def body @body ||= JSON.parse(@response.body.to_s) rescue JSON::ParserError @body = "" end |
#error ⇒ Object
35 36 37 |
# File 'lib/eml/response.rb', line 35 def error body.fetch("message", nil) if body.respond_to?(:fetch) end |
#headers ⇒ Object
40 41 42 |
# File 'lib/eml/response.rb', line 40 def headers @response.headers end |
#http_status ⇒ Object
45 46 47 |
# File 'lib/eml/response.rb', line 45 def http_status @http_status ||= @response.status.code end |
#success? ⇒ Boolean
50 51 52 |
# File 'lib/eml/response.rb', line 50 def success? http_status == 200 end |
#url ⇒ Object
55 56 57 |
# File 'lib/eml/response.rb', line 55 def url @url ||= T.unsafe(@response).url.to_s end |