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,154 @@
1
+ require 'test_helper'
2
+ require 'roar/http_verbs'
3
+ require 'roar/json'
4
+
5
+ class HttpVerbsTest < MiniTest::Spec
6
+ BandRepresenter = Integration::BandRepresenter
7
+
8
+ # keep this class clear of Roar modules.
9
+ class Band
10
+ attr_accessor :name, :label
11
+
12
+ def label=(v) # in ruby 2.2, #label= is not there, all at sudden. what *is* that?
13
+ @label = v
14
+ end
15
+ end
16
+
17
+ let (:band) { OpenStruct.new(:name => "bodyjar").extend(Roar::HttpVerbs, BandRepresenter) }
18
+
19
+
20
+ describe "HttpVerbs" do
21
+ before do
22
+ @band = Band.new
23
+ @band.extend(BandRepresenter)
24
+ @band.extend(Roar::HttpVerbs)
25
+ end
26
+
27
+ describe "transport_engine" do
28
+ before do
29
+ @http_verbs = Roar::HttpVerbs
30
+ @net_http = Roar::Transport::NetHTTP
31
+ end
32
+
33
+ it "has a default set in the transport module level" do
34
+ assert_equal @net_http, @band.transport_engine
35
+ end
36
+
37
+ it "allows changing on instance level" do
38
+ @band.transport_engine = :soap
39
+ assert_equal @net_http, @http_verbs.transport_engine
40
+ assert_equal :soap, @band.transport_engine
41
+ end
42
+ end
43
+
44
+ describe "HttpVerbs.get" do
45
+ it "returns instance from incoming representation" do
46
+ band = @band.get(uri: "http://localhost:4567/bands/slayer", as: "application/json")
47
+ assert_equal "Slayer", band.name
48
+ assert_equal "Canadian Maple", band.label
49
+ end
50
+
51
+ # FIXME: move to faraday test.
52
+ require 'roar/transport/faraday'
53
+ describe 'a non-existent resource' do
54
+ it 'handles HTTP errors and raises a ResourceNotFound error with FaradayHttpTransport' do
55
+ @band.transport_engine = Roar::Transport::Faraday
56
+ assert_raises(::Faraday::Error::ResourceNotFound) do
57
+ @band.get(uri: 'http://localhost:4567/bands/anthrax', as: "application/json")
58
+ end
59
+ end
60
+
61
+ it 'returns Roar::Transport::Error for NetHttpTransport in case of non 20x' do
62
+ @band.transport_engine = Roar::Transport::NetHTTP
63
+
64
+ exception = assert_raises(Roar::Transport::Error) do
65
+ @band.get(uri: 'http://localhost:4567/bands/anthrax', as: "application/json")
66
+ end
67
+
68
+ exception.response.code.must_equal "404"
69
+ end
70
+ end
71
+ end
72
+
73
+ describe "#get" do
74
+ it "updates instance with incoming representation" do
75
+ @band.get(:uri => "http://localhost:4567/bands/slayer", :as => "application/json")
76
+ assert_equal ["Slayer", "Canadian Maple"], [@band.name, @band.label]
77
+ end
78
+ end
79
+
80
+ describe "#post" do
81
+ it "updates instance with incoming representation" do
82
+ @band.name = "Strung Out"
83
+ assert_equal nil, @band.label
84
+
85
+ @band.post(:uri => "http://localhost:4567/bands", :as => "application/xml")
86
+ assert_equal "STRUNG OUT", @band.name
87
+ assert_equal nil, @band.label
88
+ end
89
+ end
90
+
91
+ describe "#put" do
92
+ it "updates instance with incoming representation" do
93
+ @band.name = "Strung Out"
94
+ @band.label = "Fat Wreck"
95
+ @band.put(:uri => "http://localhost:4567/bands/strungout", :as => "application/xml")
96
+ assert_equal "STRUNG OUT", @band.name
97
+ assert_equal "FAT WRECK", @band.label
98
+ end
99
+ end
100
+
101
+ describe "#patch" do
102
+ it 'does something' do
103
+ @band.label = 'Fat Mike'
104
+ @band.patch(:uri => "http://localhost:4567/bands/strungout", :as => "application/xml")
105
+ assert_equal 'FAT MIKE', @band.label
106
+ end
107
+ end
108
+
109
+ describe "#delete" do
110
+ it 'does something' do
111
+ @band.delete(:uri => "http://localhost:4567/bands/metallica", :as => "application/xml")
112
+ end
113
+ end
114
+
115
+
116
+ describe "HTTPS and Authentication" do
117
+ let (:song) { OpenStruct.new(:name => "bodyjar").extend(Roar::HttpVerbs, BandRepresenter) }
118
+
119
+ describe "Basic Auth: passing manually" do
120
+
121
+ end
122
+
123
+ describe "HTTPS: passing manually" do
124
+ verbs do |verb|
125
+ it "allows #{verb}" do
126
+ song.send(verb, :uri => "https://localhost:8443/bands/bodyjar", :as => "application/json")
127
+
128
+ if verb == "delete"
129
+ song.name.must_equal "bodyjar"
130
+ else
131
+ song.name.must_equal "Bodyjar"
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "HTTPS+Basic Auth: passing manually" do
138
+ it "allows GET" do
139
+ song.get(:uri => "https://localhost:8443/protected", :as => "application/json", :basic_auth => [:admin, :password])
140
+
141
+ song.name.must_equal "Bodyjar"
142
+ end
143
+ end
144
+ end
145
+
146
+ describe "request customization" do
147
+ it "yields the request object" do
148
+ band.get(:uri => "http://localhost:4567/cookies", :as => "application/json") do |req|
149
+ req.add_field("Cookie", "Yumyum")
150
+ end.name.must_equal "Bodyjar"
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,155 @@
1
+ require 'test_helper'
2
+
3
+ class HypermediaTest < MiniTest::Spec
4
+
5
+ describe "Hypermedia Feature" do
6
+
7
+ let (:song) { Song.new(:title => "Brandy Wine") }
8
+
9
+ before do
10
+ @bookmarks = Class.new do
11
+ include AttributesConstructor
12
+ include Roar::XML
13
+ include Roar::Hypermedia
14
+
15
+ self.representation_wrap = "bookmarks"
16
+ end
17
+
18
+ @bookmarks_with_links = Class.new(@bookmarks)
19
+ @bookmarks_with_links.class_eval do
20
+ self.representation_wrap = "bookmarks"
21
+
22
+ property :id
23
+ link :self do "http://bookmarks" end
24
+ link :all do "http://bookmarks/all" end
25
+
26
+ attr_accessor :id, :self, :all
27
+ end
28
+ end
29
+
30
+
31
+ describe "#to_xml" do
32
+ it "works when no links defined" do
33
+ repr = Module.new do
34
+ include Roar::XML
35
+ include Roar::Hypermedia
36
+
37
+ self.representation_wrap = "song"
38
+ property :title
39
+ end
40
+
41
+ song.extend(repr).to_xml.must_equal_xml "<song><title>Brandy Wine</title></song>"
42
+ end
43
+
44
+ let (:rpr) { Module.new do
45
+ include Roar::XML
46
+ include Roar::Hypermedia
47
+
48
+ self.representation_wrap = "song"
49
+ property :title
50
+
51
+ link(:self) { "/songs/#{title}" }
52
+ link(:all) { "/songs" }
53
+ end }
54
+
55
+ it "includes links in rendered document" do
56
+ song.extend(rpr).to_xml.must_equal_xml %{
57
+ <song>
58
+ <title>Brandy Wine</title>
59
+ <link rel="self" href="/songs/Brandy Wine"/>
60
+ <link rel="all" href="/songs"/>
61
+ </song>}
62
+ end
63
+
64
+ it "suppresses links when links: false" do
65
+ song.extend(rpr).to_xml(:links => false).must_equal_xml "<song><title>Brandy Wine</title></song>"
66
+ end
67
+
68
+ it "renders nested links" do
69
+ song_rpr = rpr
70
+
71
+ album_rpr = Module.new do
72
+ include Roar::XML
73
+ include Roar::Hypermedia
74
+
75
+ self.representation_wrap = "album"
76
+ collection :songs, :extend => song_rpr
77
+
78
+ link(:self) { "/albums/mixed" }
79
+ end
80
+
81
+ Album.new(:songs => [song]).extend(album_rpr).to_xml.must_equal_xml %{
82
+ <album>
83
+ <song>
84
+ <title>Brandy Wine</title>
85
+ <link rel="self" href="/songs/Brandy Wine"/>
86
+ <link rel="all" href="/songs"/>
87
+ </song>
88
+ <link rel="self" href="/albums/mixed"/>
89
+ </album>
90
+ }
91
+ end
92
+ end
93
+
94
+ describe "#to_json" do
95
+ class Note
96
+ include Roar::JSON
97
+ include Roar::Hypermedia
98
+ link(:self) { "http://me" }
99
+ end
100
+
101
+ it "works twice" do
102
+ note = Note.new
103
+ assert_equal note.to_json, note.to_json
104
+ end
105
+
106
+ it "sets up links even when nested" do
107
+ class Page
108
+ include AttributesConstructor
109
+ include Roar::JSON
110
+ property :note, :class => Note
111
+ attr_accessor :note
112
+ end
113
+
114
+ assert_equal "{\"note\":{\"links\":[{\"rel\":\"self\",\"href\":\"http://me\"}]}}", Page.new(:note => Note.new).to_json
115
+ end
116
+ end
117
+
118
+
119
+
120
+ describe "#from_xml" do
121
+ it "extracts links from document" do
122
+ doc = @bookmarks_with_links.new.from_xml(%{
123
+ <bookmarks>
124
+ <link rel="self" href="http://bookmarks">
125
+ </bookmarks>
126
+ })
127
+
128
+ assert_kind_of Roar::Hypermedia::LinkCollection, doc.links
129
+ assert_equal 1, doc.links.size
130
+ assert_equal(["self", "http://bookmarks"], [doc.links["self"].rel, doc.links["self"].href])
131
+ end
132
+
133
+ it "sets up an empty link list if no links found in the document" do
134
+ @bookmarks_with_links.new.from_xml(%{<bookmarks/>}).links.must_equal nil
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+
141
+ class LinkCollectionTest < MiniTest::Spec
142
+ describe "LinkCollection" do
143
+ subject {
144
+ Roar::Hypermedia::LinkCollection[
145
+ @self_link = link(:rel => :self), @next_link = link(:rel => :next)
146
+ ]
147
+ }
148
+
149
+ describe "::[]" do
150
+ it "keys by using rel string" do
151
+ subject.values.must_equal [@self_link, @next_link]
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,190 @@
1
+ require 'test_helper'
2
+
3
+ class HypermediaTest < MiniTest::Spec
4
+ describe "inheritance" do
5
+ before do
6
+ module BaseRepresenter
7
+ include Roar::JSON
8
+ include Roar::Hypermedia
9
+
10
+ link(:base) { "http://base" }
11
+ end
12
+
13
+ module Bar
14
+ include Roar::JSON
15
+ include Roar::Hypermedia
16
+
17
+ link(:bar) { "http://bar" }
18
+ end
19
+
20
+ module Foo
21
+ include Roar::JSON
22
+ include Roar::Hypermedia
23
+ include BaseRepresenter
24
+ include Bar
25
+
26
+ link(:foo) { "http://foo" }
27
+ end
28
+ end
29
+
30
+ it "inherits parent links" do
31
+ foo = Object.new.extend(Foo)
32
+
33
+ assert_equal "{\"links\":[{\"rel\":\"base\",\"href\":\"http://base\"},{\"rel\":\"bar\",\"href\":\"http://bar\"},{\"rel\":\"foo\",\"href\":\"http://foo\"}]}", foo.to_json
34
+ end
35
+
36
+ it "inherits links from all mixed-in representers" do
37
+ skip
38
+ Object.new.extend(BaseRepresenter).extend(Bar).to_json.must_equal "{\"links\":[{\"rel\":\"base\",\"href\":\"http://base\"},{\"rel\":\"bar\",\"href\":\"http://bar\"}]}"
39
+ end
40
+ end
41
+
42
+ describe "#links_array" do
43
+ subject { Object.new.extend(rpr) }
44
+
45
+ representer_for do
46
+ link(:self) { "//self" }
47
+ end
48
+
49
+
50
+ describe "#to_json" do
51
+ it "renders" do
52
+ subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\"}]}"
53
+ end
54
+ end
55
+
56
+ describe "#from_json" do
57
+ it "parses" do
58
+ subject.from_json "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\"}]}"
59
+ subject.links.must_equal({"self" => link("rel" => "self", "href" => "//self")})
60
+ end
61
+ end
62
+
63
+
64
+ describe "#link" do
65
+
66
+ describe "with any options" do
67
+ representer_for do
68
+ link(:rel => :self, :title => "Hey, @myabc") { "//self" }
69
+ end
70
+
71
+ it "renders options" do
72
+ subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"title\":\"Hey, @myabc\",\"href\":\"//self\"}]}"
73
+ end
74
+ end
75
+
76
+ describe "with string rel" do
77
+ representer_for do
78
+ link("ns:self") { "//self" }
79
+ end
80
+
81
+ it "renders rel" do
82
+ subject.to_json.must_equal "{\"links\":[{\"rel\":\"ns:self\",\"href\":\"//self\"}]}"
83
+ end
84
+ end
85
+
86
+ describe "passing options to serialize" do
87
+ representer_for do
88
+ link(:self) { |opts| "//self/#{opts[:id]}" }
89
+ end
90
+
91
+ it "receives options when rendering" do
92
+ subject.to_json(:id => 1).must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self/1\"}]}"
93
+ end
94
+
95
+ describe "in a composition" do
96
+ representer_for do
97
+ property :entity, :extend => self
98
+ link(:self) { |opts| "//self/#{opts[:id]}" }
99
+ end
100
+
101
+ it "propagates options" do
102
+ Song.new(:entity => Song.new).extend(rpr).to_json(:id => 1).must_equal "{\"entity\":{\"links\":[{\"rel\":\"self\",\"href\":\"//self/1\"}]},\"links\":[{\"rel\":\"self\",\"href\":\"//self/1\"}]}"
103
+ end
104
+ end
105
+ end
106
+
107
+ describe "returning option hash from block" do
108
+ representer_for do
109
+ link(:self) do {:href => "//self", :type => "image/jpg"} end
110
+ link(:other) do |params|
111
+ hash = { :href => "//other" }
112
+ hash.merge!(:type => 'image/jpg') if params[:type]
113
+ hash
114
+ end
115
+ end
116
+
117
+ it "is rendered as link attributes" do
118
+ subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\",\"type\":\"image/jpg\"},{\"rel\":\"other\",\"href\":\"//other\"}]}"
119
+ end
120
+
121
+ it "is rendered according to context" do
122
+ subject.to_json(type: true).must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\",\"type\":\"image/jpg\"},{\"rel\":\"other\",\"href\":\"//other\",\"type\":\"image/jpg\"}]}"
123
+ subject.to_json.must_equal "{\"links\":[{\"rel\":\"self\",\"href\":\"//self\",\"type\":\"image/jpg\"},{\"rel\":\"other\",\"href\":\"//other\"}]}"
124
+ end
125
+ end
126
+
127
+ describe "not calling #link" do
128
+ representer_for {}
129
+
130
+ it "still allows rendering" do
131
+ subject.to_json.must_equal "{}"
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ class HyperlinkTest < MiniTest::Spec
139
+ describe "Hyperlink" do
140
+ subject { link(:rel => "self", "href" => "http://self", "data-whatever" => "Hey, @myabc") }
141
+
142
+ it "accepts string keys in constructor" do
143
+ assert_equal "Hey, @myabc", subject.send("data-whatever")
144
+ end
145
+
146
+ it "responds to #rel" do
147
+ assert_equal "self", subject.rel
148
+ end
149
+
150
+ it "responds to #href" do
151
+ assert_equal "http://self", subject.href
152
+ end
153
+
154
+ it "responds to #replace with string keys" do
155
+ subject.replace("rel" => "next")
156
+ assert_equal nil, subject.href
157
+ assert_equal "next", subject.rel
158
+ end
159
+
160
+ it "responds to #each and implements Enumerable" do
161
+ assert_equal ["rel:self", "href:http://self", "data-whatever:Hey, @myabc"], subject.collect { |k,v| "#{k}:#{v}" }
162
+ end
163
+ end
164
+
165
+ describe "Config inheritance" do
166
+ it "doesn't mess up with inheritable_array" do # FIXME: remove this test when uber is out.
167
+ OpenStruct.new.extend( Module.new do
168
+ include Roar::JSON
169
+ include(Module.new do
170
+ include Roar::JSON
171
+ include Roar::Hypermedia
172
+
173
+ property :bla
174
+
175
+ link( :self) {"bo"}
176
+
177
+ #puts "hey ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
178
+ #puts representable_attrs.inheritable_array(:links).inspect
179
+ end)
180
+
181
+
182
+ #puts representable_attrs.inheritable_array(:links).inspect
183
+
184
+ property :blow
185
+ include Roar::Hypermedia
186
+ link(:bla) { "boo" }
187
+ end).to_hash.must_equal({"links"=>[{"rel"=>"self", "href"=>"bo"}, {"rel"=>"bla", "href"=>"boo"}]})
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "sinatra"
4
+ gem "roar", :path => "../.."
@@ -0,0 +1,8 @@
1
+ module Integration
2
+ module BandRepresenter
3
+ include Roar::JSON
4
+
5
+ property :name
6
+ property :label
7
+ end
8
+ end
@@ -0,0 +1,42 @@
1
+ require "integration/band_representer"
2
+ # require 'sinatra/runner' # TODO: merge that into sinatra-contrib.
3
+ require 'lib/runner'
4
+
5
+ class ServerRunner < Sinatra::Runner
6
+ def app_file
7
+ File.expand_path("../server.rb", __FILE__)
8
+ end
9
+
10
+ def command
11
+ "bundle exec ruby #{app_file} -p #{port} -e production"
12
+ end
13
+
14
+ def ping_path # to be overwritten
15
+ '/ping'
16
+ end
17
+ end
18
+
19
+ class SslServerRunner < ServerRunner
20
+ def command
21
+ "bundle exec ruby #{File.expand_path("../ssl_server.rb", __FILE__)}"
22
+ end
23
+
24
+ def port
25
+ 8443
26
+ end
27
+
28
+ def protocol
29
+ "https"
30
+ end
31
+ end
32
+
33
+ runner = ServerRunner.new
34
+ runner.run
35
+
36
+ ssl_runner = SslServerRunner.new
37
+ ssl_runner.run
38
+
39
+ Minitest.after_run do
40
+ runner.kill
41
+ ssl_runner.kill
42
+ end
@@ -0,0 +1,120 @@
1
+ require "bundler/setup"
2
+ require "sinatra"
3
+ require "ostruct"
4
+ require "roar/json"
5
+ require "sinatra/multi_route"
6
+
7
+ require File.expand_path("../band_representer.rb", __FILE__)
8
+
9
+ class Band
10
+ attr_reader :name, :label
11
+
12
+ def name=(value)
13
+ @name = value.upcase
14
+ end
15
+
16
+ def label=(value)
17
+ @label = value.upcase
18
+ end
19
+ end
20
+
21
+ def consume_band
22
+ Band.new.
23
+ extend(Integration::BandRepresenter).
24
+ from_json(request.body.read)
25
+ end
26
+
27
+ get "/method" do
28
+ "<method>get</method>"
29
+ end
30
+
31
+ post "/method" do
32
+ "<method>post - #{request.body.read}</method>"
33
+ end
34
+
35
+ put "/method" do
36
+ "<method>put - #{request.body.read}</method>"
37
+ end
38
+
39
+ delete "/method" do
40
+ "<method>delete</method>"
41
+ end
42
+
43
+ patch "/method" do
44
+ "<method>patch - #{request.body.read}</method>"
45
+ end
46
+
47
+ get '/deliberate-error' do
48
+ raise 'this error was deliberate'
49
+ end
50
+
51
+ post "/bands" do
52
+ #if request.content_type =~ /xml/
53
+ body consume_band.to_json
54
+
55
+ status 201
56
+ end
57
+
58
+ put "/bands/strungout" do
59
+ # DISCUSS: as long as we don't agree on what to return in PUT/PATCH, let's return an updated document.
60
+ body consume_band.to_json
61
+ #status 204
62
+ end
63
+
64
+ patch '/bands/strungout' do
65
+ # DISCUSS: as long as we don't agree on what to return in PUT/PATCH, let's return an updated document.
66
+ body consume_band.to_json
67
+ #status 204
68
+ end
69
+
70
+ get "/bands/slayer" do
71
+ OpenStruct.new(:name => "Slayer", :label => "Canadian Maple").
72
+ extend(Integration::BandRepresenter).
73
+ to_json
74
+ end
75
+
76
+ delete '/bands/metallica' do
77
+ status 204
78
+ end
79
+
80
+ route :get, :delete, :patch, :post, :put, %r{/bands/nirvana/status([\d]*)_and_data} do
81
+ status params[:captures]
82
+ OpenStruct.new(:name => "Nirvana", :label => "Sub Pop").
83
+ extend(Integration::BandRepresenter).
84
+ to_json
85
+ end
86
+
87
+ route :get, :delete, :patch, :post, :put, %r{/bands/nirvana/status([\d]*)_no_data} do
88
+ status params[:captures]
89
+ end
90
+
91
+
92
+ helpers do
93
+ def protected!
94
+ return if authorized?
95
+ headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
96
+ halt 401, "Not authorized\n"
97
+ end
98
+
99
+ def authorized?
100
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
101
+ @auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['admin', 'password']
102
+ end
103
+ end
104
+
105
+ route :get, :post, :put, :delete, "/protected/bands/bodyjar" do
106
+ protected!
107
+
108
+ OpenStruct.new(:name => "Bodyjar").
109
+ extend(Integration::BandRepresenter).
110
+ to_json
111
+ end
112
+
113
+ route :get, :post, :put, :delete, "/cookies" do
114
+ raise "No cookies!" unless request.env["HTTP_COOKIE"] == "Yumyum"
115
+ %{{"name": "Bodyjar"}}
116
+ end
117
+
118
+ get "/ping" do
119
+ "1"
120
+ end