Class: ClassyPlivo::Response
- Inherits:
-
Object
- Object
- ClassyPlivo::Response
- Defined in:
- lib/classy_plivo/response.rb
Constant Summary collapse
- NoPayloadKeyError =
Raised when attempting to access a key that does not exist in Plivo’s response payload
Class.new(NoMethodError)
Instance Attribute Summary collapse
-
#http_status ⇒ Object
readonly
Returns the value of attribute http_status.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Instance Method Summary collapse
-
#error? ⇒ Boolean
True if 4XX or 5XX status code is received.
- #has_payload_key?(key) ⇒ Boolean
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
-
#method_missing(method, *args, &block) ⇒ Object
Delegate all other method calls to the payload hash.
Constructor Details
#initialize(response) ⇒ Response
9 10 11 |
# File 'lib/classy_plivo/response.rb', line 9 def initialize(response) @http_status, @payload = response end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Delegate all other method calls to the payload hash
23 24 25 26 27 28 29 30 31 |
# File 'lib/classy_plivo/response.rb', line 23 def method_missing(method, *args, &block) payload_key = method.to_s if has_payload_key?(payload_key) payload[payload_key] else fail NoPayloadKeyError, "Key '#{payload_key}' not found in response payload: #{payload}" end end |
Instance Attribute Details
#http_status ⇒ Object (readonly)
Returns the value of attribute http_status.
3 4 5 |
# File 'lib/classy_plivo/response.rb', line 3 def http_status @http_status end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
3 4 5 |
# File 'lib/classy_plivo/response.rb', line 3 def payload @payload end |
Instance Method Details
#error? ⇒ Boolean
True if 4XX or 5XX status code is received
14 15 16 |
# File 'lib/classy_plivo/response.rb', line 14 def error? http_status >= 400 && http_status <= 599 end |
#has_payload_key?(key) ⇒ Boolean
18 19 20 |
# File 'lib/classy_plivo/response.rb', line 18 def has_payload_key?(key) payload.has_key?(key) end |