Skip to content

Commit

Permalink
serializer bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Sep 28, 2012
1 parent 2c93eb5 commit 8d93e7d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/sawyer/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize(endpoint, options = nil)
#
# Returns a Sawyer::Relation::Map.
def rels
@rels ||= root.data.rels
@rels ||= root.data._rels
end

# Public: Retains a reference to the root response of the API.
Expand Down
14 changes: 8 additions & 6 deletions lib/sawyer/resource.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Sawyer
class Resource
SPECIAL_METHODS = Set.new %w(agent rels fields)
SPECIAL_METHODS = Set.new(%w(agent rels fields))
attr_reader :_agent, :_rels, :_fields

# Initializes a Resource with the given data.
Expand All @@ -9,12 +9,14 @@ class Resource
# data - Hash of key/value properties.
def initialize(agent, data)
@_agent = agent
@_rels = Relation.from_links(agent, data.delete(:_links))
@_fields = Set.new []
@_rels = Relation.from_links(agent, data.delete(:_links))
@_fields = Set.new
@_metaclass = (class << self; self; end)
data.each do |key, value|
@_fields << key
instance_variable_set "@#{key}", process_value(value)
end
@_metaclass.send(:attr_accessor, *data.keys)
end

# Processes an individual value of this resource. Hashes get exploded
Expand Down Expand Up @@ -47,14 +49,14 @@ def key?(key)
def method_missing(method, *args)
attr_name, suffix = method.to_s.scan(/([a-z0-9\_]+)(\?|\=)?$/i).first
if suffix == ATTR_SETTER
(class << self; self; end).send :attr_accessor, attr_name
@_metaclass.send(:attr_accessor, attr_name)
@_fields << attr_name.to_sym
instance_variable_set "@#{attr_name}", args.first
instance_variable_set("@#{attr_name}", args.first)
elsif @_fields.include?(attr_name.to_sym)
value = instance_variable_get("@#{attr_name}")
case suffix
when nil
(class << self; self; end).send :attr_accessor, attr_name
@_metaclass.send(:attr_accessor, attr_name)
value
when ATTR_PREDICATE then !!value
end
Expand Down
9 changes: 6 additions & 3 deletions lib/sawyer/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def decode(data)
def encode_object(data)
case data
when Hash then encode_hash(data)
when Array then data.map { |o| encode_object(data) }
when Array then data.map { |o| encode_object(o) }
else data
end
end
Expand All @@ -73,6 +73,7 @@ def encode_hash(hash)
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
Expand All @@ -81,7 +82,7 @@ def encode_hash(hash)
def decode_object(data)
case data
when Hash then decode_hash(data)
when Array then data.map { |o| decode_object(data) }
when Array then data.map { |o| decode_object(o) }
else data
end
end
Expand All @@ -94,10 +95,12 @@ def decode_hash(hash)
end

def decode_hash_value(key, value)
if key =~ /^_(at|on)$/
if key =~ /_(at|on)$/
Time.parse(value)
elsif value.is_a?(Hash)
decode_hash(value)
elsif value.is_a?(Array)
value.map { |o| decode_hash_value(key, o) }
else
value
end
Expand Down
19 changes: 19 additions & 0 deletions test/agent_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ def test_requests_with_body_and_options_to_get
:query => {:foo => 'bar'}
assert_equal 200, res.status
end

def test_encodes_and_decodes_times
time = Time.at(Time.now.to_i)
data = {:a => 1, :b => true, :c => 'c', :created_at => time}
data = [data.merge(:foo => [data])]
encoded = Sawyer::Agent.encode(data)
decoded = Sawyer::Agent.decode(encoded)

2.times do
assert_equal 1, decoded.size
decoded = decoded.shift

assert_equal 1, decoded[:a]
assert_equal true, decoded[:b]
assert_equal 'c', decoded[:c]
assert_equal time, decoded[:created_at]
decoded = decoded[:foo]
end
end
end
end

6 changes: 3 additions & 3 deletions test/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def test_attribute_setter

def test_dynamic_attribute_methods_from_getter
res = Resource.new :agent, :a => 1
assert res.key?(:a)
assert !res.respond_to?(:a)
assert !res.respond_to?(:a=)
assert res.key?(:a)
assert res.respond_to?(:a)
assert res.respond_to?(:a=)

assert_equal 1, res.a
assert res.respond_to?(:a)
Expand Down

0 comments on commit 8d93e7d

Please sign in to comment.