roar 0.0.1.alpha1 → 1.0.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.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.travis.yml +10 -0
  4. data/CHANGES.markdown +203 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE +20 -0
  7. data/README.markdown +734 -0
  8. data/Rakefile +10 -0
  9. data/TODO.markdown +13 -0
  10. data/examples/example.rb +370 -0
  11. data/examples/example_server.rb +34 -0
  12. data/gemfiles/Gemfile.representable-2.0 +5 -0
  13. data/gemfiles/Gemfile.representable-2.1 +5 -0
  14. data/lib/roar/client.rb +38 -0
  15. data/lib/roar/coercion.rb +20 -0
  16. data/lib/roar/decorator.rb +11 -0
  17. data/lib/roar/http_verbs.rb +88 -0
  18. data/lib/roar/hypermedia.rb +179 -0
  19. data/lib/roar/json/collection.rb +3 -0
  20. data/lib/roar/json/collection_json.rb +208 -0
  21. data/lib/roar/json/hal.rb +223 -0
  22. data/lib/roar/json/hash.rb +3 -0
  23. data/lib/roar/json/json_api.rb +228 -0
  24. data/lib/roar/json.rb +55 -0
  25. data/lib/roar/representer.rb +11 -0
  26. data/lib/roar/transport/faraday.rb +49 -0
  27. data/lib/roar/transport/net_http/request.rb +77 -0
  28. data/lib/roar/transport/net_http.rb +49 -0
  29. data/lib/roar/version.rb +1 -1
  30. data/lib/roar/xml.rb +54 -0
  31. data/lib/roar.rb +4 -1
  32. data/roar.gemspec +14 -5
  33. data/test/client_test.rb +36 -0
  34. data/test/coercion_feature_test.rb +23 -0
  35. data/test/collection_json_test.rb +130 -0
  36. data/test/decorator_test.rb +132 -0
  37. data/test/faraday_http_transport_test.rb +82 -0
  38. data/test/fixtures/sample.pem +31 -0
  39. data/test/hal_json_test.rb +160 -0
  40. data/test/hal_links_test.rb +31 -0
  41. data/test/http_verbs_test.rb +154 -0
  42. data/test/hypermedia_feature_test.rb +155 -0
  43. data/test/hypermedia_test.rb +190 -0
  44. data/test/integration/Gemfile +4 -0
  45. data/test/integration/band_representer.rb +8 -0
  46. data/test/integration/runner.rb +42 -0
  47. data/test/integration/server.rb +120 -0
  48. data/test/integration/ssl_server.rb +60 -0
  49. data/test/json_api_test.rb +395 -0
  50. data/test/json_representer_test.rb +149 -0
  51. data/test/lib/runner.rb +134 -0
  52. data/test/lonely_test.rb +9 -0
  53. data/test/net_http_transport_test.rb +78 -0
  54. data/test/representer_test.rb +39 -0
  55. data/test/ssl_client_certs_test.rb +102 -0
  56. data/test/test_helper.rb +60 -0
  57. data/test/xml_representer_test.rb +68 -0
  58. metadata +209 -49
