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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +10 -0
- data/CHANGES.markdown +203 -0
- data/Gemfile +6 -0
- data/LICENSE +20 -0
- data/README.markdown +734 -0
- data/Rakefile +10 -0
- data/TODO.markdown +13 -0
- data/examples/example.rb +370 -0
- data/examples/example_server.rb +34 -0
- data/gemfiles/Gemfile.representable-2.0 +5 -0
- data/gemfiles/Gemfile.representable-2.1 +5 -0
- data/lib/roar/client.rb +38 -0
- data/lib/roar/coercion.rb +20 -0
- data/lib/roar/decorator.rb +11 -0
- data/lib/roar/http_verbs.rb +88 -0
- data/lib/roar/hypermedia.rb +179 -0
- data/lib/roar/json/collection.rb +3 -0
- data/lib/roar/json/collection_json.rb +208 -0
- data/lib/roar/json/hal.rb +223 -0
- data/lib/roar/json/hash.rb +3 -0
- data/lib/roar/json/json_api.rb +228 -0
- data/lib/roar/json.rb +55 -0
- data/lib/roar/representer.rb +11 -0
- data/lib/roar/transport/faraday.rb +49 -0
- data/lib/roar/transport/net_http/request.rb +77 -0
- data/lib/roar/transport/net_http.rb +49 -0
- data/lib/roar/version.rb +1 -1
- data/lib/roar/xml.rb +54 -0
- data/lib/roar.rb +4 -1
- data/roar.gemspec +14 -5
- data/test/client_test.rb +36 -0
- data/test/coercion_feature_test.rb +23 -0
- data/test/collection_json_test.rb +130 -0
- data/test/decorator_test.rb +132 -0
- data/test/faraday_http_transport_test.rb +82 -0
- data/test/fixtures/sample.pem +31 -0
- data/test/hal_json_test.rb +160 -0
- data/test/hal_links_test.rb +31 -0
- data/test/http_verbs_test.rb +154 -0
- data/test/hypermedia_feature_test.rb +155 -0
- data/test/hypermedia_test.rb +190 -0
- data/test/integration/Gemfile +4 -0
- data/test/integration/band_representer.rb +8 -0
- data/test/integration/runner.rb +42 -0
- data/test/integration/server.rb +120 -0
- data/test/integration/ssl_server.rb +60 -0
- data/test/json_api_test.rb +395 -0
- data/test/json_representer_test.rb +149 -0
- data/test/lib/runner.rb +134 -0
- data/test/lonely_test.rb +9 -0
- data/test/net_http_transport_test.rb +78 -0
- data/test/representer_test.rb +39 -0
- data/test/ssl_client_certs_test.rb +102 -0
- data/test/test_helper.rb +60 -0
- data/test/xml_representer_test.rb +68 -0
- metadata +209 -49
data/Rakefile
CHANGED
data/TODO.markdown
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# 1.0
|
|
2
|
+
|
|
3
|
+
* merge client errors request
|
|
4
|
+
* simpler link for JSON-API?
|
|
5
|
+
* remove HttpVerbs deprecations
|
|
6
|
+
|
|
7
|
+
* Hyperlink representers => decrators. test hash representer with decorator (rpr)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
* Add proxies, so nested models can be lazy-loaded.
|
|
11
|
+
* move #prepare_links! call to #_links or something so it doesn't need to be called in #serialize.
|
|
12
|
+
* abstract ::links_definition_options and move them out of the generic representers (JSON, XML).
|
|
13
|
+
* work on HAL-Object
|
data/examples/example.rb
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require 'bundler'
|
|
4
|
+
Bundler.setup
|
|
5
|
+
|
|
6
|
+
require 'ostruct'
|
|
7
|
+
require 'roar/representer/json'
|
|
8
|
+
|
|
9
|
+
def reset_representer(*module_name)
|
|
10
|
+
module_name.each do |mod|
|
|
11
|
+
mod.module_eval do
|
|
12
|
+
@representable_attrs = nil
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Song < OpenStruct
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
module SongRepresenter
|
|
22
|
+
include Roar::Representer::JSON
|
|
23
|
+
|
|
24
|
+
property :title
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
song = Song.new(title: "Fate").extend(SongRepresenter)
|
|
28
|
+
puts song.to_json
|
|
29
|
+
|
|
30
|
+
# Parsing
|
|
31
|
+
|
|
32
|
+
song = Song.new.extend(SongRepresenter)
|
|
33
|
+
song.from_json('{"title":"Linoleum"}')
|
|
34
|
+
puts song.title
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Decorator
|
|
38
|
+
|
|
39
|
+
require 'roar/decorator'
|
|
40
|
+
|
|
41
|
+
module Decorator
|
|
42
|
+
class SongRepresenter < Roar::Decorator
|
|
43
|
+
include Roar::Representer::JSON
|
|
44
|
+
|
|
45
|
+
property :title
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
song = Song.new(title: "Medicine Balls")
|
|
50
|
+
puts Decorator::SongRepresenter.new(song).to_json
|
|
51
|
+
|
|
52
|
+
# Collections
|
|
53
|
+
|
|
54
|
+
reset_representer(SongRepresenter)
|
|
55
|
+
|
|
56
|
+
module SongRepresenter
|
|
57
|
+
include Roar::Representer::JSON
|
|
58
|
+
|
|
59
|
+
property :title
|
|
60
|
+
collection :composers
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
song = Song.new(title: "Roxanne", composers: ["Sting", "Stu Copeland"])
|
|
65
|
+
song.extend(SongRepresenter)
|
|
66
|
+
puts song.to_json
|
|
67
|
+
|
|
68
|
+
# Nesting
|
|
69
|
+
|
|
70
|
+
class Album < OpenStruct
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
module AlbumRepresenter
|
|
74
|
+
include Roar::Representer::JSON
|
|
75
|
+
|
|
76
|
+
property :title
|
|
77
|
+
collection :songs, extend: SongRepresenter, class: Song
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
album = Album.new(title: "True North", songs: [Song.new(title: "The Island"), Song.new(:title => "Changing Tide")])
|
|
81
|
+
album.extend(AlbumRepresenter)
|
|
82
|
+
puts album.to_json
|
|
83
|
+
|
|
84
|
+
album = Album.new
|
|
85
|
+
album.extend(AlbumRepresenter)
|
|
86
|
+
|
|
87
|
+
album.from_json('{"title":"Indestructible","songs":[{"title":"Tropical London"},{"title":"Roadblock"}]}')
|
|
88
|
+
|
|
89
|
+
puts album.songs.last.inspect
|
|
90
|
+
|
|
91
|
+
# Inline Representers # FIXME: what about collections?
|
|
92
|
+
|
|
93
|
+
reset_representer(AlbumRepresenter)
|
|
94
|
+
|
|
95
|
+
module AlbumRepresenter
|
|
96
|
+
include Roar::Representer::JSON
|
|
97
|
+
|
|
98
|
+
property :title
|
|
99
|
+
|
|
100
|
+
collection :songs, class: Song do
|
|
101
|
+
property :title
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
album = Album.new(title: "True North", songs: [Song.new(title: "The Island"), Song.new(:title => "Changing Tide")])
|
|
106
|
+
album.extend(AlbumRepresenter)
|
|
107
|
+
puts album.to_json
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
album = Album.new
|
|
112
|
+
album.extend(AlbumRepresenter)
|
|
113
|
+
album.from_json('{"title":"True North","songs":[{"title":"The Island"},{"title":"Changing Tide"}]}')
|
|
114
|
+
puts album.title
|
|
115
|
+
puts album.songs.first.title
|
|
116
|
+
|
|
117
|
+
# parse_strategy: :sync
|
|
118
|
+
|
|
119
|
+
reset_representer(AlbumRepresenter)
|
|
120
|
+
|
|
121
|
+
module AlbumRepresenter
|
|
122
|
+
include Roar::Representer::JSON
|
|
123
|
+
|
|
124
|
+
property :title
|
|
125
|
+
|
|
126
|
+
collection :songs, extend: SongRepresenter, parse_strategy: :sync
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
album = Album.new(title: "True North", songs: [Song.new(title: "The Island"), Song.new(:title => "Changing Tide")])
|
|
131
|
+
album.extend(AlbumRepresenter)
|
|
132
|
+
|
|
133
|
+
puts album.songs[0].object_id
|
|
134
|
+
album.from_json('{"title":"True North","songs":[{"title":"Secret Society"},{"title":"Changing Tide"}]}')
|
|
135
|
+
puts album.songs[0].title
|
|
136
|
+
puts album.songs[0].object_id##
|
|
137
|
+
|
|
138
|
+
# Coercion, renaming, ..
|
|
139
|
+
|
|
140
|
+
# Hypermedia
|
|
141
|
+
|
|
142
|
+
reset_representer(SongRepresenter)
|
|
143
|
+
|
|
144
|
+
module SongRepresenter
|
|
145
|
+
include Roar::Representer::JSON
|
|
146
|
+
include Roar::Representer::Feature::Hypermedia
|
|
147
|
+
|
|
148
|
+
property :title
|
|
149
|
+
|
|
150
|
+
link :self do
|
|
151
|
+
"http://songs/#{title}"
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
song.extend(SongRepresenter)
|
|
156
|
+
puts song.to_json
|
|
157
|
+
|
|
158
|
+
# roar-rails and URL helpers
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# Passing options into link
|
|
162
|
+
|
|
163
|
+
reset_representer(SongRepresenter)
|
|
164
|
+
|
|
165
|
+
module SongRepresenter
|
|
166
|
+
include Roar::Representer::JSON
|
|
167
|
+
|
|
168
|
+
property :title
|
|
169
|
+
|
|
170
|
+
link :self do |opts|
|
|
171
|
+
"http://#{opts[:base_url]}songs/#{title}"
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
song.extend(SongRepresenter)
|
|
176
|
+
puts song.to_json(base_url: "localhost:3001/")
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
# Discovering Hypermedia
|
|
180
|
+
|
|
181
|
+
song = Song.new.extend(SongRepresenter)
|
|
182
|
+
song.from_json('{"title":"Roxanne","links":[{"rel":"self","href":"http://songs/Roxanne"}]}')
|
|
183
|
+
puts song.links[:self].href
|
|
184
|
+
|
|
185
|
+
# Media Formats: HAL
|
|
186
|
+
|
|
187
|
+
require 'roar/representer/json/hal'
|
|
188
|
+
|
|
189
|
+
module HAL
|
|
190
|
+
module SongRepresenter
|
|
191
|
+
include Roar::Representer::JSON::HAL
|
|
192
|
+
|
|
193
|
+
property :title
|
|
194
|
+
|
|
195
|
+
link :self do
|
|
196
|
+
"http://songs/#{title}"
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
song.extend(HAL::SongRepresenter)
|
|
202
|
+
puts song.to_json
|
|
203
|
+
|
|
204
|
+
reset_representer(AlbumRepresenter)
|
|
205
|
+
|
|
206
|
+
module AlbumRepresenter
|
|
207
|
+
include Roar::Representer::JSON::HAL
|
|
208
|
+
|
|
209
|
+
property :title
|
|
210
|
+
|
|
211
|
+
collection :songs, class: Song, embedded: true do
|
|
212
|
+
property :title
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
album = Album.new(title: "True North", songs: [Song.new(title: "The Island"), Song.new(:title => "Changing Tide")])
|
|
217
|
+
album.extend(AlbumRepresenter)
|
|
218
|
+
puts album.to_json
|
|
219
|
+
|
|
220
|
+
# Media Formats: JSON+Collection
|
|
221
|
+
|
|
222
|
+
require 'roar/representer/json/collection_json'
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
module Collection
|
|
226
|
+
module SongRepresenter
|
|
227
|
+
include Roar::Representer::JSON::CollectionJSON
|
|
228
|
+
version "1.0"
|
|
229
|
+
href { "http://localhost/songs/" }
|
|
230
|
+
|
|
231
|
+
property :title
|
|
232
|
+
|
|
233
|
+
items(:class => Song) do
|
|
234
|
+
href { "//songs/#{title}" }
|
|
235
|
+
|
|
236
|
+
property :title, :prompt => "Song title"
|
|
237
|
+
|
|
238
|
+
link(:download) { "//songs/#{title}.mp3" }
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
template do
|
|
242
|
+
property :title, :prompt => "Song title"
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
queries do
|
|
246
|
+
link :search do
|
|
247
|
+
{:href => "//search", :data => [{:name => "q", :value => ""}]}
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
song = Song.new(title: "Roxanne")
|
|
254
|
+
song.extend(Collection::SongRepresenter)
|
|
255
|
+
puts song.to_json
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
# Client-side
|
|
260
|
+
# share in gem, parse existing document.
|
|
261
|
+
|
|
262
|
+
reset_representer(SongRepresenter)
|
|
263
|
+
|
|
264
|
+
module SongRepresenter
|
|
265
|
+
include Roar::Representer::JSON
|
|
266
|
+
include Roar::Representer::Feature::Hypermedia
|
|
267
|
+
|
|
268
|
+
property :title
|
|
269
|
+
property :id
|
|
270
|
+
|
|
271
|
+
link :self do
|
|
272
|
+
"http://songs/#{title}"
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
require 'roar/representer/feature/client'
|
|
278
|
+
|
|
279
|
+
module Client
|
|
280
|
+
class Song < OpenStruct
|
|
281
|
+
include Roar::Representer::JSON
|
|
282
|
+
include SongRepresenter
|
|
283
|
+
include Roar::Representer::Feature::Client
|
|
284
|
+
end
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
song = Client::Song.new(title: "Roxanne")
|
|
288
|
+
song.post("http://localhost:4567/songs", "application/json")
|
|
289
|
+
puts song.id
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
song = Client::Song.new
|
|
293
|
+
song.get("http://localhost:4567/songs/1", "application/json")
|
|
294
|
+
puts song.title
|
|
295
|
+
puts song.links[:self].href
|
|
296
|
+
|
|
297
|
+
# XML
|
|
298
|
+
|
|
299
|
+
require 'roar/representer/xml'
|
|
300
|
+
|
|
301
|
+
module XML
|
|
302
|
+
module SongRepresenter
|
|
303
|
+
include Roar::Representer::XML
|
|
304
|
+
include Roar::Representer::Feature::Hypermedia
|
|
305
|
+
|
|
306
|
+
property :title
|
|
307
|
+
property :id
|
|
308
|
+
|
|
309
|
+
link :self do
|
|
310
|
+
"http://songs/#{title}"
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
song = Song.new(title: "Roxanne", id: 42)
|
|
316
|
+
song.extend(XML::SongRepresenter)
|
|
317
|
+
puts song.to_xml
|
|
318
|
+
|
|
319
|
+
# Coercion
|
|
320
|
+
|
|
321
|
+
reset_representer(SongRepresenter)
|
|
322
|
+
|
|
323
|
+
require 'roar/representer/feature/coercion'
|
|
324
|
+
|
|
325
|
+
module SongRepresenter
|
|
326
|
+
include Roar::Representer::JSON
|
|
327
|
+
include Roar::Representer::Feature::Coercion
|
|
328
|
+
|
|
329
|
+
property :title
|
|
330
|
+
property :released_at, type: DateTime
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
song = Song.new
|
|
334
|
+
song.extend(SongRepresenter)
|
|
335
|
+
song.from_json('{"released_at":"1981/03/31"}')
|
|
336
|
+
|
|
337
|
+
puts song.released_at
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
class LinkOptionsCollection < Array
|
|
341
|
+
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
module HyperlinkiRepresenter
|
|
345
|
+
include Roar::Representer::JSON
|
|
346
|
+
|
|
347
|
+
def to_hash(*) # setup the link
|
|
348
|
+
# FIXME: why does self.to_s throw a stack level too deep (SystemStackError) ?
|
|
349
|
+
"#{self}"
|
|
350
|
+
# how would the Link instance get access to its Definition in order to execute the block?
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
module Representer
|
|
355
|
+
include Roar::Representer::JSON
|
|
356
|
+
|
|
357
|
+
def self.links
|
|
358
|
+
[:self, :next]
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
collection :links, :extend => HyperlinkiRepresenter
|
|
362
|
+
|
|
363
|
+
def links
|
|
364
|
+
# get link configurations from representable_attrs object.
|
|
365
|
+
#self.representable_attrs.links
|
|
366
|
+
LinkOptionsCollection.new(["self", "next"])
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
puts "".extend(Representer).to_json
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "sinatra"
|
|
5
|
+
require "ostruct"
|
|
6
|
+
require "roar/representer/json"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
get "/method" do
|
|
10
|
+
"<method>get</method>"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
post "/songs" do
|
|
14
|
+
'{"id":"1","title":"Roxanne","links":[{"rel":"self","href":"http://localhost/songs/1"}]}'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
get "/songs/1" do
|
|
19
|
+
'{"id":"1","title":"Roxanne","links":[{"rel":"self","href":"http://localhost/songs/1"}]}'
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
post "/respond404" do
|
|
23
|
+
status 404
|
|
24
|
+
'{"id":"1","title":"Roxanne","links":[{"rel":"self","href":"http://localhost/songs/1"}],"revision":"2"}'
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
post "/respond407" do
|
|
28
|
+
status 407
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
get "/respond200" do
|
|
32
|
+
status 200
|
|
33
|
+
'{"id":"1","title":"Roxanne","links":[{"rel":"self","href":"http://localhost/songs/1"},"revision":"3"]}'
|
|
34
|
+
end
|
data/lib/roar/client.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require "roar/http_verbs"
|
|
2
|
+
|
|
3
|
+
module Roar
|
|
4
|
+
# Automatically add accessors for properties and collections. Also mixes in HttpVerbs.
|
|
5
|
+
module Client
|
|
6
|
+
include HttpVerbs
|
|
7
|
+
|
|
8
|
+
def self.extended(base)
|
|
9
|
+
base.instance_eval do
|
|
10
|
+
representable_attrs.each do |attr|
|
|
11
|
+
name = attr.name
|
|
12
|
+
next if name == "links" # ignore hyperlinks.
|
|
13
|
+
|
|
14
|
+
# TODO: could anyone please make this better?
|
|
15
|
+
instance_eval %Q{
|
|
16
|
+
def #{name}=(v)
|
|
17
|
+
@#{name} = v
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def #{name}
|
|
21
|
+
@#{name}
|
|
22
|
+
end
|
|
23
|
+
}
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def to_hash(options={})
|
|
29
|
+
options[:links] ||= false
|
|
30
|
+
super(options)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def to_xml(options={}) # sorry, but i'm not even sure if anyone uses this module.
|
|
34
|
+
options[:links] ||= false
|
|
35
|
+
super(options)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
gem 'virtus'
|
|
2
|
+
require 'virtus'
|
|
3
|
+
require 'representable/coercion'
|
|
4
|
+
|
|
5
|
+
module Roar
|
|
6
|
+
# Use the +:type+ option to specify the conversion type.
|
|
7
|
+
# class ImmigrantSong
|
|
8
|
+
# include Roar::Representer::JSON
|
|
9
|
+
# include Roar::Representer::Feature::Coercion
|
|
10
|
+
#
|
|
11
|
+
# property :composed_at, :type => DateTime, :default => "May 12th, 2012"
|
|
12
|
+
# end
|
|
13
|
+
module Coercion
|
|
14
|
+
def self.included(base)
|
|
15
|
+
base.class_eval do
|
|
16
|
+
include Representable::Coercion
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
require 'roar/transport/net_http'
|
|
2
|
+
|
|
3
|
+
module Roar
|
|
4
|
+
# Gives HTTP-power to representers. They can serialize, send, process and deserialize HTTP-requests.
|
|
5
|
+
module HttpVerbs
|
|
6
|
+
|
|
7
|
+
class << self
|
|
8
|
+
attr_accessor :transport_engine
|
|
9
|
+
|
|
10
|
+
def included(base)
|
|
11
|
+
base.extend ClassMethods
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
self.transport_engine = ::Roar::Transport::NetHTTP
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
module ClassMethods
|
|
18
|
+
# GETs +url+ with +format+ and returns deserialized represented object.
|
|
19
|
+
def get(*args)
|
|
20
|
+
new.get(*args)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
attr_writer :transport_engine
|
|
26
|
+
def transport_engine
|
|
27
|
+
@transport_engine || HttpVerbs.transport_engine
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Serializes the object, POSTs it to +url+ with +format+, deserializes the returned document
|
|
31
|
+
# and updates properties accordingly.
|
|
32
|
+
def post(options={}, &block)
|
|
33
|
+
response = http.post_uri(options.merge(:body => serialize), &block)
|
|
34
|
+
handle_response(response)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# GETs +url+ with +format+, deserializes the returned document and updates properties accordingly.
|
|
38
|
+
def get(options={}, &block)
|
|
39
|
+
response = http.get_uri(options, &block)
|
|
40
|
+
handle_response(response)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Serializes the object, PUTs it to +url+ with +format+, deserializes the returned document
|
|
44
|
+
# and updates properties accordingly.
|
|
45
|
+
def put(options={}, &block)
|
|
46
|
+
response = http.put_uri(options.merge(:body => serialize), &block)
|
|
47
|
+
handle_response(response)
|
|
48
|
+
self
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def patch(options={}, &block)
|
|
52
|
+
response = http.patch_uri(options.merge(:body => serialize), &block)
|
|
53
|
+
handle_response(response)
|
|
54
|
+
self
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def delete(options, &block)
|
|
58
|
+
http.delete_uri(options, &block)
|
|
59
|
+
self
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
def handle_response(response)
|
|
64
|
+
document = response.body
|
|
65
|
+
deserialize(document)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def http
|
|
69
|
+
transport_engine.new
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def handle_deprecated_args(body, *args) # TODO: remove in 1.0.
|
|
73
|
+
options = args.first
|
|
74
|
+
|
|
75
|
+
if args.size > 1
|
|
76
|
+
warn %{DEPRECATION WARNING: #get, #post, #put, #delete and #patch no longer accept positional arguments. Please call them as follows:
|
|
77
|
+
get(uri: "http://localhost/songs", as: "application/json")
|
|
78
|
+
post(uri: "http://localhost/songs", as: "application/json")
|
|
79
|
+
Thank you and have a beautiful day.}
|
|
80
|
+
options = {:uri => args[0], :as => args[1]} if args.size == 2
|
|
81
|
+
options = {:uri => args[0], :as => args[2]}
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
options[:body] = body
|
|
85
|
+
options
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|