Skip to content

Commit

Permalink
renaming load/dump to parse/render
Browse files Browse the repository at this point in the history
  • Loading branch information
locks committed Dec 14, 2013
1 parent f30b07f commit 4895991
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
5 changes: 1 addition & 4 deletions lib/halibut/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ module Halibut::Adapter
# # => true
#
module JSON

# Returns an Halibut::HAL::Resource from a JSON string
def self.parse(json)
ResourceExtractor.new(json).resource
end

# Returns a JSON string representation of an Halibut::HAL::Resource
def self.dump(resource)
def self.render(resource)
MultiJson.dump resource.to_hash
end

Expand Down
4 changes: 2 additions & 2 deletions lib/halibut/adapter/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
module Halibut::Adapter

module XML
def self.load(xml)
def self.parse(xml)
ResourceExtractor.new(xml).resource
end

def self.dump(resource)
def self.render(resource)
raise NotImplementedError
end

Expand Down
4 changes: 2 additions & 2 deletions spec/adapter/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

it "serializes to JSON" do
resource = Halibut::Core::Resource.new Halibut::Core::Link.new("http://example.com")
subject = Halibut::Adapter::JSON.dump resource
subject = Halibut::Adapter::JSON.render resource
json = load_json "simple"

MultiJson.load(subject).must_equal MultiJson.load(json)
Expand All @@ -37,7 +37,7 @@

it "provides to_json helper" do
json = Halibut::Adapter::JSON.parse(load_json "serialize")
json = Halibut::Adapter::JSON.dump(json)
json = Halibut::Adapter::JSON.render(json)

order = Halibut::Core::Resource.new Halibut::Core::Link.new("/orders/123")
order.set_property "total", 30.00
Expand Down
49 changes: 49 additions & 0 deletions spec/adapter/xml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative '../spec_helper'


read_files = ->() {
Dir.tap {|it| it.chdir('spec/test-resources/src/main/resources') } \
.glob('*.xml') \
.map {|f| File.read f }
}


describe Halibut::Adapter::XML do

it "serializes to XML" do
skip "To Be Implemented"
end

it "deserializes from XML" do
builder = Halibut::Builder.new 'https://example.com/api/customer/123456' do
property 'age', "33"
property 'expired', "false"
property 'id', "123456"
property 'name', 'Example Resource'
property 'nullprop', ""
property 'optional', "true"

relation 'curie' do
link 'https://example.com/apidocs/accounts', name: 'ns'
link 'https://example.com/apidocs/roles', name: 'role'
end
link 'ns:parent', 'https://example.com/api/customer/1234', name: 'bob',
title: 'The Parent',
hreflang: 'en'
link 'ns:users', 'https://example.com/api/customer/123456?users'
end.resource

xml = load_resource('exampleWithNullProperty.xml')

deserialized = Halibut::Adapter::XML.parse(xml)
deserialized.must_equal builder, diff(deserialized.to_hash, builder.to_hash)
end

it "provides to_xml helper" do
skip "To Be Implemented"

xml = Halibut::Adapter::XML.parse(load_resource 'exampleWithNullProperty.xml')
xml = Halibut::Adapter::XML.render(xml)
end

end
8 changes: 8 additions & 0 deletions spec/core/relation_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
subject.must_be_empty
end

it "rejects nil items" do
subject.add 'first', 'first'
subject.add 'second', nil

subject['first'].size.must_equal 1
subject['first'].first.must_equal 'first'
end

it "has a single item per relation" do
subject.add 'first' , 'first'
subject.add 'second', 'second'
Expand Down

0 comments on commit 4895991

Please sign in to comment.