Class: Resend::Response
- Inherits:
-
Object
- Object
- Resend::Response
- Includes:
- Enumerable
- Defined in:
- lib/resend/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 []=.
-
#as_json(*args) ⇒ Hash
Serialize response data for encoders that call as_json, such as ActiveSupport.
-
#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.
-
#to_json(*args) ⇒ String
Serialize response data without response headers.
-
#transform_keys!(&block) ⇒ Resend::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/resend/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
118 119 120 121 122 123 124 125 126 |
# File 'lib/resend/response.rb', line 118 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/resend/response.rb', line 29 def headers @headers end |
Instance Method Details
#[](key) ⇒ Object
Hash-like access via []
34 35 36 |
# File 'lib/resend/response.rb', line 34 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
Hash-like assignment via []=
41 42 43 |
# File 'lib/resend/response.rb', line 41 def []=(key, value) @data[key] = value end |
#as_json(*args) ⇒ Hash
Serialize response data for encoders that call as_json, such as ActiveSupport
69 70 71 |
# File 'lib/resend/response.rb', line 69 def as_json(*args) @data.respond_to?(:as_json) ? @data.as_json(*args) : @data end |
#dig(*keys) ⇒ Object
Dig into nested hash structure
48 49 50 |
# File 'lib/resend/response.rb', line 48 def dig(*keys) @data.dig(*keys) end |
#each(&block) ⇒ Object
Enable enumeration over the data
95 96 97 |
# File 'lib/resend/response.rb', line 95 def each(&block) @data.each(&block) end |
#empty? ⇒ Boolean
Check if response is empty
108 109 110 |
# File 'lib/resend/response.rb', line 108 def empty? @data.empty? end |
#inspect ⇒ String
String representation for debugging
130 131 132 |
# File 'lib/resend/response.rb', line 130 def inspect "#<Resend::Response data=#{@data.inspect} headers=#{@headers.keys.inspect}>" end |
#key?(key) ⇒ Boolean Also known as: has_key?
Check if key exists
88 89 90 |
# File 'lib/resend/response.rb', line 88 def key?(key) @data.key?(key) end |
#keys ⇒ Array
Get all keys from the data
75 76 77 |
# File 'lib/resend/response.rb', line 75 def keys @data.keys end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to hash-like methods
113 114 115 |
# File 'lib/resend/response.rb', line 113 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/resend/response.rb', line 54 def to_h @data end |
#to_json(*args) ⇒ String
Serialize response data without response headers
62 63 64 |
# File 'lib/resend/response.rb', line 62 def to_json(*args) @data.to_json(*args) end |
#transform_keys!(&block) ⇒ Resend::Response
Transform keys in the underlying data
101 102 103 104 |
# File 'lib/resend/response.rb', line 101 def transform_keys!(&block) @data.transform_keys!(&block) self end |
#values ⇒ Array
Get all values from the data
81 82 83 |
# File 'lib/resend/response.rb', line 81 def values @data.values end |