@@ -0,0 +1,60 @@
1
+ require 'sinatra/base'
2
+ require 'webrick'
3
+ require 'webrick/https'
4
+ require 'openssl'
5
+ require 'sinatra/multi_route'
6
+
7
+ name = "/C=US/ST=SomeState/L=SomeCity/O=Organization/OU=Unit/CN=localhost"
8
+ ca = OpenSSL::X509::Name.parse(name)
9
+ key = OpenSSL::PKey::RSA.new(1024)
10
+ crt = OpenSSL::X509::Certificate.new
11
+ crt.version = 2
12
+ crt.serial = 1
13
+ crt.subject = ca
14
+ crt.issuer = ca
15
+ crt.public_key = key.public_key
16
+ crt.not_before = Time.now
17
+ crt.not_after = Time.now + 1 * 365 * 24 * 60 * 60 # 1 year
18
+ webrick_options = {
19
+ :Port => 8443,
20
+ :SSLEnable => true,
21
+ :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
22
+ :SSLCertificate => crt,
23
+ :SSLPrivateKey => key,
24
+ :SSLCertName => [[ "CN", WEBrick::Utils::getservername ]],
25
+ }
26
+
27
+ class SslServer < Sinatra::Base
28
+ register Sinatra::MultiRoute
29
+
30
+ get '/ping' do
31
+ "1"
32
+ end
33
+
34
+ route :get, :post, :put, :delete, "/bands/bodyjar" do
35
+ %{{"name": "Bodyjar"}}
36
+ end
37
+
38
+ # FIXME: redundant with server.rb.
39
+ helpers do
40
+ def protected!
41
+ return if authorized?
42
+ headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
43
+ halt 401, "Not authorized\n"
44
+ end
45
+
46
+ def authorized?
47
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
48
+ @auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['admin', 'password']
49
+ end
50
+ end
51
+
52
+ get "/protected" do
53
+ protected!
54
+
55
+ %{{"name": "Bodyjar"}}
56
+ end
57
+ end
58
+ server = ::Rack::Handler::WEBrick
59
+
60
+ server.run(SslServer, webrick_options)
@@ -0,0 +1,395 @@
1
+ require 'test_helper'
2
+ require 'roar/json/json_api'
3
+ require 'json'
4
+
5
+ class JSONAPITest < MiniTest::Spec
6
+ let(:song) {
7
+ s = OpenStruct.new(
8
+ bla: "halo",
9
+ id: "1",
10
+ title: 'Computadores Fazem Arte',
11
+ album: OpenStruct.new(id: 9, title: "Hackers"),
12
+ :album_id => "9",
13
+ :musician_ids => ["1","2"],
14
+ :composer_id => "10",
15
+ :listener_ids => ["8"],
16
+ musicians: [OpenStruct.new(id: 1, name: "Eddie Van Halen"), OpenStruct.new(id: 2, name: "Greg Howe")]
17
+ )
18
+
19
+ }
20
+
21
+ # minimal resource, singular
22
+ module MinimalSingular
23
+ include Roar::JSON::JSONAPI
24
+ type :songs
25
+
26
+ property :id
27
+ end
28
+
29
+ class MinimalSingularDecorator < Roar::Decorator
30
+ include Roar::JSON::JSONAPI
31
+ type :songs
32
+
33
+ property :id
34
+ end
35
+
36
+ [MinimalSingular, MinimalSingularDecorator].each do |representer|
37
+ describe "minimal singular with #{representer}" do
38
+ subject { representer.prepare(song) }
39
+
40
+ it { subject.to_json.must_equal "{\"songs\":{\"id\":\"1\"}}" }
41
+ it { subject.from_json("{\"songs\":{\"id\":\"2\"}}").id.must_equal "2" }
42
+ end
43
+ end
44
+
45
+
46
+
47
+ module Singular
48
+ include Roar::JSON::JSONAPI
49
+ type :songs
50
+
51
+ property :id
52
+ property :title
53
+
54
+ # local per-model "id" links
55
+ links do
56
+ property :album_id, :as => :album
57
+ collection :musician_ids, :as => :musicians
58
+ end
59
+ has_one :composer
60
+ has_many :listeners
61
+
62
+
63
+ # global document links.
64
+ link "songs.album" do
65
+ {
66
+ type: "album",
67
+ href: "http://example.com/albums/{songs.album}"
68
+ }
69
+ end
70
+
71
+ compound do
72
+ property :album do
73
+ property :title
74
+ end
75
+
76
+ collection :musicians do
77
+ property :name
78
+ end
79
+ end
80
+ end
81
+
82
+ class SingularDecorator < Roar::Decorator
83
+ include Roar::JSON::JSONAPI
84
+ type :songs
85
+
86
+ property :id
87
+ property :title
88
+
89
+ # local per-model "id" links
90
+ links do
91
+ property :album_id, :as => :album
92
+ collection :musician_ids, :as => :musicians
93
+ end
94
+ has_one :composer
95
+ has_many :listeners
96
+
97
+
98
+ # global document links.
99
+ link "songs.album" do
100
+ {
101
+ type: "album",
102
+ href: "http://example.com/albums/{songs.album}"
103
+ }
104
+ end
105
+
106
+ compound do
107
+ property :album do
108
+ property :title
109
+ end
110
+
111
+ collection :musicians do
112
+ property :name
113
+ end
114
+ end
115
+ end
116
+
117
+ [Singular, SingularDecorator].each do |representer|
118
+ describe "singular with #{representer}" do
119
+ subject { song.extend(Singular) }
120
+
121
+ let (:document) do
122
+ {
123
+ "songs" => {
124
+ "id" => "1",
125
+ "title" => "Computadores Fazem Arte",
126
+ "links" => {
127
+ "album" => "9",
128
+ "musicians" => [ "1", "2" ],
129
+ "composer"=>"10",
130
+ "listeners"=>["8"]
131
+ }
132
+ },
133
+ "links" => {
134
+ "songs.album"=> {
135
+ "href"=>"http://example.com/albums/{songs.album}", "type"=>"album"
136
+ }
137
+ },
138
+ "linked" => {
139
+ "album"=> [{"title"=>"Hackers"}],
140
+ "musicians"=> [
141
+ {"name"=>"Eddie Van Halen"},
142
+ {"name"=>"Greg Howe"}
143
+ ]
144
+ }
145
+ }
146
+ end
147
+
148
+ # to_hash
149
+ it do
150
+ subject.to_hash.must_equal document
151
+ end
152
+
153
+ # #to_json
154
+ it do
155
+ subject.to_json.must_equal JSON.generate(document)
156
+ end
157
+
158
+ # #from_json
159
+ it do
160
+ song = OpenStruct.new.extend(Singular)
161
+ song.from_json(
162
+ JSON.generate(
163
+ {
164
+ "songs" => {
165
+ "id" => "1",
166
+ "title" => "Computadores Fazem Arte",
167
+ "links" => {
168
+ "album" => "9",
169
+ "musicians" => [ "1", "2" ],
170
+ "composer"=>"10",
171
+ "listeners"=>["8"]
172
+ }
173
+ },
174
+ "links" => {
175
+ "songs.album"=> {
176
+ "href"=>"http://example.com/albums/{songs.album}", "type"=>"album"
177
+ }
178
+ }
179
+ }
180
+ )
181
+ )
182
+
183
+ song.id.must_equal "1"
184
+ song.title.must_equal "Computadores Fazem Arte"
185
+ song.album_id.must_equal "9"
186
+ song.musician_ids.must_equal ["1", "2"]
187
+ song.composer_id.must_equal "10"
188
+ song.listener_ids.must_equal ["8"]
189
+ end
190
+ end
191
+ end
192
+
193
+
194
+ # collection with links
195
+ [Singular, SingularDecorator].each do |representer|
196
+ describe "collection with links and compound" do
197
+ subject { Singular.for_collection.prepare([song, song]) }
198
+
199
+ let (:document) do
200
+ {
201
+ "songs" => [
202
+ {
203
+ "id" => "1",
204
+ "title" => "Computadores Fazem Arte",
205
+ "links" => {
206
+ "album" => "9",
207
+ "musicians" => [ "1", "2" ],
208
+ "composer"=>"10",
209
+ "listeners"=>["8"]
210
+ }
211
+ }, {
212
+ "id" => "1",
213
+ "title" => "Computadores Fazem Arte",
214
+ "links" => {
215
+ "album" => "9",
216
+ "musicians" => [ "1", "2" ],
217
+ "composer"=>"10",
218
+ "listeners"=>["8"]
219
+ }
220
+ }
221
+ ],
222
+ "links" => {
223
+ "songs.album" => {
224
+ "href" => "http://example.com/albums/{songs.album}",
225
+ "type" => "album" # DISCUSS: does that have to be albums ?
226
+ },
227
+ },
228
+ "linked"=>{
229
+ "album" =>[{"title"=>"Hackers"}], # only once!
230
+ "musicians"=>[{"name"=>"Eddie Van Halen"}, {"name"=>"Greg Howe"}]
231
+ }
232
+ }
233
+ end
234
+
235
+ # to_hash
236
+ it do
237
+ subject.to_hash.must_equal document
238
+ end
239
+
240
+ # #to_json
241
+ it { subject.to_json.must_equal JSON.generate(document) }
242
+ end
243
+
244
+
245
+ # from_json
246
+ it do
247
+ song1, song2 = Singular.for_collection.prepare([OpenStruct.new, OpenStruct.new]).from_json(
248
+ JSON.generate(
249
+ {
250
+ "songs" => [
251
+ {
252
+ "id" => "1",
253
+ "title" => "Computadores Fazem Arte",
254
+ "links" => {
255
+ "album" => "9",
256
+ "musicians" => [ "1", "2" ],
257
+ "composer"=>"10",
258
+ "listeners"=>["8"]
259
+ },
260
+ },
261
+ {
262
+ "id" => "2",
263
+ "title" => "Talking To Remind Me",
264
+ "links" => {
265
+ "album" => "1",
266
+ "musicians" => [ "3", "4" ],
267
+ "composer"=>"2",
268
+ "listeners"=>["6"]
269
+ }
270
+ },
271
+ ],
272
+ "links" => {
273
+ "songs.album"=> {
274
+ "href"=>"http://example.com/albums/{songs.album}", "type"=>"album"
275
+ }
276
+ }
277
+ }
278
+ )
279
+ )
280
+
281
+ song1.id.must_equal "1"
282
+ song1.title.must_equal "Computadores Fazem Arte"
283
+ song1.album_id.must_equal "9"
284
+ song1.musician_ids.must_equal ["1", "2"]
285
+ song1.composer_id.must_equal "10"
286
+ song1.listener_ids.must_equal ["8"]
287
+
288
+ song2.id.must_equal "2"
289
+ song2.title.must_equal "Talking To Remind Me"
290
+ song2.album_id.must_equal "1"
291
+ song2.musician_ids.must_equal ["3", "4"]
292
+ song2.composer_id.must_equal "2"
293
+ song2.listener_ids.must_equal ["6"]
294
+ end
295
+ end
296
+
297
+
298
+ class CollectionWithoutCompound < self
299
+ module Representer
300
+ include Roar::JSON::JSONAPI
301
+ type :songs
302
+
303
+ property :id
304
+ property :title
305
+
306
+ # local per-model "id" links
307
+ links do
308
+ property :album_id, :as => :album
309
+ collection :musician_ids, :as => :musicians
310
+ end
311
+ has_one :composer
312
+ has_many :listeners
313
+
314
+
315
+ # global document links.
316
+ link "songs.album" do
317
+ {
318
+ type: "album",
319
+ href: "http://example.com/albums/{songs.album}"
320
+ }
321
+ end
322
+ end
323
+
324
+ subject { [song, song].extend(Singular.for_collection) }
325
+
326
+ # to_json
327
+ it do
328
+ subject.extend(Representer.for_collection).to_hash.must_equal(
329
+ {
330
+ "songs"=>[{"id"=>"1", "title"=>"Computadores Fazem Arte", "links"=>{"album"=>"9", "musicians"=>["1", "2"], "composer"=>"10", "listeners"=>["8"]}}, {"id"=>"1", "title"=>"Computadores Fazem Arte", "links"=>{"album"=>"9", "musicians"=>["1", "2"], "composer"=>"10", "listeners"=>["8"]}}],
331
+ "links"=>{"songs.album"=>{"href"=>"http://example.com/albums/{songs.album}", "type"=>"album"}
332
+ }
333
+ }
334
+ )
335
+ end
336
+ end
337
+
338
+
339
+ class ExplicitMeta < self
340
+ module Representer
341
+ include Roar::JSON::JSONAPI
342
+
343
+ type :songs
344
+ property :id
345
+
346
+ meta do
347
+ property :page
348
+ end
349
+ end
350
+
351
+ module Page
352
+ def page
353
+ 2
354
+ end
355
+ end
356
+
357
+ let (:song) { Struct.new(:id).new(1) }
358
+
359
+ subject { [song, song].extend(Representer.for_collection).extend(Page) }
360
+
361
+ # to_json
362
+ it do
363
+ subject.to_hash.must_equal(
364
+ {
365
+ "songs"=>[{"id"=>1}, {"id"=>1}],
366
+ "meta" =>{"page"=>2}
367
+ }
368
+ )
369
+ end
370
+ end
371
+
372
+
373
+ class ImplicitMeta < self
374
+ module Representer
375
+ include Roar::JSON::JSONAPI
376
+
377
+ type :songs
378
+ property :id
379
+ end
380
+
381
+ let (:song) { Struct.new(:id).new(1) }
382
+
383
+ subject { [song, song].extend(Representer.for_collection) }
384
+
385
+ # to_json
386
+ it do
387
+ subject.to_hash("meta" => {"page" => 2}).must_equal(
388
+ {
389
+ "songs"=>[{"id"=>1}, {"id"=>1}],
390
+ "meta" =>{"page"=>2}
391
+ }
392
+ )
393
+ end
394
+ end
395
+ end
@@ -0,0 +1,149 @@
1
+ require 'test_helper'
2
+
3
+ require "test_xml/mini_test"
4
+ require "roar/json"
5
+
6
+ class JsonRepresenterTest < MiniTest::Spec
7
+ class Order
8
+ include Roar::JSON
9
+ property :id
10
+ property :pending
11
+ attr_accessor :id, :pending
12
+
13
+ def id=(v) # in ruby 2.2, #id= is not there, all at sudden. what *is* that?
14
+ @id=v
15
+ end
16
+ end
17
+
18
+
19
+ describe "JsonRepresenter" do
20
+ before do
21
+ @order = Order.new
22
+ end
23
+
24
+
25
+ describe "#to_json" do
26
+ before do
27
+ @order.id = 1
28
+ end
29
+
30
+ it "returns the serialized model" do
31
+ assert_equal '{"id":1}', @order.to_json
32
+ end
33
+
34
+ it "is aliased by #serialize" do
35
+ assert_equal '{"id":1}', @order.serialize
36
+ end
37
+
38
+ it "accepts :include and :exclude" do
39
+ assert_equal '{}', @order.to_json(:exclude => [:id])
40
+ end
41
+ end
42
+
43
+ describe "#from_json" do
44
+ it "returns the deserialized model" do
45
+ @order.from_json('{"id":1}')
46
+ assert_equal 1, @order.id
47
+ end
48
+
49
+ it "is aliased by #deserialize" do
50
+ @order.deserialize('{"id":1}')
51
+ assert_equal 1, @order.id
52
+ end
53
+
54
+ it "works with a nil document" do
55
+ assert @order.from_json(nil)
56
+ end
57
+
58
+ it "works with an empty document" do
59
+ assert @order.from_json('')
60
+ end
61
+
62
+ it "accepts :include and :exclude" do
63
+ @order.from_json('{"id":1}', :exclude => [:id])
64
+ assert_equal nil, @order.id
65
+ end
66
+ end
67
+
68
+ describe "JSON.from_json" do
69
+ it "is aliased by #deserialize" do
70
+ @order = Order.new.deserialize('{"id":1}')
71
+ assert_equal 1, @order.id
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ # test the generic roar+json HyperlinkRepresenter
78
+ class JsonHyperlinkRepresenterTest
79
+ describe "API" do
80
+ before do
81
+ @link = Roar::Hypermedia::Hyperlink.new.extend(Roar::JSON::HyperlinkRepresenter).from_json(
82
+ '{"rel":"self", "href":"http://roar.apotomo.de", "media":"web"}')
83
+ end
84
+
85
+ it "responds to #rel" do
86
+ assert_equal "self", @link.rel
87
+ end
88
+
89
+ it "responds to #href" do
90
+ assert_equal "http://roar.apotomo.de", @link.href
91
+ end
92
+
93
+ it "responds to #media" do
94
+ assert_equal "web", @link.media
95
+ end
96
+
97
+ it "responds to #to_json" do
98
+ assert_equal "{\"rel\":\"self\",\"href\":\"http://roar.apotomo.de\",\"media\":\"web\"}", @link.to_json
99
+ end
100
+ end
101
+ end
102
+
103
+ class JsonHypermediaTest
104
+ describe "Hypermedia API" do
105
+ before do
106
+ @c = Class.new do
107
+ include AttributesConstructor
108
+ include Roar::JSON
109
+ include Roar::Hypermedia
110
+ attr_accessor :id, :self, :next
111
+
112
+ property :id
113
+
114
+ link :self do "http://self" end
115
+ link :next do "http://next/#{id}" end
116
+ end
117
+
118
+ @r = @c.new
119
+ end
120
+
121
+ it "responds to #links" do
122
+ @r.links.must_equal nil
123
+ end
124
+
125
+ it "extracts links from JSON" do
126
+ r = @r.from_json('{"links":[{"rel":"self","href":"http://self"}]}')
127
+
128
+ assert_equal 1, r.links.size
129
+ link = r.links["self"]
130
+ assert_equal(["self", "http://self"], [link.rel, link.href])
131
+ end
132
+
133
+ it "renders link: correctly in JSON" do
134
+ assert_equal "{\"id\":1,\"links\":[{\"rel\":\"self\",\"href\":\"http://self\"},{\"rel\":\"next\",\"href\":\"http://next/1\"}]}", @c.new(:id => 1).to_json
135
+ end
136
+
137
+ it "doesn't render links when empty" do
138
+ assert_equal("{\"links\":[]}", Class.new do
139
+ include Roar::JSON
140
+ include Roar::Hypermedia
141
+
142
+ link :self do nil end
143
+ link :next do false end
144
+ end.new.to_json)
145
+ end
146
+
147
+ end
148
+ end
149
+