roar 0.11.4 → 0.11.5
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 +5 -1
- data/Gemfile +1 -1
- data/README.textile +22 -21
- data/lib/roar/representer.rb +1 -1
- data/lib/roar/representer/feature/client.rb +3 -3
- data/lib/roar/representer/feature/coercion.rb +1 -1
- data/lib/roar/representer/feature/http_verbs.rb +8 -9
- data/lib/roar/representer/feature/hypermedia.rb +83 -63
- data/lib/roar/representer/json.rb +12 -12
- data/lib/roar/representer/json/hal.rb +98 -25
- data/lib/roar/representer/transport/faraday.rb +5 -11
- data/lib/roar/representer/transport/net_http.rb +5 -5
- data/lib/roar/representer/xml.rb +11 -11
- data/lib/roar/version.rb +1 -1
- data/roar.gemspec +2 -2
- data/test/client_test.rb +2 -2
- data/test/coercion_feature_test.rb +2 -2
- data/test/dummy/app/controllers/albums_controller.rb +6 -6
- data/test/dummy/app/models/album.rb +1 -1
- data/test/dummy/app/representers/representer/xml/album.rb +3 -3
- data/test/dummy/app/representers/representer/xml/song.rb +1 -1
- data/test/dummy/config/boot.rb +1 -1
- data/test/fake_server.rb +14 -15
- data/test/faraday_http_transport_test.rb +25 -10
- data/test/hal_json_test.rb +73 -31
- data/test/http_verbs_feature_test.rb +12 -12
- data/test/hypermedia_feature_test.rb +38 -145
- data/test/hypermedia_test.rb +100 -0
- data/test/integration_test.rb +22 -22
- data/test/json_representer_test.rb +31 -31
- data/test/net_http_transport_test.rb +24 -10
- data/test/order_representers.rb +11 -11
- data/test/rails/controller_methods_test.rb +25 -25
- data/test/rails/rails_representer_methods_test.rb +3 -3
- data/test/representer_test.rb +6 -6
- data/test/test_helper.rb +33 -4
- data/test/xml_representer_test.rb +47 -47
- metadata +3 -3
- data/lib/roar/rails.rb +0 -21
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class HypermediaTest < MiniTest::Spec
|
4
|
+
describe "#links_array" do
|
5
|
+
subject { Object.new.extend(rpr) }
|
6
|
+
|
7
|
+
representer_for do
|
8
|
+
link(:self) { "//self" }
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
describe "#to_json" do
|
13
|
+
it "renders" do
|
14
|
+
subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\"}]}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#from_json" do
|
19
|
+
it "parses" do
|
20
|
+
subject.from_json "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\"}]}"
|
21
|
+
subject.links.must_equal({"self" => link(:rel => :self, :href => "//self")})
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
describe "#link" do
|
27
|
+
|
28
|
+
describe "with any options" do
|
29
|
+
representer_for do
|
30
|
+
link(:rel => :self, :title => "Hey, @myabc") { "//self" }
|
31
|
+
end
|
32
|
+
|
33
|
+
it "renders options" do
|
34
|
+
subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"title\":\"Hey, @myabc\",\"href\":\"//self\"}]}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "passing options to serialize" do
|
39
|
+
representer_for do
|
40
|
+
link(:self) { |opts| "//self/#{opts[:id]}" }
|
41
|
+
end
|
42
|
+
|
43
|
+
it "receives options when rendering" do
|
44
|
+
subject.to_json(:id => 1).must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self/1\"}]}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "returning option hash from block" do
|
49
|
+
representer_for do
|
50
|
+
link(:self) do {:href => "//self", :type => "image/jpg"} end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "is rendered as link attributes" do
|
54
|
+
subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\",\"type\":\"image/jpg\"}]}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# private tests:
|
60
|
+
|
61
|
+
it "returns array of links for rendering" do
|
62
|
+
subject.send :prepare_links!
|
63
|
+
subject.links_array.must_equal [link(:rel => :self, :href => "//self")]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "#links_array maps array to LinkCollection hash" do
|
67
|
+
subject.links_array= [link(:rel => :self, :href => "//self")]
|
68
|
+
subject.links.must_equal({"self" => link(:rel => :self, :href => "//self")})
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class LinksDefinitionTest < MiniTest::Spec
|
74
|
+
describe "LinksDefinition" do
|
75
|
+
subject { Roar::Representer::Feature::Hypermedia::LinksDefinition.new(:links) }
|
76
|
+
|
77
|
+
it "responds to #<<" do
|
78
|
+
subject << "arbitrary bullshit"
|
79
|
+
subject.to_a.must_equal ["arbitrary bullshit"]
|
80
|
+
end
|
81
|
+
|
82
|
+
it "responds to #each" do
|
83
|
+
subject.to_a.must_equal []
|
84
|
+
end
|
85
|
+
|
86
|
+
it "accepts options in constructor" do
|
87
|
+
assert_equal [], subject.rel2block
|
88
|
+
end
|
89
|
+
|
90
|
+
it "accepts configuration" do
|
91
|
+
subject.rel2block << {:rel => :self}
|
92
|
+
assert_equal [{:rel=>:self}], subject.rel2block
|
93
|
+
end
|
94
|
+
|
95
|
+
it "responds to #clone" do
|
96
|
+
subject.rel2block << {:rel => :self}
|
97
|
+
assert subject.clone.rel2block.object_id != subject.rel2block.object_id
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/test/integration_test.rb
CHANGED
@@ -6,78 +6,78 @@ class IntegrationTest < MiniTest::Spec
|
|
6
6
|
class Beer
|
7
7
|
include Roar::Representer::JSON
|
8
8
|
include Roar::Representer::Feature::Hypermedia
|
9
|
-
|
9
|
+
|
10
10
|
property :name
|
11
|
-
|
11
|
+
|
12
12
|
link :self do
|
13
13
|
"http://beers/#{name.downcase}"
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
class Beers
|
18
18
|
include Roar::Representer::JSON
|
19
19
|
include Roar::Representer::Feature::Hypermedia
|
20
|
-
|
20
|
+
|
21
21
|
collection :items, :class => Beer
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
describe "Beer service" do
|
25
25
|
it "provides a document for a particular beer" do
|
26
26
|
assert_equal "{\"beer\":{\"name\":\"Eisenbahn\",\"links\":[{\"rel\":\"self\",\"href\":\"http://beers/eisenbahn\"}]}}", Beer.from_attributes(name: "Eisenbahn").to_json
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
it "provides a detailed beers list" do
|
30
30
|
beers = ["Jever", "Becks", "Eisenbahn", "Colorado"].collect do |name|
|
31
31
|
Beer.from_attributes(name: name)
|
32
32
|
end
|
33
|
-
|
34
|
-
|
33
|
+
|
34
|
+
|
35
35
|
list = Beers.new
|
36
36
|
list.items = beers
|
37
|
-
|
37
|
+
|
38
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
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
it "provides a pageable beers list without details" do
|
42
42
|
class BeerCollection
|
43
43
|
include Roar::Representer::JSON
|
44
44
|
include Roar::Representer::Feature::Hypermedia
|
45
|
-
|
45
|
+
|
46
46
|
attr_accessor :per_page, :current_page, :all_items
|
47
|
-
|
47
|
+
|
48
48
|
collection :beers, :class => Beer
|
49
49
|
property :total
|
50
|
-
|
50
|
+
|
51
51
|
def total
|
52
52
|
all_items.size
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
def beers
|
56
56
|
all_items[(current_page-1)*per_page..current_page*per_page-1]
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
link :next do
|
60
60
|
"http://beers/all?page=#{current_page+1}" if current_page < total / per_page
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
link :prev do
|
64
64
|
"http://beers/all?page=#{current_page-1}" if current_page > 1
|
65
65
|
end
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
beers = ["Jever", "Becks", "Eisenbahn", "Colorado"].collect do |name|
|
69
69
|
Beer.from_attributes(name: name)
|
70
70
|
end
|
71
|
-
|
72
|
-
|
71
|
+
|
72
|
+
|
73
73
|
list = BeerCollection.new
|
74
74
|
list.all_items = beers # this would be a AR collection from a #find.
|
75
75
|
list.current_page = 1
|
76
76
|
list.per_page = 2
|
77
|
-
|
77
|
+
|
78
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
|
-
|
79
|
+
|
80
|
+
|
81
81
|
list.current_page = 2
|
82
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
|
83
83
|
end
|
@@ -10,43 +10,43 @@ class JsonRepresenterTest < MiniTest::Spec
|
|
10
10
|
property :pending
|
11
11
|
attr_accessor :id, :pending
|
12
12
|
end
|
13
|
-
|
14
|
-
|
13
|
+
|
14
|
+
|
15
15
|
describe "JsonRepresenter" do
|
16
16
|
before do
|
17
17
|
@order = Order.new
|
18
18
|
end
|
19
|
-
|
20
|
-
|
19
|
+
|
20
|
+
|
21
21
|
describe "#to_json" do
|
22
22
|
before do
|
23
23
|
@order.id = 1
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
it "returns the serialized model" do
|
27
27
|
assert_equal '{"id":1}', @order.to_json
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
it "is aliased by #serialize" do
|
31
31
|
assert_equal '{"id":1}', @order.serialize
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it "accepts :include and :exclude" do
|
35
35
|
assert_equal '{}', @order.to_json(:exclude => [:id])
|
36
36
|
end
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
describe "#from_json" do
|
40
40
|
it "returns the deserialized model" do
|
41
41
|
@order.from_json('{"id":1}')
|
42
42
|
assert_equal 1, @order.id
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
it "is aliased by #deserialize" do
|
46
46
|
@order.deserialize('{"id":1}')
|
47
47
|
assert_equal 1, @order.id
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
it "works with a nil document" do
|
51
51
|
assert @order.from_json(nil)
|
52
52
|
end
|
@@ -54,13 +54,13 @@ class JsonRepresenterTest < MiniTest::Spec
|
|
54
54
|
it "works with an empty document" do
|
55
55
|
assert @order.from_json('')
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
it "accepts :include and :exclude" do
|
59
59
|
@order.from_json('{"id":1}', :exclude => [:id])
|
60
60
|
assert_equal nil, @order.id
|
61
61
|
end
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
describe "JSON.from_json" do
|
65
65
|
it "is aliased by #deserialize" do
|
66
66
|
@order = Order.deserialize('{"id":1}')
|
@@ -76,19 +76,19 @@ class JsonHyperlinkRepresenterTest
|
|
76
76
|
before do
|
77
77
|
@link = Roar::Representer::Feature::Hypermedia::Hyperlink.new.extend(Roar::Representer::JSON::HyperlinkRepresenter).from_json({:rel => :self, :href => "http://roar.apotomo.de", :media => "web"}.to_json)
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
it "responds to #rel" do
|
81
81
|
assert_equal "self", @link.rel
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
it "responds to #href" do
|
85
85
|
assert_equal "http://roar.apotomo.de", @link.href
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
it "responds to #media" do
|
89
89
|
assert_equal "web", @link.media
|
90
90
|
end
|
91
|
-
|
91
|
+
|
92
92
|
it "responds to #to_json" do
|
93
93
|
assert_equal "{\"rel\":\"self\",\"href\":\"http://roar.apotomo.de\",\"media\":\"web\"}", @link.to_json
|
94
94
|
end
|
@@ -99,46 +99,46 @@ class JsonHypermediaTest
|
|
99
99
|
describe "Hypermedia API" do
|
100
100
|
before do
|
101
101
|
@c = Class.new do
|
102
|
-
include
|
102
|
+
include AttributesConstructor
|
103
103
|
include Roar::Representer::JSON
|
104
104
|
include Roar::Representer::Feature::Hypermedia
|
105
105
|
attr_accessor :id, :self, :next
|
106
|
-
|
106
|
+
|
107
107
|
property :id
|
108
|
-
|
108
|
+
|
109
109
|
link :self do "http://self" end
|
110
110
|
link :next do "http://next/#{id}" end
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
@r = @c.new
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
it "responds to #links" do
|
117
|
-
|
117
|
+
@r.links.must_equal({})
|
118
118
|
end
|
119
|
-
|
119
|
+
|
120
120
|
it "extracts links from JSON" do
|
121
121
|
r = @c.from_json({:links => [{:rel => "self", :href => "http://self"}]}.to_json)
|
122
|
-
|
123
|
-
assert_equal 1, r.
|
124
|
-
link = r.
|
125
|
-
assert_equal(["self", "http://self"], [link.rel, link.href])
|
122
|
+
|
123
|
+
assert_equal 1, r.links_array.size
|
124
|
+
link = r.links_array.first
|
125
|
+
assert_equal(["self", "http://self"], [link.rel, link.href])
|
126
126
|
end
|
127
|
-
|
127
|
+
|
128
128
|
it "renders link: correctly in JSON" do
|
129
129
|
assert_equal "{\"id\":1,\"links\":[{\"rel\":\"self\",\"href\":\"http://self\"},{\"rel\":\"next\",\"href\":\"http://next/1\"}]}", @c.new(:id => 1).to_json
|
130
130
|
end
|
131
|
-
|
131
|
+
|
132
132
|
it "doesn't render links when empty" do
|
133
133
|
assert_equal("{\"links\":[]}", Class.new do
|
134
134
|
include Roar::Representer::JSON
|
135
135
|
include Roar::Representer::Feature::Hypermedia
|
136
|
-
|
136
|
+
|
137
137
|
link :self do nil end
|
138
138
|
link :next do false end
|
139
139
|
end.new.to_json)
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
end
|
143
143
|
end
|
144
144
|
|
@@ -3,28 +3,42 @@ require 'roar/representer/transport/net_http'
|
|
3
3
|
|
4
4
|
class NetHTTPTransportTest < MiniTest::Spec
|
5
5
|
describe "Transport" do
|
6
|
+
let(:url) { "http://roar.example.com/method" }
|
7
|
+
let(:body) { "booty" }
|
8
|
+
let(:as) { "application/xml" }
|
6
9
|
before do
|
7
10
|
@transport = Roar::Representer::Transport::NetHTTP.new
|
8
11
|
end
|
9
|
-
|
12
|
+
|
10
13
|
it "#get_uri returns response" do
|
11
|
-
|
14
|
+
@transport.get_uri(url, as).must_match_net_response :get, url, as
|
12
15
|
end
|
13
|
-
|
16
|
+
|
14
17
|
it "#post_uri returns response" do
|
15
|
-
|
18
|
+
@transport.post_uri(url, body, as).must_match_net_response :post, url, as, body
|
16
19
|
end
|
17
|
-
|
20
|
+
|
18
21
|
it "#put_uri returns response" do
|
19
|
-
|
22
|
+
@transport.put_uri(url, body, as).must_match_net_response :put, url, as, body
|
20
23
|
end
|
21
|
-
|
24
|
+
|
22
25
|
it "#delete_uri returns response" do
|
23
|
-
|
26
|
+
@transport.delete_uri(url, as).must_match_net_response :delete, url, as
|
24
27
|
end
|
25
|
-
|
28
|
+
|
26
29
|
it "#patch_uri returns response" do
|
27
|
-
|
30
|
+
@transport.patch_uri(url, body, as).must_match_net_response :patch, url, as, body
|
28
31
|
end
|
29
32
|
end
|
30
33
|
end
|
34
|
+
|
35
|
+
module MiniTest::Assertions
|
36
|
+
|
37
|
+
def assert_net_response(type, response, url, as, body = nil)
|
38
|
+
# TODO: Assert headers
|
39
|
+
assert_equal "<method>#{type}#{(' - ' + body) if body}</method>", response.body
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
Net::HTTPOK.infect_an_assertion :assert_net_response, :must_match_net_response
|
data/test/order_representers.rb
CHANGED
@@ -3,35 +3,35 @@ require 'roar/representer/feature/http_verbs'
|
|
3
3
|
require 'roar/representer/feature/hypermedia'
|
4
4
|
|
5
5
|
module JSON
|
6
|
-
|
6
|
+
|
7
7
|
class Item
|
8
8
|
include Roar::Representer::JSON
|
9
|
-
|
9
|
+
|
10
10
|
property :article_id
|
11
11
|
property :amount
|
12
|
-
|
12
|
+
|
13
13
|
include Roar::Representer::Feature::HttpVerbs
|
14
14
|
include Roar::Representer::Feature::Hypermedia
|
15
15
|
end
|
16
|
-
|
17
|
-
|
16
|
+
|
17
|
+
|
18
18
|
class Order
|
19
19
|
include Roar::Representer::JSON
|
20
20
|
property :client_id
|
21
21
|
collection :items, :class => Item
|
22
|
-
|
23
|
-
|
22
|
+
|
23
|
+
|
24
24
|
include Roar::Representer::Feature::HttpVerbs
|
25
25
|
include Roar::Representer::Feature::Hypermedia
|
26
|
-
|
27
|
-
|
26
|
+
|
27
|
+
|
28
28
|
link :items do
|
29
29
|
items_url
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
link :self do
|
33
33
|
order_url(represented)
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
end
|