Class: HybiscusPdfReport::ResponseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/hybiscus_pdf_report/response_object.rb

Overview

Base class for all objects returned by the Hybiscus PDF Reports API

Direct Known Subclasses

Response

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ ResponseObject

Returns a new instance of ResponseObject.



8
9
10
11
# File 'lib/hybiscus_pdf_report/response_object.rb', line 8

def initialize(attributes)
  @original_attributes = attributes || {}
  @attributes = OpenStruct.new(@original_attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hybiscus_pdf_report/response_object.rb', line 26

def method_missing(method, *args, &block)
  # Handle attribute access
  if @attributes.respond_to?(method)
    attribute = @attributes.send(method, *args, &block)
    return attribute.is_a?(Hash) ? ResponseObject.new(attribute) : attribute
  end

  # Try to access as OpenStruct attribute (this will return nil for non-existent attributes)
  begin
    attribute = @attributes.public_send(method, *args, &block)
    attribute.is_a?(Hash) ? ResponseObject.new(attribute) : attribute
  rescue NoMethodError
    # If OpenStruct doesn't recognize it, delegate to super
    super
  end
end

Instance Method Details

#inspectObject



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

def inspect
  @attributes.inspect
end

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/hybiscus_pdf_report/response_object.rb', line 43

def respond_to_missing?(method, include_private = false)
  @attributes.respond_to?(method) || super
end

#to_hObject

Delegate certain OpenStruct methods directly



14
15
16
# File 'lib/hybiscus_pdf_report/response_object.rb', line 14

def to_h
  @attributes.to_h
end

#to_sObject



18
19
20
# File 'lib/hybiscus_pdf_report/response_object.rb', line 18

def to_s
  @attributes.to_s
end