Class: Rev::ApiSerializable
- Inherits:
-
Object
- Object
- Rev::ApiSerializable
- Defined in:
- lib/rev-api/api_serializable.rb
Overview
Utility class with instance methods for hash/JSON conversion
Direct Known Subclasses
Attachment, BillingAddress, CaptionInfo, Comment, Input, InputOptions, Link, Notification, Order, OrderRequest, OrdersListPage, Payment, TranscriptionInfo
Instance Method Summary collapse
-
#initialize(fields = {}) ⇒ ApiSerializable
constructor
Map given hash to instance properties.
-
#to_hash ⇒ Hash
Recursively convert object to hash.
-
#to_json(*args) ⇒ Object
Recursively convert object to JSON (internally utilizing hash).
Constructor Details
#initialize(fields = {}) ⇒ ApiSerializable
Map given hash to instance properties
8 9 10 |
# File 'lib/rev-api/api_serializable.rb', line 8 def initialize(fields = {}) fields.each { |k,v| self.instance_variable_set("@#{k.to_sym}", v) if self.methods.include? k.to_sym } end |
Instance Method Details
#to_hash ⇒ Hash
Recursively convert object to hash
16 17 18 19 20 21 22 23 |
# File 'lib/rev-api/api_serializable.rb', line 16 def to_hash h = {} instance_variables.each do |e| o = instance_variable_get e.to_sym h[e[1..-1]] = (o.respond_to? :to_hash) ? o.to_hash : o; end h end |
#to_json(*args) ⇒ Object
Recursively convert object to JSON (internally utilizing hash)
26 27 28 |
# File 'lib/rev-api/api_serializable.rb', line 26 def to_json *args to_hash.to_json *args end |