Class: LineNotify::Response

Inherits:
Object
  • Object
show all
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

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

Returns:

  • (Boolean)


87
88
89
# File 'lib/line_notify_client/response.rb', line 87

def bad_request?
  status == HTTP_STATUS_BAD_REQUEST
end

#bodyObject

### 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 “`

Returns:

  • (Boolean)


83
84
85
# File 'lib/line_notify_client/response.rb', line 83

def error?
  !success?
end

#headersObject

### 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

Returns:

  • (Boolean)


95
96
97
# File 'lib/line_notify_client/response.rb', line 95

def internal_error?
  status == HTTP_STATUS_INTERNAL_ERROR
end

#statusObject

### 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_messageObject

### 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 status_message
  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 “`

Returns:

  • (Boolean)


72
73
74
# File 'lib/line_notify_client/response.rb', line 72

def success?
  status == HTTP_STATUS_SUCCESS
end

#token_expired?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/line_notify_client/response.rb', line 91

def token_expired?
  status == HTTP_STATUS_TOKEN_EXPIRED
end