Module: TreasureData::API::ToHashStruct::ClassModule
- Included in:
- TreasureData::API::ToHashStruct
- Defined in:
- lib/td/client/api/to_hash_struct.rb
Instance Method Summary collapse
- #from_hash(hash) ⇒ Object
- #from_json(json) ⇒ Object
- #model ⇒ Object
- #model_property(key, klass) ⇒ Object
- #parse_json(body) ⇒ Object
Instance Method Details
#from_hash(hash) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/td/client/api/to_hash_struct.rb', line 20 def from_hash(hash) return new if hash.nil? new(*members.map { |sym| v = hash[sym] || hash[sym.to_s] model.key?(sym) ? model[sym].from_hash(v) : v }) end |
#from_json(json) ⇒ Object
16 17 18 |
# File 'lib/td/client/api/to_hash_struct.rb', line 16 def from_json(json) from_hash(parse_json(json)) end |
#model ⇒ Object
32 33 34 |
# File 'lib/td/client/api/to_hash_struct.rb', line 32 def model @model ||= {} end |
#model_property(key, klass) ⇒ Object
28 29 30 |
# File 'lib/td/client/api/to_hash_struct.rb', line 28 def model_property(key, klass) model[key.to_sym] = klass end |
#parse_json(body) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/td/client/api/to_hash_struct.rb', line 4 def parse_json(body) begin js = JSON.load(body) rescue raise "Unexpected API response: #{$!}" end unless js.is_a?(Hash) raise "Unexpected API response: #{body}" end js end |