Class: LineNotify::Response
- Inherits:
-
Object
- Object
- LineNotify::Response
- Defined in:
- lib/line_notify_client/response.rb
Overview
## LineNotify::Response A class for response data returned from API.
Constant Summary collapse
- HTTP_STATUS_SUCCESS =
200
- HTTP_STATUS_BAD_REQUEST =
400
- HTTP_STATUS_TOKEN_EXPIRED =
401
- HTTP_STATUS_INTERNAL_ERROR =
500
Instance Method Summary collapse
- #bad_request? ⇒ Boolean
-
#body ⇒ Object
### LineNotify::Response#body Returns response body returned from API as a ‘Hash`.
-
#error? ⇒ Boolean
### LineNotify::Response#status Returns response status from API as a ‘TrueClass` or `FalseClass`.
-
#headers ⇒ Object
### LineNotify::Response#headers Returns response headers returned from API as a ‘Hash`.
-
#initialize(faraday_response) ⇒ Response
constructor
A new instance of Response.
- #internal_error? ⇒ Boolean
-
#status ⇒ Object
### LineNotify::Response#status Returns response status code returned from API as a ‘Integer`.
-
#status_message ⇒ Object
### LineNotify::Response#status_message Returns response status message returned from API as a ‘String`.
-
#success? ⇒ Boolean
### LineNotify::Response#success? Returns success boolean value from API as a ‘TrueClass` or `FalseClass`.
- #token_expired? ⇒ Boolean
Constructor Details
#initialize(faraday_response) ⇒ Response
Returns a new instance of Response.
13 14 15 16 17 |
# File 'lib/line_notify_client/response.rb', line 13 def initialize(faraday_response) @raw_body = faraday_response.body @raw_headers = faraday_response.headers @raw_status = faraday_response.status end |
Instance Method Details
#bad_request? ⇒ Boolean
87 88 89 |
# File 'lib/line_notify_client/response.rb', line 87 def bad_request? status == HTTP_STATUS_BAD_REQUEST end |
#body ⇒ Object
### LineNotify::Response#body Returns response body returned from API as a ‘Hash`.
“‘rb response.body #=> { … } “`
26 27 28 |
# File 'lib/line_notify_client/response.rb', line 26 def body @raw_body end |
#error? ⇒ Boolean
### LineNotify::Response#status Returns response status from API as a ‘TrueClass` or `FalseClass`.
“‘rb response.success? #=> true “`
83 84 85 |
# File 'lib/line_notify_client/response.rb', line 83 def error? !success? end |
#headers ⇒ Object
### LineNotify::Response#headers Returns response headers returned from API as a ‘Hash`.
“‘rb response.headers #=> { “Content-Type” => “application/json” } “`
37 38 39 40 41 |
# File 'lib/line_notify_client/response.rb', line 37 def headers @headers ||= @raw_headers.inject({}) do |result, (key, value)| result.merge(key.split("-").map(&:capitalize).join("-") => value) end end |
#internal_error? ⇒ Boolean
95 96 97 |
# File 'lib/line_notify_client/response.rb', line 95 def internal_error? status == HTTP_STATUS_INTERNAL_ERROR end |
#status ⇒ Object
### LineNotify::Response#status Returns response status code returned from API as a ‘Integer`.
“‘rb response.status #=> 200 “`
50 51 52 |
# File 'lib/line_notify_client/response.rb', line 50 def status @raw_status end |
#status_message ⇒ Object
### LineNotify::Response#status_message Returns response status message returned from API as a ‘String`.
“‘rb response.status_message #=> “OK” “`
61 62 63 |
# File 'lib/line_notify_client/response.rb', line 61 def Rack::Utils::HTTP_STATUS_CODES[status] end |
#success? ⇒ Boolean
### LineNotify::Response#success? Returns success boolean value from API as a ‘TrueClass` or `FalseClass`.
“‘rb response.success? #=> true “`
72 73 74 |
# File 'lib/line_notify_client/response.rb', line 72 def success? status == HTTP_STATUS_SUCCESS end |
#token_expired? ⇒ Boolean
91 92 93 |
# File 'lib/line_notify_client/response.rb', line 91 def token_expired? status == HTTP_STATUS_TOKEN_EXPIRED end |