ruby-oembed 0.7.6 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +4 -1
  2. data/CHANGELOG.rdoc +49 -0
  3. data/Gemfile.lock +13 -8
  4. data/README.rdoc +79 -0
  5. data/Rakefile +21 -6
  6. data/lib/oembed/errors.rb +23 -4
  7. data/lib/oembed/formatter/json/backends/activesupportjson.rb +28 -0
  8. data/lib/oembed/formatter/json/backends/jsongem.rb +31 -0
  9. data/lib/oembed/formatter/json/backends/yaml.rb +85 -0
  10. data/lib/oembed/formatter/json.rb +69 -0
  11. data/lib/oembed/formatter/xml/backends/rexml.rb +44 -0
  12. data/lib/oembed/formatter/xml/backends/xmlsimple.rb +39 -0
  13. data/lib/oembed/formatter/xml.rb +76 -0
  14. data/lib/oembed/formatter.rb +97 -0
  15. data/lib/oembed/provider.rb +102 -38
  16. data/lib/oembed/provider_discovery.rb +19 -7
  17. data/lib/oembed/providers/embedly_urls.yml +487 -0
  18. data/lib/oembed/providers/oohembed_urls.yml +17 -0
  19. data/lib/oembed/providers.rb +68 -11
  20. data/lib/oembed/response/link.rb +14 -0
  21. data/lib/oembed/response/photo.rb +12 -0
  22. data/lib/oembed/response/rich.rb +11 -0
  23. data/lib/oembed/response/video.rb +10 -0
  24. data/lib/oembed/response.rb +58 -10
  25. data/lib/oembed/version.rb +18 -0
  26. data/lib/oembed.rb +2 -1
  27. data/lib/tasks/oembed.rake +45 -0
  28. data/lib/tasks/rspec.rake +5 -0
  29. data/ruby-oembed.gemspec +38 -17
  30. data/spec/formatter/json/.DS_Store +0 -0
  31. data/spec/formatter/json/jsongem_backend_spec.rb +34 -0
  32. data/spec/formatter/json/yaml_backend_spec.rb +30 -0
  33. data/spec/formatter/xml/rexml_backend_spec.rb +30 -0
  34. data/spec/formatter/xml/xmlsimple_backend_spec.rb +34 -0
  35. data/spec/formatter_spec.rb +35 -0
  36. data/spec/provider_spec.rb +189 -24
  37. data/spec/providers_spec.rb +20 -1
  38. data/spec/response_spec.rb +129 -48
  39. data/spec/spec_helper.rb +5 -6
  40. metadata +45 -38
  41. data/CHANGELOG.md +0 -29
  42. data/README.md +0 -50
  43. data/VERSION +0 -1
  44. data/lib/oembed/embedly_urls.json +0 -227
  45. data/lib/oembed/formatters.rb +0 -40
  46. data/rails/init.rb +0 -3
