roar 0.8.0 → 0.9.0
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 +18 -0
- data/Gemfile +1 -7
- data/README.textile +56 -65
- data/Rakefile +1 -1
- data/TODO.markdown +1 -0
- data/lib/roar/rails/representer_methods.rb +3 -2
- data/lib/roar/representer/feature/http_verbs.rb +29 -37
- data/lib/roar/representer/feature/hypermedia.rb +78 -13
- data/lib/roar/representer/feature/model_representing.rb +19 -27
- data/lib/roar/representer/feature/transport.rb +45 -0
- data/lib/roar/representer/json.rb +43 -14
- data/lib/roar/representer/xml.rb +44 -24
- data/lib/roar/representer.rb +31 -57
- data/lib/roar/version.rb +1 -1
- data/roar.gemspec +4 -6
- data/test/Gemfile +1 -0
- data/test/dummy/config/routes.rb +1 -1
- data/test/fake_server.rb +8 -11
- data/test/http_verbs_feature_test.rb +70 -0
- data/test/hypermedia_feature_test.rb +173 -0
- data/test/integration_test.rb +63 -100
- data/test/json_representer_test.rb +41 -35
- data/test/model_representing_test.rb +24 -26
- data/test/order_representers.rb +6 -3
- data/test/representer_test.rb +38 -4
- data/test/test_helper.rb +26 -8
- data/test/transport_test.rb +10 -13
- data/test/xml_representer_test.rb +81 -101
- metadata +20 -57
- data/lib/roar/client/entity_proxy.rb +0 -58
- data/lib/roar/client/proxy.rb +0 -14
- data/lib/roar/client/transport.rb +0 -29
- data/lib/roar/model/representable.rb +0 -31
- data/lib/roar/model.rb +0 -36
- data/test/http_verbs_test.rb +0 -46
- data/test/hypermedia_test.rb +0 -35
- data/test/model_test.rb +0 -50
- data/test/proxy_test.rb +0 -89
- data/test/representable_test.rb +0 -49
- data/test/ruby_representation_test.rb +0 -144
- data/test/test_helper_test.rb +0 -59
- data/test/xml_hypermedia_test.rb +0 -47
data/test/integration_test.rb
CHANGED
|
@@ -1,122 +1,85 @@
|
|
|
1
1
|
require 'test_helper'
|
|
2
|
-
|
|
3
|
-
require 'roar/representer/xml'
|
|
4
2
|
require 'roar/representer/json'
|
|
5
|
-
|
|
6
|
-
require 'roar/representer/feature/http_verbs'
|
|
7
3
|
require 'roar/representer/feature/hypermedia'
|
|
8
4
|
|
|
9
|
-
class
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
link :search do
|
|
21
|
-
search_url
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
link :self do
|
|
25
|
-
order_url(represented)
|
|
26
|
-
end
|
|
5
|
+
class IntegrationTest < MiniTest::Spec
|
|
6
|
+
class Beer
|
|
7
|
+
include Roar::Representer::JSON
|
|
8
|
+
include Roar::Representer::Feature::Hypermedia
|
|
9
|
+
|
|
10
|
+
property :name
|
|
11
|
+
|
|
12
|
+
link :self do
|
|
13
|
+
"http://beers/#{name.downcase}"
|
|
27
14
|
end
|
|
28
15
|
end
|
|
29
16
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
property :name
|
|
36
|
-
property :label
|
|
37
|
-
|
|
38
|
-
include Roar::Representer::Feature::HttpVerbs
|
|
39
|
-
include Roar::Representer::Feature::Hypermedia
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
link :search do
|
|
43
|
-
search_url
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
link :self do
|
|
47
|
-
order_url(represented)
|
|
48
|
-
end
|
|
49
|
-
end
|
|
17
|
+
class Beers
|
|
18
|
+
include Roar::Representer::JSON
|
|
19
|
+
include Roar::Representer::Feature::Hypermedia
|
|
20
|
+
|
|
21
|
+
collection :items, :class => Beer
|
|
50
22
|
end
|
|
51
23
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
@r = @r.post("http://localhost:9999/band", "application/band+json")
|
|
62
|
-
assert_equal "n/a", @r.label
|
|
63
|
-
|
|
64
|
-
# check HATEOAS:
|
|
65
|
-
#@r.extend Roar::Representer::Feature::Hypermedia
|
|
66
|
-
assert_equal "http://search", @r.links[:search]
|
|
67
|
-
assert_equal "http://band/strungout", @r.links[:self]
|
|
24
|
+
describe "Beer service" do
|
|
25
|
+
it "provides a document for a particular beer" do
|
|
26
|
+
assert_equal "{\"beer\":{\"name\":\"Eisenbahn\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/eisenbahn\"}]}}", Beer.from_attributes(name: "Eisenbahn").to_json
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "provides a detailed beers list" do
|
|
30
|
+
beers = ["Jever", "Becks", "Eisenbahn", "Colorado"].collect do |name|
|
|
31
|
+
Beer.from_attributes(name: name)
|
|
68
32
|
end
|
|
69
33
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
assert_equal "http://localhost:9999/orders/1", @o.links[:self]
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
# manually POST item:
|
|
85
|
-
@i = ::JSON::Item.new(:article_id => "666-S", :amount => 1)
|
|
86
|
-
@i.post!(@o.links[:items], "application/item+json")
|
|
87
|
-
@o.get!(@o.links[:self], "application/order+json")
|
|
34
|
+
|
|
35
|
+
list = Beers.new
|
|
36
|
+
list.items = beers
|
|
37
|
+
|
|
38
|
+
assert_equal "{\"beers\":{\"items\":[{\"name\":\"Jever\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/jever\"}]},{\"name\":\"Becks\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/becks\"}]},{\"name\":\"Eisenbahn\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/eisenbahn\"}]},{\"name\":\"Colorado\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/colorado\"}]}]}}", list.to_json
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "provides a pageable beers list without details" do
|
|
42
|
+
class BeerCollection
|
|
43
|
+
include Roar::Representer::JSON
|
|
44
|
+
include Roar::Representer::Feature::Hypermedia
|
|
88
45
|
|
|
89
|
-
|
|
90
|
-
assert_equal 1, @o.items.size
|
|
91
|
-
assert_equal @i.to_attributes, @o.items.first.to_attributes
|
|
46
|
+
attr_accessor :per_page, :current_page, :all_items
|
|
92
47
|
|
|
48
|
+
collection :beers, :class => Beer
|
|
49
|
+
property :total
|
|
93
50
|
|
|
94
|
-
|
|
51
|
+
def total
|
|
52
|
+
all_items.size
|
|
53
|
+
end
|
|
95
54
|
|
|
96
|
-
|
|
97
|
-
|
|
55
|
+
def beers
|
|
56
|
+
all_items[(current_page-1)*per_page..current_page*per_page-1]
|
|
57
|
+
end
|
|
98
58
|
|
|
99
|
-
|
|
59
|
+
link :next do
|
|
60
|
+
"http://beers/all?page=#{current_page+1}" if current_page < total / per_page
|
|
61
|
+
end
|
|
100
62
|
|
|
63
|
+
link :prev do
|
|
64
|
+
"http://beers/all?page=#{current_page-1}" if current_page > 1
|
|
65
|
+
end
|
|
101
66
|
end
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
it "allows a POST workflow" do
|
|
106
|
-
# create representation with initial values:
|
|
107
|
-
@r = XML::BandRepresenter.new(:name => "Bigwig")
|
|
108
|
-
assert_equal "Bigwig", @r.name
|
|
109
|
-
|
|
110
|
-
@r = @r.post("http://localhost:9999/band", "application/band+xml")
|
|
111
|
-
assert_equal "n/a", @r.label
|
|
112
|
-
|
|
113
|
-
# check HATEOAS:
|
|
114
|
-
#@r.extend Roar::Representer::Feature::Hypermedia
|
|
115
|
-
assert_equal "http://search", @r.links[:search]
|
|
116
|
-
assert_equal "http://band/strungout", @r.links[:self]
|
|
67
|
+
|
|
68
|
+
beers = ["Jever", "Becks", "Eisenbahn", "Colorado"].collect do |name|
|
|
69
|
+
Beer.from_attributes(name: name)
|
|
117
70
|
end
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
list = BeerCollection.new
|
|
74
|
+
list.all_items = beers # this would be a AR collection from a #find.
|
|
75
|
+
list.current_page = 1
|
|
76
|
+
list.per_page = 2
|
|
77
|
+
|
|
78
|
+
assert_equal "{\"beer_collection\":{\"beers\":[{\"name\":\"Jever\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/jever\"}]},{\"name\":\"Becks\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/becks\"}]}],\"total\":4,\"links\":[{\"rel\":\"next\",\"href\":\"http://beers/all?page=2\"},{\"rel\":\"prev\"}]}}", list.to_json
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
list.current_page = 2
|
|
82
|
+
assert_equal "{\"beer_collection\":{\"beers\":[{\"name\":\"Eisenbahn\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/eisenbahn\"}]},{\"name\":\"Colorado\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/colorado\"}]}],\"total\":4,\"links\":[{\"rel\":\"next\"},{\"rel\":\"prev\",\"href\":\"http://beers/all?page=1\"}]}}", list.to_json
|
|
118
83
|
end
|
|
119
|
-
|
|
120
|
-
|
|
121
84
|
end
|
|
122
85
|
end
|
|
@@ -3,50 +3,65 @@ require 'test_helper'
|
|
|
3
3
|
require "test_xml/mini_test"
|
|
4
4
|
require "roar/representer/json"
|
|
5
5
|
|
|
6
|
-
class
|
|
7
|
-
class
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
class JsonRepresenterTest < MiniTest::Spec
|
|
7
|
+
class Order
|
|
8
|
+
include Roar::Representer::JSON
|
|
9
|
+
property :id
|
|
10
|
+
property :pending
|
|
10
11
|
end
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
describe "JsonRepresenter" do
|
|
14
15
|
before do
|
|
15
|
-
@
|
|
16
|
+
@order = Order.new
|
|
16
17
|
end
|
|
17
18
|
|
|
19
|
+
|
|
18
20
|
describe "#to_json" do
|
|
19
|
-
|
|
20
|
-
@
|
|
21
|
-
assert_equal '{"order":{"id":1}}', @r.serialize
|
|
21
|
+
before do
|
|
22
|
+
@order.id = 1
|
|
22
23
|
end
|
|
23
24
|
|
|
25
|
+
it "returns the serialized model" do
|
|
26
|
+
assert_equal '{"id":1}', @order.to_json
|
|
27
|
+
end
|
|
24
28
|
|
|
25
|
-
it "
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
it "is aliased by #serialize" do
|
|
30
|
+
assert_equal '{"id":1}', @order.serialize
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe "#from_json" do
|
|
35
|
+
it "returns the deserialized model" do
|
|
36
|
+
@order.from_json('{"id":1}')
|
|
37
|
+
assert_equal 1, @order.id
|
|
28
38
|
end
|
|
29
39
|
|
|
30
|
-
it "
|
|
31
|
-
|
|
40
|
+
it "is aliased by #deserialize" do
|
|
41
|
+
@order.deserialize('{"id":1}')
|
|
42
|
+
assert_equal 1, @order.id
|
|
32
43
|
end
|
|
33
44
|
|
|
45
|
+
it "works with a nil document" do
|
|
46
|
+
assert @order.from_json(nil)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe "JSON.from_json" do
|
|
51
|
+
it "is aliased by #deserialize" do
|
|
52
|
+
@order = Order.deserialize('{"id":1}')
|
|
53
|
+
assert_equal 1, @order.id
|
|
54
|
+
end
|
|
34
55
|
end
|
|
35
56
|
end
|
|
36
|
-
|
|
37
57
|
end
|
|
38
58
|
|
|
39
59
|
class JsonHyperlinkRepresenterTest
|
|
40
60
|
describe "API" do
|
|
41
61
|
before do
|
|
42
|
-
@l = Roar::Representer::JSON::Hyperlink.from_json({:
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
it "responds to #representation_name" do
|
|
46
|
-
assert_equal :link, @l.class.representation_name
|
|
62
|
+
@l = Roar::Representer::JSON::Hyperlink.from_json({:rel => :self, :href => "http://roar.apotomo.de"}.to_json)
|
|
47
63
|
end
|
|
48
64
|
|
|
49
|
-
|
|
50
65
|
it "responds to #rel" do
|
|
51
66
|
assert_equal "self", @l.rel
|
|
52
67
|
end
|
|
@@ -60,12 +75,11 @@ end
|
|
|
60
75
|
class JsonHypermediaTest
|
|
61
76
|
describe "Hypermedia API" do
|
|
62
77
|
before do
|
|
63
|
-
@c = Class.new
|
|
78
|
+
@c = Class.new do
|
|
79
|
+
include Roar::Representer::JSON
|
|
64
80
|
include Roar::Representer::Feature::Hypermedia
|
|
65
81
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
representable_property :id
|
|
82
|
+
property :id
|
|
69
83
|
|
|
70
84
|
link :self do "http://self" end
|
|
71
85
|
link :next do "http://next/#{id}" end
|
|
@@ -75,27 +89,19 @@ class JsonHypermediaTest
|
|
|
75
89
|
end
|
|
76
90
|
|
|
77
91
|
it "responds to #links" do
|
|
78
|
-
assert_equal
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
it "computes links in #from_attributes" do
|
|
82
|
-
@r = @c.from_attributes({"id" => 1})
|
|
83
|
-
assert_equal 2, @r.links.size
|
|
84
|
-
assert_equal({"rel"=>:self, "href"=>"http://self"}, @r.links.first.to_attributes)
|
|
85
|
-
assert_equal({"rel"=>:next, "href"=>"http://next/1"}, @r.links.last.to_attributes)
|
|
92
|
+
assert_equal [], @r.links
|
|
86
93
|
end
|
|
87
94
|
|
|
88
95
|
it "extracts links from JSON" do
|
|
89
|
-
@r = @c.
|
|
96
|
+
@r = @c.from_json({:links => [{:rel => "self", :href => "http://self"}]}.to_json)
|
|
90
97
|
|
|
91
98
|
assert_equal 1, @r.links.size
|
|
92
99
|
assert_equal({"rel"=>"self", "href"=>"http://self"}, @r.links.first.to_attributes)
|
|
93
100
|
end
|
|
94
101
|
|
|
95
102
|
it "renders link: correctly in JSON" do
|
|
96
|
-
assert_equal "{\"
|
|
103
|
+
assert_equal "{\"id\":1,\"links\":[{\"rel\":\"self\",\"href\":\"http://self\"},{\"rel\":\"next\",\"href\":\"http://next/1\"}]}", @c.from_attributes(:id => 1).to_json
|
|
97
104
|
end
|
|
98
|
-
|
|
99
105
|
end
|
|
100
106
|
end
|
|
101
107
|
|
|
@@ -3,24 +3,27 @@ require 'roar/representer/feature/model_representing'
|
|
|
3
3
|
|
|
4
4
|
class ModelRepresentingTest < MiniTest::Spec
|
|
5
5
|
describe "ModelRepresenting" do
|
|
6
|
-
class ItemRepresenter
|
|
7
|
-
include Roar::Representer::
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
class ItemRepresenter
|
|
7
|
+
include Roar::Representer::XML
|
|
8
|
+
include Roar::Representer::Feature::ModelRepresenting
|
|
9
|
+
self.representation_wrap= :item
|
|
10
|
+
property :value
|
|
10
11
|
end
|
|
11
12
|
|
|
12
|
-
class PositionRepresenter
|
|
13
|
-
include Roar::Representer::
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
class PositionRepresenter
|
|
14
|
+
include Roar::Representer::XML
|
|
15
|
+
include Roar::Representer::Feature::ModelRepresenting
|
|
16
|
+
self.representation_wrap= :position
|
|
17
|
+
property :id
|
|
18
|
+
property :item, :class => ItemRepresenter
|
|
17
19
|
end
|
|
18
20
|
|
|
19
|
-
class OrderRepresenter
|
|
20
|
-
include Roar::Representer::
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
class OrderRepresenter
|
|
22
|
+
include Roar::Representer::XML
|
|
23
|
+
include Roar::Representer::Feature::ModelRepresenting
|
|
24
|
+
self.representation_wrap= :order
|
|
25
|
+
property :id
|
|
26
|
+
collection :items, :class => ItemRepresenter
|
|
24
27
|
end
|
|
25
28
|
|
|
26
29
|
describe "#definition_class" do
|
|
@@ -33,7 +36,6 @@ class ModelRepresentingTest < MiniTest::Spec
|
|
|
33
36
|
describe "#for_model" do
|
|
34
37
|
it "copies represented model attributes, nothing more" do
|
|
35
38
|
@o = Position.new("id" => 1, "item" => Item.new("value" => "Beer"))
|
|
36
|
-
|
|
37
39
|
@r = PositionRepresenter.for_model(@o)
|
|
38
40
|
assert_kind_of PositionRepresenter, @r
|
|
39
41
|
assert_equal 1, @r.id
|
|
@@ -43,23 +45,13 @@ class ModelRepresentingTest < MiniTest::Spec
|
|
|
43
45
|
assert_equal "Beer", @i.value
|
|
44
46
|
end
|
|
45
47
|
|
|
46
|
-
it "
|
|
48
|
+
it "references the model in @represented" do
|
|
47
49
|
@o = Position.new("id" => 1, "item" => @i = Item.new("value" => "Beer"))
|
|
48
50
|
|
|
49
51
|
@r = PositionRepresenter.for_model(@o)
|
|
50
52
|
assert_equal @o, @r.represented
|
|
51
53
|
assert_equal @i, @r.item.represented
|
|
52
54
|
end
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
it "works with Hyperlink attributes" do
|
|
56
|
-
@c = Class.new(ItemRepresenter) do
|
|
57
|
-
link :self do "http://self" end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
assert_equal({"value"=>"Beer", "links"=>[{"rel"=>:self, "href"=>"http://self"}]}, @c.for_model(Item.new("value" => "Beer")).to_attributes)
|
|
61
|
-
end
|
|
62
|
-
|
|
63
55
|
end
|
|
64
56
|
|
|
65
57
|
describe "#serialize_model" do
|
|
@@ -93,6 +85,9 @@ class ModelRepresentingTest < MiniTest::Spec
|
|
|
93
85
|
OrderRepresenter.class_eval do
|
|
94
86
|
include Roar::Representer::Feature::ActiveRecordMethods
|
|
95
87
|
end
|
|
88
|
+
ItemRepresenter.class_eval do
|
|
89
|
+
include Roar::Representer::Feature::ActiveRecordMethods
|
|
90
|
+
end
|
|
96
91
|
assert_equal({"id" => 1, "items_attributes" => [{"value" => "Beer"}]}, @r.to_nested_attributes) # DISCUSS: overwrite #to_attributes.
|
|
97
92
|
end
|
|
98
93
|
|
|
@@ -103,11 +98,14 @@ class ModelRepresentingTest < MiniTest::Spec
|
|
|
103
98
|
|
|
104
99
|
OrderRepresenter.class_eval do
|
|
105
100
|
include Roar::Representer::Feature::ActiveRecordMethods
|
|
101
|
+
include Roar::Representer::Feature::Hypermedia
|
|
106
102
|
link :self do
|
|
107
103
|
# "bla"
|
|
108
104
|
end
|
|
109
105
|
end
|
|
110
106
|
ItemRepresenter.class_eval do
|
|
107
|
+
include Roar::Representer::Feature::ActiveRecordMethods
|
|
108
|
+
include Roar::Representer::Feature::Hypermedia
|
|
111
109
|
link :self do
|
|
112
110
|
|
|
113
111
|
end
|
data/test/order_representers.rb
CHANGED
|
@@ -4,7 +4,9 @@ require 'roar/representer/feature/hypermedia'
|
|
|
4
4
|
|
|
5
5
|
module JSON
|
|
6
6
|
|
|
7
|
-
class Item
|
|
7
|
+
class Item
|
|
8
|
+
include Roar::Representer::JSON
|
|
9
|
+
|
|
8
10
|
property :article_id
|
|
9
11
|
property :amount
|
|
10
12
|
|
|
@@ -13,9 +15,10 @@ module JSON
|
|
|
13
15
|
end
|
|
14
16
|
|
|
15
17
|
|
|
16
|
-
class Order
|
|
18
|
+
class Order
|
|
19
|
+
include Roar::Representer::JSON
|
|
17
20
|
property :client_id
|
|
18
|
-
collection :items, :
|
|
21
|
+
collection :items, :class => Item
|
|
19
22
|
|
|
20
23
|
|
|
21
24
|
include Roar::Representer::Feature::HttpVerbs
|
data/test/representer_test.rb
CHANGED
|
@@ -3,7 +3,9 @@ require 'test_helper'
|
|
|
3
3
|
class RepresenterTest < MiniTest::Spec
|
|
4
4
|
describe "Representer" do
|
|
5
5
|
before do
|
|
6
|
-
@c = Class.new
|
|
6
|
+
@c = Class.new do
|
|
7
|
+
include Roar::Representer
|
|
8
|
+
end
|
|
7
9
|
end
|
|
8
10
|
|
|
9
11
|
it "aliases #representable_property to #property" do
|
|
@@ -16,9 +18,41 @@ class RepresenterTest < MiniTest::Spec
|
|
|
16
18
|
assert_equal "songs", @c.representable_attrs.first.name
|
|
17
19
|
end
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
|
|
22
|
+
describe "#from_attributes" do
|
|
23
|
+
it "accepts a block yielding the created representer instance" do
|
|
24
|
+
@c.class_eval { attr_accessor :name }
|
|
25
|
+
|
|
26
|
+
assert_equal("Conan", @c.from_attributes({}) { |rep| rep.name = "Conan" }.name)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "copies known properties, only, but doesn't complain" do
|
|
30
|
+
@c.class_eval { property :id }
|
|
31
|
+
|
|
32
|
+
assert_equal 1, @c.from_attributes("id" => 1, "unknown" => "don't use me").id
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "accepts symbols and strings as property name" do
|
|
36
|
+
@c.class_eval { property :id }
|
|
37
|
+
|
|
38
|
+
assert_equal @c.from_attributes(:id => 1).id, @c.from_attributes("id" => 1).id
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
describe "Inheritance" do
|
|
44
|
+
it "properly inherits properties from modules" do
|
|
45
|
+
module PersonRepresentation
|
|
46
|
+
include Roar::Representer::JSON
|
|
47
|
+
property :name
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class Person
|
|
51
|
+
include Roar::Representer::JSON
|
|
52
|
+
include PersonRepresentation
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
assert_equal "{\"name\":\"Paulo\"}", Person.from_attributes(:name => "Paulo").to_json
|
|
22
56
|
end
|
|
23
57
|
|
|
24
58
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -4,18 +4,36 @@ Bundler.setup
|
|
|
4
4
|
require 'test/unit'
|
|
5
5
|
require 'minitest/spec'
|
|
6
6
|
|
|
7
|
-
require 'active_model'
|
|
8
|
-
#require 'roar/client/entity_proxy'
|
|
9
7
|
require 'roar/representer'
|
|
10
|
-
require 'roar/
|
|
8
|
+
require 'roar/representer/feature/hypermedia'
|
|
11
9
|
require 'roar/representer/feature/http_verbs'
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
module TestModel
|
|
12
|
+
def self.included(base)
|
|
13
|
+
base.extend ClassMethods
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
module ClassMethods
|
|
18
|
+
def accessors(*names)
|
|
19
|
+
names.each do |name|
|
|
20
|
+
attr_accessor name
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
attr_accessor :attributes
|
|
26
|
+
|
|
27
|
+
def initialize(attributes={})
|
|
28
|
+
attributes.each do |k,v|
|
|
29
|
+
send("#{k}=", v)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
15
33
|
|
|
16
34
|
|
|
17
35
|
class Item
|
|
18
|
-
include
|
|
36
|
+
include TestModel
|
|
19
37
|
accessors :value
|
|
20
38
|
|
|
21
39
|
def self.model_name
|
|
@@ -24,7 +42,7 @@ class Item
|
|
|
24
42
|
end
|
|
25
43
|
|
|
26
44
|
class Position
|
|
27
|
-
include
|
|
45
|
+
include TestModel
|
|
28
46
|
accessors :id, :item
|
|
29
47
|
|
|
30
48
|
def self.model_name
|
|
@@ -33,7 +51,7 @@ class Position
|
|
|
33
51
|
end
|
|
34
52
|
|
|
35
53
|
class Order
|
|
36
|
-
include
|
|
54
|
+
include TestModel
|
|
37
55
|
accessors :id, :items
|
|
38
56
|
|
|
39
57
|
def self.model_name
|
data/test/transport_test.rb
CHANGED
|
@@ -1,29 +1,26 @@
|
|
|
1
1
|
require 'test_helper'
|
|
2
|
-
require 'roar/
|
|
2
|
+
require 'roar/representer/feature/transport'
|
|
3
3
|
|
|
4
4
|
class TransportTest < MiniTest::Spec
|
|
5
5
|
describe "Transport" do
|
|
6
6
|
before do
|
|
7
|
-
@
|
|
8
|
-
include Roar::Client::Transport
|
|
9
|
-
end
|
|
10
|
-
@o = @klass.new
|
|
7
|
+
@transport = Roar::Representer::Feature::Transport
|
|
11
8
|
end
|
|
12
9
|
|
|
13
|
-
it "#get_uri returns
|
|
14
|
-
assert_equal "<method>get</method>", @
|
|
10
|
+
it "#get_uri returns response" do
|
|
11
|
+
assert_equal "<method>get</method>", @transport.get_uri("http://localhost:9999/method", "application/xml").body
|
|
15
12
|
end
|
|
16
13
|
|
|
17
|
-
it "#post_uri returns
|
|
18
|
-
assert_equal "<method>post</method>", @
|
|
14
|
+
it "#post_uri returns response" do
|
|
15
|
+
assert_equal "<method>post</method>", @transport.post_uri("http://localhost:9999/method", "booty", "application/xml").body
|
|
19
16
|
end
|
|
20
17
|
|
|
21
|
-
it "#put_uri returns
|
|
22
|
-
assert_equal "<method>put</method>", @
|
|
18
|
+
it "#put_uri returns response" do
|
|
19
|
+
assert_equal "<method>put</method>", @transport.put_uri("http://localhost:9999/method", "booty", "application/xml").body
|
|
23
20
|
end
|
|
24
21
|
|
|
25
|
-
it "#delete_uri returns
|
|
26
|
-
assert_equal "<method>delete</method>", @
|
|
22
|
+
it "#delete_uri returns response" do
|
|
23
|
+
assert_equal "<method>delete</method>", @transport.delete_uri("http://localhost:9999/method", "application/xml").body
|
|
27
24
|
end
|
|
28
25
|
|
|
29
26
|
# TODO: how to get PATCH into Sinatra?
|