Class: Inttegro::ResponseObject

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/inttegro/response_object.rb

Overview

Lightweight wrapper for API responses that allows both hash-style and method-style access while keeping values serializable.

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ ResponseObject

Returns a new instance of ResponseObject.



16
17
18
19
# File 'lib/inttegro/response_object.rb', line 16

def initialize(data = {})
  @data = T.let({}, T::Hash[String, Object])
  set_data(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



79
80
81
82
83
84
# File 'lib/inttegro/response_object.rb', line 79

def method_missing(name, *args, &block)
  key = name.to_s
  return @data[key] if @data.key?(key)

  super
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/inttegro/response_object.rb', line 22

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

#deserialize(klass) ⇒ Object



32
33
34
# File 'lib/inttegro/response_object.rb', line 32

def deserialize(klass)
  Inttegro.deserialize(to_h, klass)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/inttegro/response_object.rb', line 37

def respond_to_missing?(name, include_private = false)
  @data.key?(name.to_s) || super
end

#to_hObject



27
28
29
# File 'lib/inttegro/response_object.rb', line 27

def to_h
  @data.transform_values { |value| deep_unwrap(value) }
end