Class: Button::Response
- Inherits:
-
Object
- Object
- Button::Response
- Defined in:
- lib/button/response.rb
Overview
Response is a simple proxy class for easy value unpacking from an API response. It is constructed with a hash and proxies method calls on the instance down to keys in the underling hash.
## Usage
response = Button::Response.new({ :a => 1, :b => “two” }) puts response.a puts response.to_hash
Instance Method Summary collapse
-
#initialize(attrs) ⇒ Response
constructor
A new instance of Response.
- #method_missing(attr) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #to_hash ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(attrs) ⇒ Response
Returns a new instance of Response.
13 14 15 |
# File 'lib/button/response.rb', line 13 def initialize(attrs) @attrs = attrs || {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(attr) ⇒ Object
29 30 31 |
# File 'lib/button/response.rb', line 29 def method_missing(attr) @attrs[attr] || super end |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
33 34 35 |
# File 'lib/button/response.rb', line 33 def respond_to_missing?(method_name, include_private = false) @attrs.key?(method_name) || super end |
#to_hash ⇒ Object
25 26 27 |
# File 'lib/button/response.rb', line 25 def to_hash @attrs end |
#to_s ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/button/response.rb', line 17 def to_s values = @attrs.reduce([]) do |acc, (name, value)| acc + ["#{name}: #{value || 'nil'}"] end.join(', ') "Button::Response(#{values})" end |