Class: EmailFuse::Response

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

Examples:

Backwards compatible hash access

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

Accessing response headers

response = EmailFuse::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:

  • The response data

  • The response headers



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

#headersHash (readonly)

Access response headers

Returns:

  • Response headers as a hash with lowercase string keys



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

def headers
  @headers
end

Instance Method Details

#[](key) ⇒ Object

Hash-like access via []

Parameters:

  • The key to access

Returns:

  • The value at the key



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

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

#[]=(key, value) ⇒ Object

Hash-like assignment via []=

Parameters:

  • The key to set

  • The value to set



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

Parameters:

  • Keys to dig through

Returns:

  • The value at the nested key path



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

Returns:

  • True if data is empty



95
96
97
# File 'lib/email_fuse/response.rb', line 95

def empty?
  @data.empty?
end

#inspectString

String representation for debugging

Returns:

  • String representation of the response



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

Parameters:

  • The key to check

Returns:

  • True if key exists



75
76
77
# File 'lib/email_fuse/response.rb', line 75

def key?(key)
  @data.key?(key)
end

#keysArray

Get all keys from the data

Returns:

  • Array of keys



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

Returns:



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_hHash Also known as: to_hash

Convert to plain hash

Returns:

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

Returns:

  • Self for chaining



88
89
90
91
# File 'lib/email_fuse/response.rb', line 88

def transform_keys!(&block)
  @data.transform_keys!(&block)
  self
end

#valuesArray

Get all values from the data

Returns:

  • Array of values



68
69
70
# File 'lib/email_fuse/response.rb', line 68

def values
  @data.values
end