@@ -0,0 +1,30 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe "OEmbed::Formatter::XML::Backends::REXML" do
4
+ include OEmbedSpecHelper
5
+
6
+ before(:all) do
7
+ lambda {
8
+ OEmbed::Formatter::XML.backend = 'REXML'
9
+ }.should_not raise_error
10
+
11
+ (!!defined?(REXML)).should == true
12
+ end
13
+
14
+ it "should support XML" do
15
+ proc { OEmbed::Formatter.supported?(:xml) }.
16
+ should_not raise_error(OEmbed::FormatNotSupported)
17
+ end
18
+
19
+ it "should be using the XmlSimple backend" do
20
+ OEmbed::Formatter::XML.backend.should == OEmbed::Formatter::XML::Backends::REXML
21
+ end
22
+
23
+ it "should decode an XML String" do
24
+ decoded = OEmbed::Formatter.decode(:xml, valid_response(:xml))
25
+ # We need to compare keys & values separately because we don't expect all
26
+ # non-string values to be recognized correctly.
27
+ decoded.keys.should == valid_response(:object).keys
28
+ decoded.values.map{|v|v.to_s}.should == valid_response(:object).values.map{|v|v.to_s}
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe "OEmbed::Formatter::XML::Backends::XmlSimple" do
4
+ include OEmbedSpecHelper
5
+
6
+ before(:all) do
7
+ lambda {
8
+ OEmbed::Formatter::XML.backend = 'XmlSimple'
9
+ }.should raise_error(LoadError)
10
+
11
+ require 'xmlsimple'
12
+
13
+ lambda {
14
+ OEmbed::Formatter::XML.backend = 'XmlSimple'
15
+ }.should_not raise_error
16
+ end
17
+
18
+ it "should support XML" do
19
+ proc { OEmbed::Formatter.supported?(:xml) }.
20
+ should_not raise_error(OEmbed::FormatNotSupported)
21
+ end
22
+
23
+ it "should be using the XmlSimple backend" do
24
+ OEmbed::Formatter::XML.backend.should == OEmbed::Formatter::XML::Backends::XmlSimple
25
+ end
26
+
27
+ it "should decode an XML String" do
28
+ decoded = OEmbed::Formatter.decode(:xml, valid_response(:xml))
29
+ # We need to compare keys & values separately because we don't expect all
30
+ # non-string values to be recognized correctly.
31
+ decoded.keys.should == valid_response(:object).keys
32
+ decoded.values.map{|v|v.to_s}.should == valid_response(:object).values.map{|v|v.to_s}
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe OEmbed::Formatter do
4
+ include OEmbedSpecHelper
5
+
6
+ it "should support JSON" do
7
+ proc { OEmbed::Formatter.support?(:json) }.
8
+ should_not raise_error(OEmbed::FormatNotSupported)
9
+ end
10
+
11
+ it "should default to JSON" do
12
+ OEmbed::Formatter.default.should == 'json'
13
+ end
14
+
15
+ it "should decode a JSON String" do
16
+ decoded = OEmbed::Formatter.decode(:json, valid_response(:json))
17
+ # We need to compare keys & values separately because we don't expect all
18
+ # non-string values to be recognized correctly.
19
+ decoded.keys.should == valid_response(:object).keys
20
+ decoded.values.map{|v|v.to_s}.should == valid_response(:object).values.map{|v|v.to_s}
21
+ end
22
+
23
+ it "should support XML" do
24
+ proc { OEmbed::Formatter.support?(:xml) }.
25
+ should_not raise_error(OEmbed::FormatNotSupported)
26
+ end
27
+
28
+ it "should decode an XML String" do
29
+ decoded = OEmbed::Formatter.decode(:xml, valid_response(:xml))
30
+ # We need to compare keys & values separately because we don't expect all
31
+ # non-string values to be recognized correctly.
32
+ decoded.keys.should == valid_response(:object).keys
33
+ decoded.values.map{|v|v.to_s}.should == valid_response(:object).values.map{|v|v.to_s}
34
+ end
35
+ end
@@ -4,7 +4,7 @@ describe OEmbed::Provider do
4
4
  include OEmbedSpecHelper
5
5
 
6
6
  before(:all) do
7
- @default = OEmbed::Formatters::DEFAULT
7
+ @default = OEmbed::Formatter.default
8
8
  @flickr = OEmbed::Provider.new("http://www.flickr.com/services/oembed/")
9
9
  @qik = OEmbed::Provider.new("http://qik.com/api/oembed.{format}", :xml)
10
10
  @viddler = OEmbed::Provider.new("http://lab.viddler.com/services/oembed/", :json)
@@ -15,7 +15,143 @@ describe OEmbed::Provider do
15
15
  @viddler << "http://*.viddler.com/*"
16
16
  end
17
17
 
