Class: EmailFuse::Response
- Inherits:
-
Object
- Object
- EmailFuse::Response
- Includes:
- Enumerable
- Defined in:
- lib/email_fuse/response.rb
Overview
Response wrapper that maintains backwards compatibility while exposing headers
This class wraps API responses and behaves like a Hash for backwards compatibility, while also providing access to response headers via the #headers method.
Instance Attribute Summary collapse
-
#headers ⇒ Hash
readonly
Access response headers.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Hash-like access via [].
-
#[]=(key, value) ⇒ Object
Hash-like assignment via []=.
-
#dig(*keys) ⇒ Object
Dig into nested hash structure.
-
#each(&block) ⇒ Object
Enable enumeration over the data.
-
#empty? ⇒ Boolean
Check if response is empty.
-
#initialize(data, headers) ⇒ Response
constructor
A new instance of Response.
-
#inspect ⇒ String
String representation for debugging.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Check if key exists.
-
#keys ⇒ Array
Get all keys from the data.
-
#method_missing(method_name, *args, &block) ⇒ Object
Delegate unknown methods to the underlying data hash.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to hash-like methods.
-
#to_h ⇒ Hash
(also: #to_hash)
Convert to plain hash.
-
#transform_keys!(&block) ⇒ EmailFuse::Response
Transform keys in the underlying data.
-
#values ⇒ Array
Get all values from the data.
Constructor Details
#initialize(data, headers) ⇒ Response
Returns a new instance of Response.
22 23 24 25 |
# File 'lib/email_fuse/response.rb', line 22 def initialize(data, headers) @data = data.is_a?(Hash) ? data : {} @headers = normalize_headers(headers) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Delegate unknown methods to the underlying data hash
105 106 107 108 109 110 111 112 113 |
# File 'lib/email_fuse/response.rb', line 105 def method_missing(method_name, *args, &block) if @data.respond_to?(method_name) result = @data.send(method_name, *args, &block) # If the method returns the hash itself, return self to maintain wrapper result.equal?(@data) ? self : result else super end end |
Instance Attribute Details
#headers ⇒ Hash (readonly)
Access response headers
29 30 31 |
# File 'lib/email_fuse/response.rb', line 29 def headers @headers end |
Instance Method Details
#[](key) ⇒ Object
Hash-like access via []
34 35 36 |
# File 'lib/email_fuse/response.rb', line 34 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
Hash-like assignment via []=
41 42 43 |
# File 'lib/email_fuse/response.rb', line 41 def []=(key, value) @data[key] = value end |
#dig(*keys) ⇒ Object
Dig into nested hash structure
48 49 50 |
# File 'lib/email_fuse/response.rb', line 48 def dig(*keys) @data.dig(*keys) end |
#each(&block) ⇒ Object
Enable enumeration over the data
82 83 84 |
# File 'lib/email_fuse/response.rb', line 82 def each(&block) @data.each(&block) end |
#empty? ⇒ Boolean
Check if response is empty
95 96 97 |
# File 'lib/email_fuse/response.rb', line 95 def empty? @data.empty? end |
#inspect ⇒ String
String representation for debugging
117 118 119 |
# File 'lib/email_fuse/response.rb', line 117 def inspect "#<EmailFuse::Response data=#{@data.inspect} headers=#{@headers.keys.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?
Check if key exists
75 76 77 |
# File 'lib/email_fuse/response.rb', line 75 def key?(key) @data.key?(key) end |
#keys ⇒ Array
Get all keys from the data
62 63 64 |
# File 'lib/email_fuse/response.rb', line 62 def keys @data.keys end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to hash-like methods
100 101 102 |
# File 'lib/email_fuse/response.rb', line 100 def respond_to_missing?(method_name, include_private = false) @data.respond_to?(method_name) || super end |
#to_h ⇒ Hash Also known as: to_hash
Convert to plain hash
54 55 56 |
# File 'lib/email_fuse/response.rb', line 54 def to_h @data end |
#transform_keys!(&block) ⇒ EmailFuse::Response
Transform keys in the underlying data
88 89 90 91 |
# File 'lib/email_fuse/response.rb', line 88 def transform_keys!(&block) @data.transform_keys!(&block) self end |
#values ⇒ Array
Get all values from the data
68 69 70 |
# File 'lib/email_fuse/response.rb', line 68 def values @data.values end |