Class: Codelocks::Response
- Inherits:
-
Object
- Object
- Codelocks::Response
- Defined in:
- lib/codelocks/response.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#body ⇒ Hash, Nil
Parse the response from the server.
-
#error_message ⇒ String
Convenience method for fetching the error message.
-
#initialize(faraday_response) ⇒ Response
constructor
Initialize the response object.
-
#method_missing(method_name, *opts, &block) ⇒ String
Simple method missing accessor for reading returned attributes.
-
#success? ⇒ Truthy
Was the request successful?.
Constructor Details
#initialize(faraday_response) ⇒ Response
Initialize the response object
11 12 13 |
# File 'lib/codelocks/response.rb', line 11 def initialize(faraday_response) @response = faraday_response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *opts, &block) ⇒ String
Simple method missing accessor for reading returned attributes
47 48 49 |
# File 'lib/codelocks/response.rb', line 47 def method_missing(method_name, *opts, &block) body[method_name.to_s] if body.is_a?(Hash) end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response
5 6 7 |
# File 'lib/codelocks/response.rb', line 5 def response @response end |
Instance Method Details
#body ⇒ Hash, Nil
Parse the response from the server
27 28 29 |
# File 'lib/codelocks/response.rb', line 27 def body @body ||= JSON.parse(response.body) end |
#error_message ⇒ String
Convenience method for fetching the error message
35 36 37 38 39 40 41 |
# File 'lib/codelocks/response.rb', line 35 def if !success? body["Message"] end rescue JSON::ParserError => ex ex. end |
#success? ⇒ Truthy
Was the request successful?
19 20 21 |
# File 'lib/codelocks/response.rb', line 19 def success? response.success? end |