benjaminkrause-restful 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES.markdown +8 -0
- data/LICENSE.markdown +22 -0
- data/README.markdown +123 -0
- data/Rakefile +22 -0
- data/TODO.markdown +9 -0
- data/init.rb +1 -0
- data/lib/restful.rb +65 -0
- data/lib/restful/apimodel/attribute.rb +17 -0
- data/lib/restful/apimodel/collection.rb +22 -0
- data/lib/restful/apimodel/link.rb +21 -0
- data/lib/restful/apimodel/map.rb +41 -0
- data/lib/restful/apimodel/resource.rb +23 -0
- data/lib/restful/converters/active_record.rb +131 -0
- data/lib/restful/rails.rb +22 -0
- data/lib/restful/rails/action_controller.rb +14 -0
- data/lib/restful/rails/active_record/configuration.rb +167 -0
- data/lib/restful/rails/active_record/metadata_tools.rb +106 -0
- data/lib/restful/serializers/atom_like_serializer.rb +51 -0
- data/lib/restful/serializers/base.rb +57 -0
- data/lib/restful/serializers/hash_serializer.rb +59 -0
- data/lib/restful/serializers/json_serializer.rb +20 -0
- data/lib/restful/serializers/params_serializer.rb +46 -0
- data/lib/restful/serializers/xml_serializer.rb +161 -0
- data/rails/init.rb +1 -0
- data/restful.gemspec +17 -0
- data/test/converters/active_record_converter_test.rb +122 -0
- data/test/converters/basic_types_converter_test.rb +48 -0
- data/test/fixtures/models/paginated_collection.rb +4 -0
- data/test/fixtures/models/person.rb +29 -0
- data/test/fixtures/models/pet.rb +5 -0
- data/test/fixtures/models/wallet.rb +5 -0
- data/test/fixtures/people.json.yaml +94 -0
- data/test/fixtures/people.xml.yaml +123 -0
- data/test/fixtures/pets.json.yaml +20 -0
- data/test/fixtures/pets.xml.yaml +31 -0
- data/test/rails/active_record_metadata_test.rb +23 -0
- data/test/rails/configuration_test.rb +40 -0
- data/test/rails/restful_publish_test.rb +52 -0
- data/test/serializers/atom_serializer_test.rb +33 -0
- data/test/serializers/json_serializer_test.rb +82 -0
- data/test/serializers/params_serializer_test.rb +76 -0
- data/test/serializers/xml_serializer_test.rb +51 -0
- data/test/test_helper.rb +147 -0
- metadata +98 -0
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper.rb'
|
2
|
+
|
3
|
+
context "params serializer" do
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Person.restful_publish(:name, :current_location, :pets, :wallet, :created_at)
|
7
|
+
Pet.restful_publish(:name)
|
8
|
+
Wallet.restful_publish(:contents)
|
9
|
+
|
10
|
+
@person = Person.create(:name => "Joe Bloggs", :current_location => "Under a tree", :birthday => "1942-01-11")
|
11
|
+
@pet = @person.pets.create(:species => "cat", :age => 200, :name => "mietze")
|
12
|
+
@wallet = @person.wallet = Wallet.new(:contents => "an old photo, 5 euros in coins")
|
13
|
+
@person.save
|
14
|
+
end
|
15
|
+
|
16
|
+
teardown do
|
17
|
+
reset_config
|
18
|
+
end
|
19
|
+
|
20
|
+
specify "should be able to serialize objects empty collections" do
|
21
|
+
@person.pets = []
|
22
|
+
expected =
|
23
|
+
{
|
24
|
+
:name => "Joe Bloggs",
|
25
|
+
:current_location => "Under a tree",
|
26
|
+
:created_at => @person.created_at,
|
27
|
+
:wallet_attributes=>{:contents=>"an old photo, 5 euros in coins"}
|
28
|
+
}
|
29
|
+
|
30
|
+
assert_nothing_raised do
|
31
|
+
actual = @person.to_restful.serialize :params
|
32
|
+
actual.should.== expected
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
specify "should render correct date" do
|
37
|
+
actual = @person.to_restful(:birthday).serialize(:params)
|
38
|
+
|
39
|
+
expected = { :birthday => @person.birthday.to_s(:db) }
|
40
|
+
|
41
|
+
Person.create(expected).birthday.should.== @person.birthday
|
42
|
+
end
|
43
|
+
|
44
|
+
specify "serialize to params" do
|
45
|
+
actual = @person.to_restful.serialize(:params)
|
46
|
+
|
47
|
+
expected =
|
48
|
+
{
|
49
|
+
:name => "Joe Bloggs",
|
50
|
+
:current_location => "Under a tree",
|
51
|
+
:created_at => @person.created_at,
|
52
|
+
:wallet_attributes=>{:contents=>"an old photo, 5 euros in coins"},
|
53
|
+
:pets_attributes => [ {:name => "mietze"} ]
|
54
|
+
}
|
55
|
+
|
56
|
+
actual.should.== expected
|
57
|
+
end
|
58
|
+
|
59
|
+
specify "use correct key names for method that include '-' character" do
|
60
|
+
actual = @person.to_restful(:oldest_pet).serialize(:params)
|
61
|
+
|
62
|
+
expected = { :oldest_pet_attributes => {:name => "mietze"} }
|
63
|
+
|
64
|
+
actual.should.== expected
|
65
|
+
end
|
66
|
+
|
67
|
+
specify "serialize to an ar params hash" do
|
68
|
+
input = xml_fixture("pets")[:gracie]
|
69
|
+
params = Restful.from_xml(input).serialize(:params)
|
70
|
+
clone = Pet.create!(params)
|
71
|
+
|
72
|
+
clone.name.should.== "Gracie"
|
73
|
+
clone.species.should.== 123
|
74
|
+
clone.person_id.should.== @person.id
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper.rb'
|
2
|
+
|
3
|
+
context "params serializer" do
|
4
|
+
setup do
|
5
|
+
Person.restful_publish(:name, :current_location, :pets, :wallet, :created_at)
|
6
|
+
Pet.restful_publish(:name)
|
7
|
+
Wallet.restful_publish(:contents)
|
8
|
+
|
9
|
+
@person = Person.create(:name => "Joe Bloggs", :current_location => "Under a tree", :birthday => "1990-03-09")
|
10
|
+
@pet = @person.pets.create(:species => "cat", :age => 200, :name => "mietze")
|
11
|
+
@wallet = @person.wallet = Wallet.new(:contents => "an old photo, 5 euros in coins")
|
12
|
+
@person.save
|
13
|
+
end
|
14
|
+
|
15
|
+
teardown do
|
16
|
+
reset_config
|
17
|
+
end
|
18
|
+
|
19
|
+
specify "deserialize from rails style xml" do
|
20
|
+
restful = @person.to_restful
|
21
|
+
expected = restful.serialize(:xml)
|
22
|
+
serializer = Restful::Serializers::XMLSerializer.new
|
23
|
+
resource = serializer.deserialize(expected)
|
24
|
+
actual = serializer.serialize(resource)
|
25
|
+
|
26
|
+
xml_should_eql(expected, actual)
|
27
|
+
end
|
28
|
+
|
29
|
+
specify "should serialize date type correctly" do
|
30
|
+
xml_should_eql_fixture(@person.to_restful_xml(:birthday), "people", :joe_with_birthday)
|
31
|
+
end
|
32
|
+
|
33
|
+
specify "should convert a NULL inner association such as person.wallet to a link with a null value" do
|
34
|
+
@person.wallet = nil
|
35
|
+
|
36
|
+
xml_should_eql_fixture(@person.to_restful_xml(:restful_options => { :expansion => :collapsed }), "people", :verbose_with_pets)
|
37
|
+
end
|
38
|
+
|
39
|
+
specify "serialize to xml, rails style" do
|
40
|
+
xml_should_eql_fixture(@person.to_restful_xml, "people", :with_pets_and_expanded_wallet)
|
41
|
+
end
|
42
|
+
|
43
|
+
specify "should serialize hashes correctly" do
|
44
|
+
@person.pets.create(:species => "cat", :age => 100, :name => "motze")
|
45
|
+
xml_should_eql_fixture(@person.to_restful_xml(:pets_ages_hash), "people", :hashy_person)
|
46
|
+
end
|
47
|
+
|
48
|
+
specify "should serialize collections correctly" do
|
49
|
+
xml_should_eql_fixture(@person.pets.to_restful_xml, "pets", :pets_array)
|
50
|
+
end
|
51
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ruby-debug'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'test/spec'
|
7
|
+
require 'mocha'
|
8
|
+
require 'hpricot'
|
9
|
+
require 'xmlsimple'
|
10
|
+
require 'yajl'
|
11
|
+
require 'pathname'
|
12
|
+
require 'stringio'
|
13
|
+
|
14
|
+
plugin_test = Pathname.new(File.dirname(__FILE__))
|
15
|
+
plugin_root = plugin_test.join '..'
|
16
|
+
plugin_lib = plugin_root.join 'lib'
|
17
|
+
|
18
|
+
$:.unshift plugin_lib, plugin_test
|
19
|
+
|
20
|
+
RAILS_ENV = "test"
|
21
|
+
RAILS_ROOT = plugin_root # fake the rails root directory.
|
22
|
+
RESTFUL_ROOT = plugin_root
|
23
|
+
|
24
|
+
ActiveRecord::Base.logger = Logger.new(STDERR)
|
25
|
+
ActiveRecord::Base.logger.level = Logger::ERROR
|
26
|
+
ActiveRecord::Base.colorize_logging = false
|
27
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
28
|
+
|
29
|
+
silence_stream(STDOUT) do
|
30
|
+
ActiveRecord::Schema.define do
|
31
|
+
|
32
|
+
create_table :pets do |t|
|
33
|
+
t.integer :age, :default => 10
|
34
|
+
t.string :name
|
35
|
+
t.integer :species
|
36
|
+
t.integer :person_id
|
37
|
+
|
38
|
+
t.timestamp :created_at
|
39
|
+
t.timestamp :updated_at
|
40
|
+
end
|
41
|
+
|
42
|
+
create_table :people do |t|
|
43
|
+
t.string :name
|
44
|
+
t.string :current_location
|
45
|
+
t.string :biography
|
46
|
+
t.date :birthday
|
47
|
+
t.datetime :last_login
|
48
|
+
|
49
|
+
t.timestamp :created_at
|
50
|
+
t.timestamp :updated_at
|
51
|
+
end
|
52
|
+
|
53
|
+
create_table :wallets do |t|
|
54
|
+
t.string :person_id
|
55
|
+
t.string :contents
|
56
|
+
|
57
|
+
t.timestamp :created_at
|
58
|
+
t.timestamp :updated_at
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
require plugin_root.join 'init'
|
64
|
+
require 'fixtures/models/pet'
|
65
|
+
require 'fixtures/models/wallet'
|
66
|
+
require 'fixtures/models/person'
|
67
|
+
require 'fixtures/models/paginated_collection'
|
68
|
+
|
69
|
+
Restful::Rails.api_hostname = "http://example.com:3000"
|
70
|
+
|
71
|
+
#
|
72
|
+
# Helper methods
|
73
|
+
#
|
74
|
+
def reset_config
|
75
|
+
Person.restful_config = Restful.cfg
|
76
|
+
Pet.restful_config = Restful.cfg
|
77
|
+
Wallet.restful_config = Restful.cfg
|
78
|
+
end
|
79
|
+
|
80
|
+
def xml_cmp(actual, expected)
|
81
|
+
eq_all_but_zero = Object.new.instance_eval do
|
82
|
+
def ==(other)
|
83
|
+
Integer(other) == 0 ? false : true
|
84
|
+
end
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
88
|
+
actual = XmlSimple.xml_in(actual.to_s, 'normalisespace' => eq_all_but_zero)
|
89
|
+
expected = XmlSimple.xml_in(expected.to_s, 'normalisespace' => eq_all_but_zero)
|
90
|
+
actual == expected
|
91
|
+
end
|
92
|
+
|
93
|
+
def json_cmp(actual, expected)
|
94
|
+
actual = Yajl::Parser.parse(actual)
|
95
|
+
expected = Yajl::Parser.parse(expected)
|
96
|
+
puts_hash_diff actual, expected
|
97
|
+
actual == expected
|
98
|
+
end
|
99
|
+
|
100
|
+
def puts_hash_diff(hash1, hash2, indent = 0)
|
101
|
+
return if hash1 == hash2
|
102
|
+
|
103
|
+
((hash1 || {}).keys + (hash2 || {}).keys).uniq.each do |key|
|
104
|
+
next if hash1[key] == hash2[key]
|
105
|
+
print " "*indent
|
106
|
+
if hash1[key].is_a? Hash or hash2[key].is_a? Hash
|
107
|
+
puts "=== #{key} is a Hash ==="
|
108
|
+
puts_hash_diff(hash1[key] || {}, hash2[key] || {}, indent+2)
|
109
|
+
else
|
110
|
+
printf "%-#{20-indent}s %#{50-indent}s != %-50s\n", key[0..19], hash1[key], hash2[key]
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def xml_should_eql_fixture(actual, name, key)
|
116
|
+
expected = Hpricot(xml_fixture(name)[key])
|
117
|
+
actual = Hpricot(actual)
|
118
|
+
|
119
|
+
xml_should_eql(actual, expected)
|
120
|
+
end
|
121
|
+
|
122
|
+
# doing this tests that the content is the same regardless of attribute order etc.
|
123
|
+
def xml_should_eql(actual, expected)
|
124
|
+
same = xml_cmp(actual, expected)
|
125
|
+
actual.should.== expected unless same
|
126
|
+
end
|
127
|
+
|
128
|
+
def json_should_eql_fixture(actual, name, key)
|
129
|
+
expected = json_fixture(name)[key]
|
130
|
+
json_should_eql(actual, expected)
|
131
|
+
end
|
132
|
+
|
133
|
+
# doing this tests that the content is the same regardless of attribute order etc.
|
134
|
+
def json_should_eql(actual, expected)
|
135
|
+
same = json_cmp(actual, expected)
|
136
|
+
actual.should.== expected unless same
|
137
|
+
end
|
138
|
+
|
139
|
+
def file_fixture(name)
|
140
|
+
template = File.open(RESTFUL_ROOT.join("test", "fixtures", name)).read
|
141
|
+
fixture = ERB.new(template).result(binding)
|
142
|
+
yaml = YAML.load(fixture)
|
143
|
+
yaml.symbolize_keys
|
144
|
+
end
|
145
|
+
|
146
|
+
def xml_fixture(name); file_fixture("#{ name }.xml.yaml"); end
|
147
|
+
def json_fixture(name); file_fixture("#{ name }.json.yaml"); end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benjaminkrause-restful
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Bornkessel
|
8
|
+
- Rany Keddo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-08-11 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description:
|
18
|
+
email: M4SSIVE@m4ssive.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.markdown
|
25
|
+
files:
|
26
|
+
- CHANGES.markdown
|
27
|
+
- init.rb
|
28
|
+
- lib/restful/apimodel/attribute.rb
|
29
|
+
- lib/restful/apimodel/collection.rb
|
30
|
+
- lib/restful/apimodel/link.rb
|
31
|
+
- lib/restful/apimodel/map.rb
|
32
|
+
- lib/restful/apimodel/resource.rb
|
33
|
+
- lib/restful/converters/active_record.rb
|
34
|
+
- lib/restful/rails/action_controller.rb
|
35
|
+
- lib/restful/rails/active_record/configuration.rb
|
36
|
+
- lib/restful/rails/active_record/metadata_tools.rb
|
37
|
+
- lib/restful/rails.rb
|
38
|
+
- lib/restful/serializers/atom_like_serializer.rb
|
39
|
+
- lib/restful/serializers/base.rb
|
40
|
+
- lib/restful/serializers/hash_serializer.rb
|
41
|
+
- lib/restful/serializers/json_serializer.rb
|
42
|
+
- lib/restful/serializers/params_serializer.rb
|
43
|
+
- lib/restful/serializers/xml_serializer.rb
|
44
|
+
- lib/restful.rb
|
45
|
+
- LICENSE.markdown
|
46
|
+
- rails/init.rb
|
47
|
+
- Rakefile
|
48
|
+
- README.markdown
|
49
|
+
- restful.gemspec
|
50
|
+
- test/converters/active_record_converter_test.rb
|
51
|
+
- test/converters/basic_types_converter_test.rb
|
52
|
+
- test/fixtures/models/paginated_collection.rb
|
53
|
+
- test/fixtures/models/person.rb
|
54
|
+
- test/fixtures/models/pet.rb
|
55
|
+
- test/fixtures/models/wallet.rb
|
56
|
+
- test/fixtures/people.json.yaml
|
57
|
+
- test/fixtures/people.xml.yaml
|
58
|
+
- test/fixtures/pets.json.yaml
|
59
|
+
- test/fixtures/pets.xml.yaml
|
60
|
+
- test/rails/active_record_metadata_test.rb
|
61
|
+
- test/rails/configuration_test.rb
|
62
|
+
- test/rails/restful_publish_test.rb
|
63
|
+
- test/serializers/atom_serializer_test.rb
|
64
|
+
- test/serializers/json_serializer_test.rb
|
65
|
+
- test/serializers/params_serializer_test.rb
|
66
|
+
- test/serializers/xml_serializer_test.rb
|
67
|
+
- test/test_helper.rb
|
68
|
+
- TODO.markdown
|
69
|
+
has_rdoc: true
|
70
|
+
homepage: http://github.com/M4SSIVE/restful
|
71
|
+
licenses:
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options:
|
74
|
+
- --line-numbers
|
75
|
+
- --inline-source
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: "0"
|
83
|
+
version:
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: "0"
|
89
|
+
version:
|
90
|
+
requirements:
|
91
|
+
- brianmario-yajl-ruby
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.5
|
94
|
+
signing_key:
|
95
|
+
specification_version: 2
|
96
|
+
summary: api niceness.
|
97
|
+
test_files: []
|
98
|
+
|