Class: EnotasApi::JsonResult
Instance Attribute Summary
Attributes inherited from RawResult
#content, #raw_content, #status_code
Instance Method Summary
collapse
Methods inherited from RawResult
#error?, #success?
Constructor Details
#initialize(status_code, json) ⇒ JsonResult
Returns a new instance of JsonResult.
8
9
10
11
12
13
|
# File 'lib/enotas_api/common/json_result.rb', line 8
def initialize(status_code, json)
@data = JSON.parse(json) unless json.nil? || json.empty?
@data = @data.first if @data.is_a?(Array) && @data.any?
content = @data.is_a?(String) ? @data : json
super(status_code, content, json)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(field, *_args) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/enotas_api/common/json_result.rb', line 25
def method_missing(field, *_args)
if complex_object? && @data.key?(field.to_s)
value = @data[field.to_s]
return case value
when Array then value.map { |v| EnotasApi::JsonResult.new(@status_code, v.to_json) }
when Hash then EnotasApi::JsonResult.new(@status_code, value.to_json)
else value
end
end
super
end
|
Instance Method Details
#complex_object? ⇒ Boolean
38
39
40
|
# File 'lib/enotas_api/common/json_result.rb', line 38
def complex_object?
!@data.nil? && @data.is_a?(Hash)
end
|
#respond_to_missing?(field, _include_private) ⇒ Boolean
19
20
21
22
23
|
# File 'lib/enotas_api/common/json_result.rb', line 19
def respond_to_missing?(field, _include_private)
return true if complex_object? && @data.key?(field.to_s)
super
end
|
#to_json(*_args) ⇒ Object
15
16
17
|
# File 'lib/enotas_api/common/json_result.rb', line 15
def to_json(*_args)
@data.to_json
end
|