18
- it "should by default use OEmbed::Formatters::DEFAULT" do
18
+ it "should require a valid endpoint for a new instance" do
19
+ proc { OEmbed::Provider.new("http://foo.com/oembed/") }.
20
+ should_not raise_error(ArgumentError)
21
+
22
+ proc { OEmbed::Provider.new("https://foo.com/oembed/") }.
23
+ should_not raise_error(ArgumentError)
24
+ end
25
+
26
+ it "should allow a {format} string in the endpoint for a new instance" do
27
+ proc { OEmbed::Provider.new("http://foo.com/oembed.{format}/get") }.
28
+ should_not raise_error(ArgumentError)
29
+ end
30
+
31
+ it "should raise an ArgumentError given an invalid endpoint for a new instance" do
32
+ [
33
+ "httpx://foo.com/oembed/",
34
+ "ftp://foo.com/oembed/",
35
+ "foo.com/oembed/",
36
+ "http://not a uri",
37
+ nil, 1,
38
+ ].each do |endpoint|
39
+ #puts "given a endpoint of #{endpoint.inspect}"
40
+ proc { OEmbed::Provider.new(endpoint) }.
41
+ should raise_error(ArgumentError)
42
+ end
43
+ end
44
+
45
+ it "should allow no URI schema to be given" do
46
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
47
+
48
+ provier.include?("http://foo.com/1").should be_true
49
+ provier.include?("http://bar.foo.com/1").should be_true
50
+ provier.include?("http://bar.foo.com/show/1").should be_true
51
+ provier.include?("https://bar.foo.com/1").should be_true
52
+ provier.include?("http://asdf.com/1").should be_true
53
+ provier.include?("asdf").should be_true
54
+ end
55
+
56
+ it "should allow a String as a URI schema" do
57
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
58
+ provier << "http://bar.foo.com/*"
59
+
60
+ provier.include?("http://bar.foo.com/1").should be_true
61
+ provier.include?("http://bar.foo.com/show/1").should be_true
62
+
63
+ provier.include?("https://bar.foo.com/1").should be_false
64
+ provier.include?("http://foo.com/1").should be_false
65
+ end
66
+
67
+ it "should allow multiple path wildcards in a String URI schema" do
68
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
69
+ provier << "http://bar.foo.com/*/show/*"
70
+
71
+ provier.include?("http://bar.foo.com/photo/show/1").should be_true
72
+ provier.include?("http://bar.foo.com/video/show/2").should be_true
73
+ provier.include?("http://bar.foo.com/help/video/show/2").should be_true
74
+
75
+ provier.include?("https://bar.foo.com/photo/show/1").should be_false
76
+ provier.include?("http://foo.com/video/show/2").should be_false
77
+ provier.include?("http://bar.foo.com/show/1").should be_false
78
+ provier.include?("http://bar.foo.com/1").should be_false
79
+ end
80
+
81
+ it "should NOT allow multiple domain wildcards in a String URI schema" do
82
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
83
+
84
+ pending("We don't yet validate URL schema strings") do
85
+ proc { provier << "http://*.com/*" }.
86
+ should raise_error(ArgumentError)
87
+ end
88
+
89
+ provier.include?("http://foo.com/1").should be_false
90
+ end
91
+
92
+ it "should allow a sub-domain wildcard in String URI schema" do
93
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
94
+ provier << "http://*.foo.com/*"
95
+
96
+ provier.include?("http://bar.foo.com/1").should be_true
97
+ provier.include?("http://foo.foo.com/2").should be_true
98
+ provier.include?("http://foo.com/3").should be_true
99
+
100
+ provier.include?("https://bar.foo.com/1").should be_false
101
+ provier.include?("http://my.bar.foo.com/1").should be_false
102
+
103
+ provier << "http://my.*.foo.com/*"
104
+ end
105
+
106
+ it "should allow multiple sub-domain wildcards in a String URI schema" do
107
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
108
+ provier << "http://*.my.*.foo.com/*"
109
+
110
+ provier.include?("http://my.bar.foo.com/1").should be_true
111
+ provier.include?("http://my.foo.com/2").should be_true
112
+ provier.include?("http://bar.my.bar.foo.com/3").should be_true
113
+
114
+ provier.include?("http://bar.foo.com/1").should be_false
115
+ provier.include?("http://foo.bar.foo.com/1").should be_false
116
+ end
117
+
118
+ it "should NOT allow a scheme wildcard in a String URI schema" do
119
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
120
+
121
+ pending("We don't yet validate URL schema strings") do
122
+ proc { provier << "*://foo.com/*" }.
123
+ should raise_error(ArgumentError)
124
+ end
125
+
126
+ provier.include?("http://foo.com/1").should be_false
127
+ end
128
+
129
+ it "should allow a scheme other than http in a String URI schema" do
130
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
131
+ provier << "https://foo.com/*"
132
+
133
+ provier.include?("https://foo.com/1").should be_true
134
+
135
+ gopher_url = "gopher://foo.com/1"
136
+ provier.include?(gopher_url).should be_false
137
+ provier << "gopher://foo.com/*"
138
+ provier.include?(gopher_url).should be_true
139
+ end
140
+
141
+ it "should allow a Regexp as a URI schema" do
142
+ provier = OEmbed::Provider.new("http://foo.com/oembed")
143
+ provier << %r{^https?://([^\.]*\.)?foo.com/(show/)?\d+}
144
+
145
+ provier.include?("http://bar.foo.com/1").should be_true
146
+ provier.include?("http://bar.foo.com/show/1").should be_true
147
+ provier.include?("http://foo.com/1").should be_true
148
+ provier.include?("https://bar.foo.com/1").should be_true
149
+
150
+ provier.include?("http://bar.foo.com/video/1").should be_false
151
+ provier.include?("gopher://foo.com/1").should be_false
152
+ end
153
+
154
+ it "should by default use OEmbed::Formatter.default" do
19
155
  @flickr.format.should == @default
20
156
  end
21
157
 
@@ -27,8 +163,24 @@ describe OEmbed::Provider do
27
163
  @viddler.format.should == :json
28
164
  end
29
165
 
30
- it "should not allow random formats" do
31
- proc { OEmbed::Provider.new("http://www.hulu.com/api/oembed.{format}", :yml) }.
166
+ it "should allow random formats on initialization" do
167
+ proc {
168
+ yaml_provider = OEmbed::Provider.new("http://foo.com/api/oembed.{format}", :yml)
169
+ yaml_provider << "http://foo.com/*"
170
+ }.
171
+ should_not raise_error
172
+ end
173
+
174
+ it "should not allow random formats to be parsed" do
175
+ yaml_provider = OEmbed::Provider.new("http://foo.com/api/oembed.{format}", :yml)
176
+ yaml_provider << "http://foo.com/*"
177
+ yaml_url = "http://foo.com/video/1"
178
+
179
+ yaml_provider.should_receive(:raw).
180
+ with(yaml_url, {:format=>:yml}).
181
+ and_return(valid_response(:json))
182
+
183
+ proc { yaml_provider.get(yaml_url) }.
32
184
  should raise_error(OEmbed::FormatNotSupported)
33
185
  end
34
186
 
@@ -43,32 +195,28 @@ describe OEmbed::Provider do
43
195
  @qik.include?(example_url(:qik)).should be_true
44
196
  end
45
197
 
46
- it "should detect if the format is in the URL" do
47
- @flickr.format_in_url?.should be_false
48
- @qik.format_in_url?.should be_true
49
- end
50
-
51
198
  it "should raise error if the URL is invalid" do
52
- proc{ @flickr.build(example_url(:fake)) }.should raise_error(OEmbed::NotFound)
53
- proc{ @qik.build(example_url(:fake)) }.should raise_error(OEmbed::NotFound)
199
+ proc{ @flickr.send(:build, example_url(:fake)) }.should raise_error(OEmbed::NotFound)
200
+ proc{ @qik.send(:build, example_url(:fake)) }.should raise_error(OEmbed::NotFound)
54
201
  end
55
202
 
56
203
  describe "#build" do
57
204
  it "should return a proper URL" do
58
- uri = @flickr.build(example_url(:flickr))
205
+ uri = @flickr.send(:build, example_url(:flickr))
59
206
  uri.host.should == "www.flickr.com"
60
207
  uri.path.should == "/services/oembed/"
61
208
  uri.query.include?("format=#{@flickr.format}").should be_true
62
209
  uri.query.include?("url=http://flickr.com/photos/bees/2362225867/").should be_true
63
210
 
64
- uri = @qik.build(example_url(:qik))
211
+ uri = @qik.send(:build, example_url(:qik))
65
212
  uri.host.should == "qik.com"
66
213
  uri.path.should == "/api/oembed.xml"
214
+ uri.query.include?("format=#{@qik.format}").should be_false
67
215
  uri.query.should == "url=http://qik.com/video/49565"
68
216
  end
69
217
 
70
218
  it "should accept parameters" do
71
- uri = @flickr.build(example_url(:flickr),
219
+ uri = @flickr.send(:build, example_url(:flickr),
72
220
  :maxwidth => 600,
73
221
  :maxheight => 200,
74
222
  :format => :xml,
@@ -81,7 +229,7 @@ describe OEmbed::Provider do
81
229
  end
82
230
 
83
231
  it "should build correctly when format is in the endpoint URL" do
84
- uri = @qik.build(example_url(:qik), :format => :json)
232
+ uri = @qik.send(:build, example_url(:qik), :format => :json)
85
233
  uri.path.should == "/api/oembed.json"
86
234
  end
87
235
  end
@@ -95,7 +243,7 @@ describe OEmbed::Provider do
95
243
  end
96
244
  Net::HTTP.stub!(:start).and_return(res)
97
245
 
98
- @flickr.raw(example_url(:flickr)).should == "raw content"
246
+ @flickr.send(:raw, example_url(:flickr)).should == "raw content"
99
247
  end
100
248
 
101
249
  it "should raise error on 501" do
@@ -103,7 +251,7 @@ describe OEmbed::Provider do
103
251
  Net::HTTP.stub!(:start).and_return(res)
104
252
 
105
253
  proc do
106
- @flickr.raw(example_url(:flickr))
254
+ @flickr.send(:raw, example_url(:flickr))
107
255
  end.should raise_error(OEmbed::UnknownFormat)
108
256
  end
109
257
 
@@ -112,7 +260,7 @@ describe OEmbed::Provider do
112
260
  Net::HTTP.stub!(:start).and_return(res)
113
261
 
114
262
  proc do
115
- @flickr.raw(example_url(:flickr))
263
+ @flickr.send(:raw, example_url(:flickr))
116
264
  end.should raise_error(OEmbed::NotFound)
117
265
  end
118
266
 
@@ -124,7 +272,7 @@ describe OEmbed::Provider do
124
272
  Net::HTTP.stub!(:start).and_return(r)
125
273
 
126
274
  proc do
127
- @flickr.raw(example_url(:flickr))
275
+ @flickr.send(:raw, example_url(:flickr))
128
276
  end.should raise_error(OEmbed::UnknownResponse)
129
277
  end
130
278
  end
@@ -149,6 +297,28 @@ describe OEmbed::Provider do
149
297
  @flickr.get(example_url(:flickr), :format=>:yml)
150
298
  end.should raise_error(OEmbed::FormatNotSupported)
151
299
  end
300
+
301
+ it "should return OEmbed::Response" do
302
+ @flickr.stub!(:raw).and_return(valid_response(@default))
303
+ @flickr.get(example_url(:flickr)).should be_a(OEmbed::Response)
304
+ end
305
+
306
+ it "should be calling OEmbed::Response#create_for internally" do
307
+ @flickr.stub!(:raw).and_return(valid_response(@default))
308
+ OEmbed::Response.should_receive(:create_for).
309
+ with(valid_response(@default), @flickr, example_url(:flickr), @default.to_s)
310
+ @flickr.get(example_url(:flickr))
311
+
312
+ @qik.stub!(:raw).and_return(valid_response(:xml))
313
+ OEmbed::Response.should_receive(:create_for).
314
+ with(valid_response(:xml), @qik, example_url(:qik), 'xml')
315
+ @qik.get(example_url(:qik))
316
+
317
+ @viddler.stub!(:raw).and_return(valid_response(:json))
318
+ OEmbed::Response.should_receive(:create_for).
319
+ with(valid_response(:json), @viddler, example_url(:viddler), 'json')
320
+ @viddler.get(example_url(:viddler))
321
+ end
152
322
 
153
323
  it "should send the provider's format if none is specified" do
154
324
  @flickr.should_receive(:raw).
@@ -166,10 +336,5 @@ describe OEmbed::Provider do
166
336
  and_return(valid_response(:json))
167
337
  @viddler.get(example_url(:viddler))
168
338
  end
169
-
170
- it "should return OEmbed::Response" do
171
- @flickr.stub!(:raw).and_return(valid_response(@default))
172
- @flickr.get(example_url(:flickr)).is_a?(OEmbed::Response).should be_true
173
- end
174
339
  end
175
340
  end
@@ -12,7 +12,13 @@ describe OEmbed::Providers do
12
12
  @qik << "http://qik.com/*"
13
13
  end
14
14
 
15
+ after(:each) do
16
+ OEmbed::Providers.unregister_all
17
+ end
18
+
15
19
  it "should register providers" do
20
+ OEmbed::Providers.urls.should be_empty
21
+
16
22
  OEmbed::Providers.register(@flickr, @qik)
17
23
 
18
24
  OEmbed::Providers.urls.keys.should == @flickr.urls + @qik.urls
@@ -29,11 +35,15 @@ describe OEmbed::Providers do
29
35
  end
30
36
 
31
37
  it "should find by URLs" do
38
+ OEmbed::Providers.register(@flickr, @qik) # tested in "should register providers"
39
+
32
40
  OEmbed::Providers.find(example_url(:flickr)).should == @flickr
33
41
  OEmbed::Providers.find(example_url(:qik)).should == @qik
34
42
  end
35
43
 
36
44
  it "should unregister providers" do
45
+ OEmbed::Providers.register(@flickr, @qik) # tested in "should register providers"
46
+
37
47
  OEmbed::Providers.unregister(@flickr)
38
48
 
39
49
  @flickr.urls.each do |regexp|
@@ -56,7 +66,7 @@ describe OEmbed::Providers do
56
66
  @qik.urls.should include(regexp)
57
67
  end
58
68
 
59
- OEmbed::Providers.register(@qik_mirror)
69
+ OEmbed::Providers.register(@qik, @qik_mirror)
60
70
 
61
71
  OEmbed::Providers.urls.keys.should == @qik.urls
62
72
 
@@ -122,6 +132,7 @@ describe OEmbed::Providers do
122
132
  end
123
133
 
124
134
  it "should raise an error if no embeddable content is found" do
135
+ OEmbed::Providers.register_all
125
136
  ["http://fake.com/", example_url(:google_video)].each do |url|
126
137
  proc { OEmbed::Providers.get(url) }.should raise_error(OEmbed::NotFound)
127
138
  proc { OEmbed::Providers.raw(url) }.should raise_error(OEmbed::NotFound)
@@ -136,6 +147,10 @@ describe OEmbed::Providers do
136
147
  end
137
148
 
138
149
  it "should fallback to the appropriate provider when URL isn't found" do
150
+ OEmbed::Providers.register_all
151
+ OEmbed::Providers.register_fallback(OEmbed::Providers::Hulu)
152
+ OEmbed::Providers.register_fallback(OEmbed::Providers::OohEmbed)
153
+
139
154
  url = example_url(:google_video)
140
155
 
141
156
  provider = OEmbed::Providers.fallback.last
@@ -157,6 +172,10 @@ describe OEmbed::Providers do
157
172
  end
158
173
 
159
174
  it "should still raise an error if no embeddable content is found" do
175
+ OEmbed::Providers.register_all
176
+ OEmbed::Providers.register_fallback(OEmbed::Providers::Hulu)
177
+ OEmbed::Providers.register_fallback(OEmbed::Providers::OohEmbed)
178
+
160
179
  ["http://fake.com/"].each do |url|
161
180
  proc { OEmbed::Providers.get(url) }.should raise_error(OEmbed::NotFound)
162
181
  proc { OEmbed::Providers.raw(url) }.should raise_error(OEmbed::NotFound)
@@ -15,53 +15,95 @@ describe OEmbed::Response do
15
15
 
16
16
  @new_res = OEmbed::Response.new(valid_response(:object), OEmbed::Providers::OohEmbed)
17
17
 
18
- @default_res = OEmbed::Response.create_for(valid_response(:json), @flickr, example_url(:flickr))
18
+ @default_res = OEmbed::Response.create_for(valid_response(:json), @flickr, example_url(:flickr), :json)
19
19
  @xml_res = OEmbed::Response.create_for(valid_response(:xml), @qik, example_url(:qik), :xml)
20
20
  @json_res = OEmbed::Response.create_for(valid_response(:json), @viddler, example_url(:viddler), :json)
21
- end
22
-
23
- it "should set the provider" do
24
- @new_res.provider.should == OEmbed::Providers::OohEmbed
25
-
26
- @default_res.provider.should == @flickr
27
- @xml_res.provider.should == @qik
28
- @json_res.provider.should == @viddler
29
- end
30
-
31
- it "should parse the data into #fields" do
32
- @new_res.fields.keys.should == valid_response(:object).keys
33
21
 
34
- @default_res.fields.keys.should == valid_response(:object).keys
35
- @xml_res.fields.keys.should == valid_response(:object).keys
36
- @json_res.fields.keys.should == valid_response(:object).keys
22
+ # These keys should be turned into helper methods
23
+ @expected_helpers = {
24
+ "type" => "random",
25
+ "version" => "1.0",
26
+ "html" => "&lt;em&gt;Hello world!&lt;/em&gt;",
27
+ "url" => "http://foo.com/bar",
28
+ }
29
+ # These keys should already be defined
30
+ @expected_skipped = {
31
+ "fields" => "hello",
32
+ "__id__" => 1234,
33
+ "provider" => "oohEmbed",
34
+ "to_s" => "random string",
35
+ }
36
+ @all_expected = @expected_helpers.merge(@expected_skipped)
37
37
  end
38
38
 
39
- it "should only allow JSON or XML" do
40
- lambda do
41
- OEmbed::Response.create_for(valid_response(:json), @flickr, example_url(:flickr), :json)
42
- end.should_not raise_error(OEmbed::FormatNotSupported)
43
-
44
- lambda do
45
- OEmbed::Response.create_for(valid_response(:xml), @flickr, example_url(:flickr), :xml)
46
- end.should_not raise_error(OEmbed::FormatNotSupported)
47
-
48
- lambda do
49
- OEmbed::Response.create_for(valid_response(:yml), @flickr, example_url(:flickr), :yml)
50
- end.should raise_error(OEmbed::FormatNotSupported)
39
+ describe "#initialize" do
40
+ it "should parse the data into fields" do
41
+ # We need to compare keys & values separately because we don't expect all
42
+ # non-string values to be recognized correctly.
43
+
44
+ @new_res.fields.keys.should == valid_response(:object).keys
45
+ @new_res.fields.values.map{|v|v.to_s}.should == valid_response(:object).values.map{|v|v.to_s}
46
+
47
+ @default_res.fields.keys.should == valid_response(:object).keys
48
+ @default_res.fields.values.map{|v|v.to_s}.should == valid_response(:object).values.map{|v|v.to_s}
49
+
50
+ @xml_res.fields.keys.should == valid_response(:object).keys
51
+ @xml_res.fields.values.map{|v|v.to_s}.should == valid_response(:object).values.map{|v|v.to_s}
52
+
53
+ @json_res.fields.keys.should == valid_response(:object).keys
54
+ @json_res.fields.values.map{|v|v.to_s}.should == valid_response(:object).values.map{|v|v.to_s}
55
+ end
56
+
57
+ it "should set the provider" do
58
+ @new_res.provider.should == OEmbed::Providers::OohEmbed
59
+ @default_res.provider.should == @flickr
60
+ @xml_res.provider.should == @qik
61
+ @json_res.provider.should == @viddler
62
+ end
63
+
64
+ it "should set the format" do
65
+ @new_res.format.should be_nil
66
+ @default_res.format.to_s.should == 'json'
67
+ @xml_res.format.to_s.should == 'xml'
68
+ @json_res.format.to_s.should == 'json'
69
+ end
70
+
71
+ it "should set the request_url" do
72
+ @new_res.request_url.should be_nil
73
+ @default_res.request_url.to_s.should == example_url(:flickr)
74
+ @xml_res.request_url.to_s.should == example_url(:qik)
75
+ @json_res.request_url.to_s.should == example_url(:viddler)
76
+ end
51
77
  end
52
78
 
53
- it "should not parse the incorrect format" do
54
- lambda do
55
- OEmbed::Response.create_for(valid_response(:xml), example_url(:flickr), @flickr)
56
- end.should raise_error(JSON::ParserError)
57
-
58
- lambda do
59
- OEmbed::Response.create_for(valid_response(:xml), example_url(:flickr), @viddler, :json)
60
- end.should raise_error(JSON::ParserError)
61
-
62
- lambda do
63
- OEmbed::Response.create_for(valid_response(:json), example_url(:flickr), @viddler, :xml)
64
- end.should raise_error(ArgumentError)
79
+ describe "create_for" do
80
+ it "should only allow JSON or XML" do
81
+ lambda do
82
+ OEmbed::Response.create_for(valid_response(:json), @flickr, example_url(:flickr), :json)
83
+ end.should_not raise_error(OEmbed::FormatNotSupported)
84
+
85
+ lambda do
86
+ OEmbed::Response.create_for(valid_response(:xml), @flickr, example_url(:flickr), :xml)
87
+ end.should_not raise_error(OEmbed::FormatNotSupported)
88
+
89
+ lambda do
90
+ OEmbed::Response.create_for(valid_response(:yml), @flickr, example_url(:flickr), :yml)
91
+ end.should raise_error(OEmbed::FormatNotSupported)
92
+ end
93
+
94
+ it "should not parse the incorrect format" do
95
+ lambda do
96
+ OEmbed::Response.create_for(valid_response(:object), example_url(:flickr), @flickr, :json)
97
+ end.should raise_error(OEmbed::ParseError)
98
+
99
+ lambda do
100
+ OEmbed::Response.create_for(valid_response(:xml), example_url(:flickr), @viddler, :json)
101
+ end.should raise_error(OEmbed::ParseError)
102
+
103
+ lambda do
104
+ OEmbed::Response.create_for(valid_response(:json), example_url(:flickr), @viddler, :xml)
105
+ end.should raise_error(OEmbed::ParseError)
106
+ end
65
107
  end
66
108
 
67
109
  it "should access the XML data through #field" do
@@ -75,16 +117,55 @@ describe OEmbed::Response do
75
117
  @json_res.field(:type).should == "photo"
76
118
  @json_res.field(:version).should == "1.0"
77
119
  @json_res.field(:fields).should == "hello"
78
- @json_res.field(:__id__).should == 1234
120
+ @json_res.field(:__id__).should == "1234"
79
121
  end
80
122
 
81
- it "should automagically define helpers" do
82
- @default_res.type.should == "photo"
83
- @default_res.version.should == "1.0"
84
- end
123
+ describe "#define_methods!" do
124
+ it "should automagically define helpers" do
125
+ local_res = OEmbed::Response.new(@all_expected, OEmbed::Providers::OohEmbed)
85
126
 
86
- it "should protect important methods" do
87
- @default_res.fields.should_not == @default_res.field(:fields)
88
- @default_res.__id__.should_not == @default_res.field(:__id__)
127
+ @all_expected.each do |method, value|
128
+ local_res.should respond_to(method)
129
+ end
130
+ @expected_helpers.each do |method, value|
131
+ local_res.send(method).should == value
132
+ end
133
+ @expected_skipped.each do |method, value|
134
+ local_res.send(method).should_not == value
135
+ end
136
+ end
137
+
138
+ it "should protect most already defined methods" do
139
+ Object.new.should respond_to('__id__')
140
+ Object.new.should respond_to('to_s')
141
+
142
+ @all_expected.keys.should include('__id__')
143
+ @all_expected.keys.should include('to_s')
144
+
145
+ local_res = OEmbed::Response.new(@all_expected, OEmbed::Providers::OohEmbed)
146
+
147
+ local_res.__id__.should_not == local_res.field('__id__')
148
+ local_res.to_s.should_not == local_res.field('to_s')
149
+ end
150
+
151
+ it "should not protect already defined methods that are specifically overridable" do
152
+ class Object
153
+ def version
154
+ "two point oh"
155
+ end
156
+ end
157
+
158
+ Object.new.should respond_to('version')
159
+ String.new.should respond_to('version')
160
+
161
+ @all_expected.keys.should include('version')
162
+ @all_expected['version'].should_not == String.new.version
163
+
164
+ local_res = OEmbed::Response.new(@all_expected, OEmbed::Providers::OohEmbed)
165
+
166
+ local_res.version.should == local_res.field('version')
167
+ local_res.version.should_not == String.new.version
168
+ end
89
169
  end
170
+
90
171
  end