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
data/README.markdown ADDED
@@ -0,0 +1,734 @@
1
+ # ROAR
2
+
3
+ _Resource-Oriented Architectures in Ruby._
4
+
5
+ ## Introduction
6
+
7
+ Roar is a framework for parsing and rendering REST documents. Nothing more.
8
+
9
+ Representers let you define your API document structure and semantics. They allow both rendering representations from your models _and_ parsing documents to update your Ruby objects. The bi-directional nature of representers make them interesting for both server and client usage.
10
+
11
+ Roar comes with built-in JSON, JSON-HAL, JSON-API and XML support. Its highly modulare architecture provides features like coercion, hypermedia, HTTP transport, client caching and more.
12
+
13
+ Roar is completely framework-agnostic and loves being used in web kits like Rails, Webmachine, Sinatra, Padrino, etc. If you use Rails, consider [roar-rails](https://github.com/apotonick/roar-rails) for an enjoyable integration.
14
+
15
+ ## Representable
16
+
17
+ Roar is just a thin layer on top of the [representable](https://github.com/apotonick/representable) gem. While Roar gives you a DSL and behaviour for creating hypermedia APIs, representable implements all the mapping functionality.
18
+
19
+ If in need for a feature, make sure to check the [representable API docs](https://github.com/apotonick/representable) first.
20
+
21
+
22
+ ## Defining Representers
23
+
24
+ Let's see how representers work. They're fun to use.
25
+
26
+ ```ruby
27
+ require 'roar/json'
28
+
29
+ module SongRepresenter
30
+ include Roar::JSON
31
+
32
+ property :title
33
+ end
34
+ ```
35
+
36
+ API documents are defined using a representer module or decorator class. You can define plain attributes using the `::property` method.
37
+
38
+ Now let's assume we'd have `Song` which is an `ActiveRecord` class. Please note that Roar is not limited to ActiveRecord. In fact, it doesn't really care whether it's representing ActiveRecord, Datamapper or just an OpenStruct instance.
39
+
40
+ ```ruby
41
+ class Song < ActiveRecord
42
+ end
43
+ ```
44
+
45
+ ## Rendering
46
+
47
+ To render a document, you apply the representer to your model.
48
+
49
+ ```ruby
50
+ song = Song.new(title: "Fate")
51
+ song.extend(SongRepresenter)
52
+
53
+ song.to_json #=> {"title":"Fate"}
54
+ ```
55
+
56
+ Here, the representer is injected into the actual model and gives us a new `#to_json` method.
57
+
58
+ ## Parsing
59
+
60
+ The cool thing about representers is: they can be used for rendering and parsing. See how easy updating your model from a document is.
61
+
62
+ ```ruby
63
+ song = Song.new
64
+ song.extend(SongRepresenter)
65
+
66
+ song.from_json('{"title":"Linoleum"}')
67
+
68
+ song.title #=> Linoleum
69
+ ```
70
+
71
+ Again, `#from_json` comes from the representer and just updates the known properties.
72
+
73
+ Unknown attributes in the parsed document are simply ignored, making half-baked solutions like `strong_parameters` redundant.
74
+
75
+
76
+ ## Decorator
77
+
78
+ Many people dislike `#extend` due to eventual performance issue or object pollution. If you're one of those, just go with a decorator representer. They almost work identical to the module approach we just discovered.
79
+
80
+ ```ruby
81
+ require 'roar/decorator'
82
+
83
+ class SongRepresenter < Roar::Decorator
84
+ include Roar::JSON
85
+
86
+ property :title
87
+ end
88
+ ```
89
+ In place of a module you use a class, the DSL inside is the same you already know.
90
+
91
+ ```ruby
92
+ song = Song.new(title: "Medicine Balls")
93
+
94
+ SongRepresenter.new(song).to_json #=> {"title":"Medicine Balls"}
95
+ ```
96
+
97
+ Here, the `song` objects gets wrapped (or "decorated") by the decorator. It is treated as immutuable - Roar won't mix in any behaviour.
98
+
99
+ Note that decorators and representer modules have identical features. You can parse, render, nest, go nuts with both of them.
100
+
101
+ However, in this README we'll use modules to illustrate this framework.
102
+
103
+
104
+ ## Collections
105
+
106
+ Roar (or rather representable) also allows to map collections in documents.
107
+
108
+ ```ruby
109
+ module SongRepresenter
110
+ include Roar::JSON
111
+
112
+ property :title
113
+ collection :composers
114
+ end
115
+ ```
116
+
117
+ Where `::property` knows how to handle plain attributes, `::collection` does lists.
118
+
119
+ ```ruby
120
+ song = Song.new(title: "Roxanne", composers: ["Sting", "Stu Copeland"])
121
+ song.extend(SongRepresenter)
122
+
123
+ song.to_json #=> {"title":"Roxanne","composers":["Sting","Stu Copeland"]}
124
+ ```
125
+
126
+ And, yes, this also works for parsing: `from_json` will create and populate the array of the `composers` attribute.
127
+
128
+
129
+ ## Nesting
130
+
131
+ Now what if we need to tackle with collections of `Song`s? We need to implement an `Album` class.
132
+
133
+ ```ruby
134
+ class Album < ActiveRecord
135
+ has_many :songs
136
+ end
137
+ ```
138
+
139
+ Another representer to represent.
140
+
141
+ ```ruby
142
+ module AlbumRepresenter
143
+ include Roar::JSON
144
+
145
+ property :title
146
+ collection :songs, extend: SongRepresenter, class: Song
147
+ end
148
+ ```
149
+
150
+ Both `::property` and `::collection` accept options for nesting representers into representers.
151
+
152
+ The `extend:` option tells Roar which representer to use for the nested objects (here, the array items of the `album.songs` field). When parsing a document `class:` defines the nested object type.
153
+
154
+ Consider the following object setup.
155
+
156
+ ```ruby
157
+ album = Album.new(title: "True North")
158
+ album.songs << Song.new(title: "The Island")
159
+ album.songs << Song.new(:title => "Changing Tide")
160
+ ```
161
+
162
+ You apply the `AlbumRepresenter` and you get a nested document.
163
+
164
+ ```ruby
165
+ album.extend(AlbumRepresenter)
166
+
167
+ album.to_json #=> {"title":"True North","songs":[{"title":"The Island"},{"title":"Changing Tide"}]}
168
+ ```
169
+
170
+ This works vice-versa.
171
+
172
+ ```ruby
173
+ album = Album.new
174
+ album.extend(AlbumRepresenter)
175
+
176
+ album.from_json('{"title":"Indestructible","songs":[{"title":"Tropical London"},{"title":"Roadblock"}]}')
177
+
178
+ puts album.songs[1] #=> #<Song title="Roadblock">
179
+ ```
180
+
181
+ The nesting of two representers can map composed object as you find them in many many APIs.
182
+
183
+ In case you're after virtual nesting, where a nested block in your document still maps to the same outer object, [check out the `::nested` method](https://github.com/apotonick/representable#document-nesting).
184
+
185
+ ## Inline Representer
186
+
187
+ Sometimes you don't wanna create two separate representers - although it makes them reusable across your app. Use inline representers if you're not intending this.
188
+
189
+ ```ruby
190
+ module AlbumRepresenter
191
+ include Roar::JSON
192
+
193
+ property :title
194
+
195
+ collection :songs, class: Song do
196
+ property :title
197
+ end
198
+ end
199
+ ```
200
+
201
+ This will give you the same rendering and parsing behaviour as in the previous example with just one module.
202
+
203
+
204
+ ## Syncing Objects
205
+
206
+ Usually, when parsing, nested objects are created from scratch. If you want nested objects to be updated instead of being newly created, use `parse_strategy:`.
207
+
208
+ ```ruby
209
+ module AlbumRepresenter
210
+ include Roar::JSON
211
+
212
+ property :title
213
+
214
+ collection :songs, extend: SongRepresenter, parse_strategy: :sync
215
+ end
216
+ ```
217
+
218
+ This will advise Roar to update existing `songs`.
219
+
220
+ ```ruby
221
+ album.songs[0].object_id #=> 81431220
222
+
223
+ album.from_json('{"title":"True North","songs":[{"title":"Secret Society"},{"title":"Changing Tide"}]}')
224
+
225
+ album.songs[0].title #=> Secret Society
226
+ album.songs[0].object_id #=> 81431220
227
+ ```
228
+ Roar didn't create a new `Song` instance but updated its attributes, only.
229
+
230
+ We're currently [working on](https://github.com/apotonick/roar/issues/85) better strategies to easily implement `POST` and `PUT` semantics in your APIs without having to worry about the nitty-gritties.
231
+
232
+
233
+ ## Coercion
234
+
235
+ Roar provides coercion with the [virtus](https://github.com/solnic/virtus) gem.
236
+
237
+ ```ruby
238
+ require 'roar/feature/coercion'
239
+
240
+ module SongRepresenter
241
+ include Roar::JSON
242
+ include Roar::Coercion
243
+
244
+ property :title
245
+ property :released_at, type: DateTime
246
+ end
247
+ ```
248
+
249
+ The `:type` option allows to set a virtus-compatible type.
250
+
251
+ ```ruby
252
+ song = Song.new
253
+ song.extend(SongRepresenter)
254
+
255
+ song.from_json('{"released_at":"1981/03/31"}')
256
+
257
+ song.released_at #=> 1981-03-31T00:00:00+00:00
258
+ ```
259
+
260
+
261
+ ## More Features
262
+
263
+ Roar/representable gives you many more mapping features like [renaming attributes](https://github.com/apotonick/representable/#aliasing), [wrapping](https://github.com/apotonick/representable/#wrapping), [passing options](https://github.com/apotonick/representable/#passing-options), etc.
264
+
265
+
266
+ ## Hypermedia
267
+
268
+ Roar comes with built-in support for embedding and processing hypermedia in your documents.
269
+
270
+ ```ruby
271
+ module SongRepresenter
272
+ include Roar::JSON
273
+ include Roar::Hypermedia
274
+
275
+ property :title
276
+
277
+ link :self do
278
+ "http://songs/#{title}"
279
+ end
280
+ end
281
+ ```
282
+
283
+ The `Hypermedia` feature allows declaring links using the `::link` method.
284
+
285
+ ```ruby
286
+ song.extend(SongRepresenter)
287
+ song.to_json #=> {"title":"Roxanne","links":[{"rel":"self","href":"http://songs/Roxanne"}]}
288
+ ```
289
+
290
+ Per default, links are pushed into the hash using the `links` key. Link blocks are executed in represented context, allowing you to call any instance method of your model (here, we call `#title`).
291
+
292
+ Also, note that [roar-rails](https://github.com/apotonick/roar-rails) allows using URL helpers in link blocks.
293
+
294
+
295
+ ## Passing Options
296
+
297
+ Sometimes you need more data in the link block. Data that's not available from the represented model.
298
+
299
+ ```ruby
300
+ module SongRepresenter
301
+ include Roar::JSON
302
+
303
+ property :title
304
+
305
+ link :self do |opts|
306
+ "http://#{opts[:base_url]}songs/#{title}"
307
+ end
308
+ end
309
+ ```
310
+
311
+ Pass this data to the rendering method.
312
+
313
+ ```ruby
314
+ song.to_json(base_url: "localhost:3001/")
315
+ ```
316
+
317
+ Any options passed to `#to_json` will be available as block arguments in the link blocks.
318
+
319
+
320
+ ## Consuming Hypermedia
321
+
322
+ Since we defined hypermedia attributes in the representer we can also consume this hypermedia when we parse documents.
323
+
324
+ ```ruby
325
+ song.from_json('{"title":"Roxanne","links":[{"rel":"self","href":"http://songs/Roxanne"}]}')
326
+
327
+ song.links[:self].href #=> "http://songs/Roxanne"
328
+ ```
329
+
330
+ Reading link attributes works by using `#links[]` on the consuming instance.
331
+
332
+ This allows an easy way to discover hypermedia and build navigational logic on top.
333
+
334
+
335
+ ## Media Formats
336
+
337
+ While Roar comes with a built-in hypermedia format, there's official media types that are widely recognized. Roar currently supports HAL and Collection+JSON. Support for Siren and JSON-API is planned when there's sponsors.
338
+
339
+ Simply by including a module you make your representer understand the media type. This makes it easy to change formats during evaluation.
340
+
341
+ ## HAL-JSON
342
+
343
+ The [HAL](http://stateless.co/hal_specification.html) format is a simple media type that defines embedded resources and hypermedia.
344
+
345
+ ```ruby
346
+ require 'roar/json/hal'
347
+
348
+ module SongRepresenter
349
+ include Roar::JSON::HAL
350
+
351
+ property :title
352
+
353
+ link :self do
354
+ "http://songs/#{title}"
355
+ end
356
+ end
357
+ ```
358
+
359
+ Documentation for HAL can be found in the [API docs](http://rdoc.info/github/apotonick/roar/Roar/Representer/JSON/HAL).
360
+
361
+ ### Hypermedia
362
+
363
+ Including the `Roar::JSON::HAL` module adds some more DSL methods to your module. It still allows using `::link` but treats them slightly different.
364
+
365
+ ```ruby
366
+ song.to_json
367
+ #=> {"title":"Roxanne","_links":{"self":{"href":"http://songs/Roxanne"}}}
368
+ ```
369
+
370
+ According to the HAL specification, links are now key with their `rel` attribute under the `_links` key.
371
+
372
+ Parsing works like-wise: Roar will use the same setters as before but it knows how to read HAL.
373
+
374
+ ### Nesting
375
+
376
+ Nested, or embedded, resources can be defined using the `:embedded` option.
377
+
378
+ ```ruby
379
+ module AlbumRepresenter
380
+ include Roar::JSON::HAL
381
+
382
+ property :title
383
+
384
+ collection :songs, class: Song, embedded: true do
385
+ property :title
386
+ end
387
+ end
388
+ ```
389
+
390
+ To embed a resource, you can use an inline representer or use `:extend` to specify the representer name.
391
+
392
+ ```ruby
393
+ album.to_json
394
+
395
+ #=> {"title":"True North","_embedded":{"songs":[{"title":"The Island"},{"title":"Changing Tide"}]}}
396
+ ```
397
+
398
+ HAL keys nested resources under the `_embedded` key and then by their type.
399
+
400
+ All HAL features in Roar are discussed in the [API docs](http://rdoc.info/github/apotonick/roar/Roar/Representer/JSON/HAL), including [array links](https://github.com/apotonick/roar/blob/master/lib/roar/json/hal.rb#L176).
401
+
402
+
403
+ ## JSON-API
404
+
405
+ Roar also supports [JSON-API](http://jsonapi.org/) - yay! It can render _and_ parse singular and collection documents.
406
+
407
+ ### Resource
408
+
409
+ A minimal representation can be defined as follows.
410
+
411
+ ```ruby
412
+ require 'roar/json/json_api'
413
+
414
+ module SongsRepresenter
415
+ include Roar::JSON::JsonApi
416
+ type :songs
417
+
418
+ property :id
419
+ property :title
420
+ end
421
+ ```
422
+
423
+ Properties of the represented model are defined in the root level.
424
+
425
+ ### Hypermedia
426
+
427
+ You can add links to `linked` models within the resource section.
428
+
429
+ ```ruby
430
+ module SongsRepresenter
431
+ # ...
432
+
433
+ has_one :composer
434
+ has_many :listeners
435
+ end
436
+ ```
437
+
438
+ Global `links` can be added using the familiar `::link` method (this is still WIP as the DSL is not final).
439
+
440
+ ```ruby
441
+ module SongsRepresenter
442
+ # ...
443
+
444
+ link "songs.album" do
445
+ {
446
+ type: "album",
447
+ href: "http://example.com/albums/{songs.album}"
448
+ }
449
+ end
450
+ end
451
+ ```
452
+
453
+ ### Compounds
454
+
455
+ To add compound models into the document, use `::compound`.
456
+
457
+ ```ruby
458
+ module SongsRepresenter
459
+ # ...
460
+
461
+ compound do
462
+ property :album do
463
+ property :id
464
+ property :title
465
+ end
466
+
467
+ collection :musicians do
468
+ property :name
469
+ end
470
+ end
471
+ ```
472
+
473
+ ### Meta Data
474
+
475
+ Meta data can be included into the rendered collection document in two ways. Please note that parsing the `meta` field is not implemented, yet, as I wasn't sure if people need it.
476
+
477
+ You can define meta data on your collection object and then let Roar compile it.
478
+
479
+ ```ruby
480
+ module SongsRepresenter
481
+ # ..
482
+
483
+ meta do
484
+ property :page
485
+ property :total
486
+ end
487
+ ```
488
+
489
+ Your collection object has to expose those methods.
490
+
491
+ ```ruby
492
+ collection.page #=> 1
493
+ collection.total #=> 12
494
+ ```
495
+
496
+ This will render the `{"meta": {"page": 1, "total": 12}}` hash into the JSON-API document.
497
+
498
+ Another way is to provide the _complete_ meta data hash when rendering. You must not define any `meta` properties in the representer when using this approach.
499
+
500
+ ```ruby
501
+ collection.to_json("meta" => {page: params["page"], total: collection.size})
502
+ ```
503
+
504
+ If you need more functionality (and parsing), please let us know.
505
+
506
+ ### Usage
507
+
508
+ As JSON-API per definition can represent singular models and collections you have two entry points.
509
+
510
+ ```ruby
511
+ SongsRepresenter.prepare(Song.find(1)).to_json
512
+ SongsRepresenter.prepare(Song.new).from_json("..")
513
+ ```
514
+
515
+ Singular models can use the representer module directly.
516
+
517
+ ```ruby
518
+ SongsRepresenter.for_collection.prepare([Song.find(1), Song.find(2)]).to_json
519
+ SongsRepresenter.for_collection.prepare([Song.new, Song.new]).from_json("..")
520
+ ```
521
+
522
+
523
+ Parsing currently works great with singular documents - for collections, we are still working out how to encode the application semantics. Feel free to help.
524
+
525
+
526
+ ## Collection+JSON
527
+
528
+ The [Collection+JSON media format](http://amundsen.com/media-types/collection/) defines document format and semantics for requests. It is currently experimental as we're still exploring how we optimize the support with Roar. Let us know if you're using it.
529
+
530
+ ```ruby
531
+ module SongRepresenter
532
+ include Roar::JSON::CollectionJSON
533
+ version "1.0"
534
+ href { "http://localhost/songs/" }
535
+
536
+ property :title
537
+
538
+ items(:class => Song) do
539
+ href { "//songs/#{title}" }
540
+
541
+ property :title, :prompt => "Song title"
542
+
543
+ link(:download) { "//songs/#{title}.mp3" }
544
+ end
545
+
546
+ template do
547
+ property :title, :prompt => "Song title"
548
+ end
549
+
550
+ queries do
551
+ link :search do
552
+ {:href => "//search", :data => [{:name => "q", :value => ""}]}
553
+ end
554
+ end
555
+ end
556
+ ```
557
+
558
+ It renders a document following the Collection+JSON specs.
559
+
560
+ ```
561
+ #=> {"collection":{
562
+ "template":{"data":[{"name":"title","value":null}]},
563
+ "queries":[{"rel":"search","href":"//search","data":[{"name":"q","value":""}]}],
564
+ "version":"1.0",
565
+ "href":"http://localhost/songs/",
566
+ "title":"Roxanne",
567
+ "items":null}}
568
+ ```
569
+
570
+ We have big plans with this media format, as the object model in Roar plays nicely with Collection+JSON's API semantics.
571
+
572
+
573
+ ## Client-Side Support
574
+
575
+ Being a bi-directional mapper that does rendering _and_ parsing, Roar representers are perfectly suitable for use in clients, too. In many projects, representers are shared as gems between server and client.
576
+
577
+ Consider the following shared representer.
578
+
579
+ ```ruby
580
+ module SongRepresenter
581
+ include Roar::JSON
582
+ include Roar::Hypermedia
583
+
584
+ property :title
585
+ property :id
586
+
587
+ link :self do
588
+ "http://songs/#{title}"
589
+ end
590
+ end
591
+ ```
592
+
593
+ In a client where you don't have access to the database it is common to use `OpenStruct` classes as domain objects.
594
+
595
+ ```ruby
596
+ require 'roar/client'
597
+
598
+ class Song < OpenStruct
599
+ include Roar::JSON
600
+ include SongRepresenter
601
+ include Roar::Client
602
+ end
603
+ ```
604
+
605
+ ## HTTP Support
606
+
607
+ The `Client` module mixes all necessary methods into the client class, e.g. it provides HTTP support
608
+
609
+ ```ruby
610
+ song = Song.new(title: "Roxanne")
611
+ song.post(uri: "http://localhost:4567/songs", as: "application/json")
612
+
613
+ song.id #=> 42
614
+ ```
615
+
616
+ What happens here?
617
+
618
+ * You're responsible for initializing the client object with attributes. This can happen with in the constructor or using accessors.
619
+ * `post` will use the included `SongRepresenter` to compile the document using `#to_json`.
620
+ * The document gets `POST`ed to the passed URL.
621
+ * If a document is returned, it is deserialized and the client's attributes are updated.
622
+
623
+ This is a very simple but efficient mechanism for working with representations in a client application.
624
+
625
+ Roar works with all HTTP request types, check out `GET`.
626
+
627
+ ```ruby
628
+ song = Client::Song.new
629
+ song.get(uri: "http://localhost:4567/songs/1", as: "application/json")
630
+
631
+ song.title #=> "Roxanne"
632
+ song.links[:self].href #=> http://localhost/songs/1
633
+ ```
634
+
635
+ As `GET` is not supposed to send any data, you can use `#get` on an empty object to populate it with the server data.
636
+
637
+ ### HTTPS
638
+
639
+ Roar supports SSL connections - they are automatically detected via the protocol.
640
+
641
+ ```ruby
642
+ song.get(uri: "https://localhost:4567/songs/1")`
643
+ ```
644
+
645
+ ### Basic Authentication
646
+
647
+ The HTTP verbs allow you to specify credentials for HTTP basic auth.
648
+
649
+ ```ruby
650
+ song.get(uri: "http://localhost:4567/songs/1", basic_auth: ["username", "secret_password"])
651
+
652
+ ```
653
+
654
+ ### Client SSL certificates
655
+
656
+ (Only currently supported with Net:Http)
657
+
658
+ ```ruby
659
+ song.get(uri: "http://localhost:4567/songs/1", pem_file: "/path/to/client/cert.pem", ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER)
660
+
661
+ ```
662
+
663
+ Note: ssl_verify_mode is not required and will default to ```OpenSSL::SSL::VERIFY_PEER)```
664
+
665
+
666
+
667
+ ### Request customization
668
+
669
+ All verbs yield the request object before the request is sent, allowing to modify it. It is a `Net::HTTP::Request` instance (unless you use Faraday).
670
+
671
+ ```ruby
672
+ song.get(uri: "http://localhost:4567/songs/1") do |req|
673
+ req.add_field("Cookie", "Yumyum")
674
+ end
675
+ ```
676
+
677
+ ### Error handling
678
+
679
+ In case of a non-2xx response status, `#get` and friends raise a `Roar::Transport::Error` exception. The original response can be accessed as follows.
680
+
681
+ ```ruby
682
+ song.get(uri: "http://localhost/songs1") # not-existing.
683
+ rescue Roar::Transport::Error => exception
684
+ exception.response.code #=> 404
685
+ ```
686
+
687
+ ## XML
688
+
689
+ Roar also comes with XML support.
690
+
691
+ ```ruby
692
+ module SongRepresenter
693
+ include Roar::Representer::XML
694
+ include Roar::Representer::Hypermedia
695
+
696
+ property :title
697
+ property :id
698
+
699
+ link :self do
700
+ "http://songs/#{title}"
701
+ end
702
+ end
703
+ ```
704
+
705
+ Include the `Roar::Representer::XML` engine and get bi-directional XML for your objects.
706
+
707
+ ```ruby
708
+ song = Song.new(title: "Roxanne", id: 42)
709
+ song.extend(XML::SongRepresenter)
710
+
711
+ song.to_xml
712
+ ```
713
+
714
+ Note that you now use `#to_xml` and `#from_xml`.
715
+
716
+ ```xml
717
+ <song>
718
+ <title>Roxanne</title>
719
+ <id>42</id>
720
+ <link rel="self" href="http://songs/Roxanne"/>
721
+ </song>
722
+ ```
723
+
724
+ Please consult the [representable XML documentation](https://github.com/apotonick/representable/#more-on-xml) for all its great features.
725
+
726
+
727
+ ## Support
728
+
729
+ Questions? Need help? Free 1st Level Support on irc.freenode.org#roar !
730
+ We also have a [mailing list](https://groups.google.com/forum/?fromgroups#!forum/roar-talk), yiha!
731
+
732
+ ## License
733
+
734
+ Roar is released under the [MIT License](http://www.opensource.org/licenses/MIT).