Class: GeneratorLabs::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/generatorlabs/response.rb

Overview

API response wrapper providing Hash-like access to response data and rate limit info.

Supports bracket notation (response) for backward compatibility with raw Hash returns, while also exposing rate_limit_info.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, rate_limit_info = nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • data (Hash)

    Parsed JSON response body

  • rate_limit_info (RateLimitInfo, nil) (defaults to: nil)

    Rate limit information



23
24
25
26
# File 'lib/generatorlabs/response.rb', line 23

def initialize(data, rate_limit_info = nil)
  @data = data
  @rate_limit_info = rate_limit_info
end

Instance Attribute Details

#rate_limit_infoRateLimitInfo? (readonly)

Returns Rate limit information from response headers.

Returns:

  • (RateLimitInfo, nil)

    Rate limit information from response headers



19
20
21
# File 'lib/generatorlabs/response.rb', line 19

def rate_limit_info
  @rate_limit_info
end

Instance Method Details

#[](key) ⇒ Object

Access response data by key (Hash-like access).

Parameters:

  • key (String)

    The key to look up

Returns:

  • (Object)

    The value



31
32
33
# File 'lib/generatorlabs/response.rb', line 31

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

#dig(*keys) ⇒ Object

Dig into nested response data.

Parameters:

  • keys (Array)

    Keys to dig through

Returns:

  • (Object)

    The nested value



38
39
40
# File 'lib/generatorlabs/response.rb', line 38

def dig(*keys)
  @data.dig(*keys)
end

#is_a?(klass) ⇒ Boolean

Check if the response data is a Hash (for type compatibility).

Parameters:

  • klass (Class)

    The class to check

Returns:

  • (Boolean)


58
59
60
# File 'lib/generatorlabs/response.rb', line 58

def is_a?(klass)
  klass == Hash ? true : super
end

#key?(key) ⇒ Boolean

Check if a key exists in the response data.

Parameters:

  • key (String)

    The key to check

Returns:

  • (Boolean)


45
46
47
# File 'lib/generatorlabs/response.rb', line 45

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

#to_hHash

Convert to a plain Hash.

Returns:

  • (Hash)


51
52
53
# File 'lib/generatorlabs/response.rb', line 51

def to_h
  @data
end