Class: BookingSync::API::Serializer
- Inherits:
-
Object
- Object
- BookingSync::API::Serializer
- Defined in:
- lib/bookingsync/api/serializer.rb
Instance Method Summary collapse
-
#decode(data) ⇒ Object
(also: #load)
Public: Decodes a String into an Object (usually a Hash or Array of Hashes).
- #decode_hash(hash) ⇒ Object
- #decode_hash_value(key, value) ⇒ Object
- #decode_object(data) ⇒ Object
-
#encode(data) ⇒ Object
(also: #dump)
Public: Encodes an Object (usually a Hash or Array of Hashes).
- #encode_hash(hash) ⇒ Object
- #encode_object(data) ⇒ Object
-
#initialize(dump_method_name = nil, load_method_name = nil) ⇒ Serializer
constructor
Public: Wraps a serialization format for Sawyer.
- #time_field?(key, value) ⇒ Boolean
Constructor Details
#initialize(dump_method_name = nil, load_method_name = nil) ⇒ Serializer
Public: Wraps a serialization format for Sawyer. Nested objects are prepared for serialization (such as changing Times to ISO 8601 Strings). Any serialization format that responds to #dump and #load will work.
10 11 12 13 14 |
# File 'lib/bookingsync/api/serializer.rb', line 10 def initialize(dump_method_name = nil, load_method_name = nil) @format = JSON @dump = @format.method(dump_method_name || :dump) @load = @format.method(load_method_name || :load) end |
Instance Method Details
#decode(data) ⇒ Object Also known as: load
Public: Decodes a String into an Object (usually a Hash or Array of Hashes).
data - An encoded String.
Returns a decoded Object.
33 34 35 36 |
# File 'lib/bookingsync/api/serializer.rb', line 33 def decode(data) return nil if data.nil? || data.empty? decode_object(@load.call(data)) end |
#decode_hash(hash) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/bookingsync/api/serializer.rb', line 67 def decode_hash(hash) hash.keys.each do |key| hash[key.to_sym] = decode_hash_value(key, hash.delete(key)) end hash end |
#decode_hash_value(key, value) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bookingsync/api/serializer.rb', line 74 def decode_hash_value(key, value) if time_field?(key, value) if value.is_a?(String) begin Time.parse(value) rescue ArgumentError value end elsif value.is_a?(Integer) || value.is_a?(Float) Time.at(value) else value end elsif value.is_a?(Hash) decode_hash(value) elsif value.is_a?(Array) value.map { |o| decode_hash_value(key, o) } else value end end |
#decode_object(data) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/bookingsync/api/serializer.rb', line 59 def decode_object(data) case data when Hash then decode_hash(data) when Array then data.map { |o| decode_object(o) } else data end end |
#encode(data) ⇒ Object Also known as: dump
Public: Encodes an Object (usually a Hash or Array of Hashes).
data - Object to be encoded.
Returns an encoded String.
21 22 23 |
# File 'lib/bookingsync/api/serializer.rb', line 21 def encode(data) @dump.call(encode_object(data)) end |
#encode_hash(hash) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bookingsync/api/serializer.rb', line 48 def encode_hash(hash) hash.keys.each do |key| case value = hash[key] when Date then hash[key] = value.to_time.utc.xmlschema when Time then hash[key] = value.utc.xmlschema when Hash then hash[key] = encode_hash(value) end end hash end |
#encode_object(data) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/bookingsync/api/serializer.rb', line 40 def encode_object(data) case data when Hash then encode_hash(data) when Array then data.map { |o| encode_object(o) } else data end end |
#time_field?(key, value) ⇒ Boolean
96 97 98 |
# File 'lib/bookingsync/api/serializer.rb', line 96 def time_field?(key, value) value && (key =~ /_(at|on)\z/ || key =~ /(\A|_)date\z/) end |