Class: MasonHubAPI::Object
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- MasonHubAPI::Object
- Defined in:
- lib/mason_hub_api/object.rb
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Object
constructor
A new instance of Object.
- #to_ostruct(obj) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Object
Returns a new instance of Object.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mason_hub_api/object.rb', line 10 def initialize(attributes) parsed = if attributes.is_a?(String) begin JSON.parse(attributes) rescue JSON::ParserError raise MasonHubAPI::Error, "MasonHub returned an unexpected non-JSON response: #{attributes.truncate(200)}" end else attributes end super to_ostruct(parsed) end |
Instance Method Details
#to_ostruct(obj) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/mason_hub_api/object.rb', line 26 def to_ostruct(obj) if obj.is_a?(Hash) OpenStruct.new(obj.map { |key, val| [key.to_s.underscore, to_ostruct(val)] }.to_h) elsif obj.is_a?(Array) obj.map { |o| to_ostruct(o) } else # Assumed to be a primitive value obj end end |