Class: Resend::Response

Inherits:
Object
  • Object
show all
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.

Examples:

Backwards compatible hash access

response = Resend::Emails.send(params)
response[:id]  # => "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"

Accessing response headers

response = Resend::Emails.send(params)
response.headers  # => {"content-type" => "application/json", ...}
response.headers['x-ratelimit-remaining']  # => "50"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, headers) ⇒ Response

Returns a new instance of Response.

Parameters:

  • data (Hash) —

    The response data

  • headers (Hash, HTTParty::Response) —

    The response headers



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

Returns:

  • (Hash) —

    Response headers as a hash with lowercase string keys



29
30
31
# File 'lib/resend/response.rb', line 29

def headers
  @headers
end

Instance Method Details

#[](key) ⇒ Object

Hash-like access via []

Parameters:

  • key (Symbol, String) —

    The key to access

Returns:

  • (Object) —

    The value at the key



34
35
36
# File 'lib/resend/response.rb', line 34

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object

Hash-like assignment via []=

Parameters:

  • key (Symbol, String) —

    The key to set

  • value (Object) —

    The value to set



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

Parameters:

  • args (Array) —

    Options forwarded to Hash#as_json when it is defined

Returns:

  • (Hash) —

    The response data in as_json form



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

Parameters:

  • keys (Array<Symbol, String>) —

    Keys to dig through

Returns:

  • (Object) —

    The value at the nested key path



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

Returns:

  • (Boolean) —

    True if data is empty



108
109
110
# File 'lib/resend/response.rb', line 108

def empty?
  @data.empty?
end

#inspect ⇒ String

String representation for debugging

Returns:

  • (String) —

    String representation of the response



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

Parameters:

  • key (Symbol, String) —

    The key to check

Returns:

  • (Boolean) —

    True 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

Returns:

  • (Array) —

    Array of keys



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

Returns:

  • (Boolean)


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

Returns:

  • (Hash) —

    The underlying data 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

Returns:

  • (String) —

    JSON representation of the response data



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

Returns:



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

Returns:

  • (Array) —

    Array of values



81
82
83
# File 'lib/resend/response.rb', line 81

def values
  @data.values
end