oembed_links 0.1.9

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.
@@ -0,0 +1,46 @@
1
+ :config:
2
+ :method: "NetHTTP"
3
+
4
+ :providers:
5
+ :oohembed: "http://oohembed.com/oohembed/"
6
+ :vimeo: "http://www.vimeo.com/api/oembed.{format}"
7
+ :hulu: "http://www.hulu.com/api/oembed.{format}"
8
+ :flickr: "http://www.flickr.com/services/oembed"
9
+ :viddler: "http://lab.viddler.com/services/oembed/"
10
+
11
+ :oohembed:
12
+ :format: "json"
13
+ :schemes:
14
+ - "http://*.amazon.(com|co.uk|de|ca|jp)/*/(gp/product|o/ASIN|obidos/ASIN|dp)/*"
15
+ - "http://*.amazon.(com|co.uk|de|ca|jp)/(gp/product|o/ASIN|obidos/ASIN|dp)/*"
16
+ - "http://*.collegehumor.com/video:*"
17
+ - "http://*.funnyordie.com/videos/*"
18
+ - "http://video.google.com/videoplay?*"
19
+ - "http://*.imdb.com/title/tt*/"
20
+ - "http://*.metacafe.com/watch/*"
21
+ - "http://*.slideshare.net/*"
22
+ - "http://twitter.com/*/statuses/*"
23
+ - "http://*.wikipedia.org/wiki/*"
24
+ - "http://*.wordpress.com/[0-9]{4}/[0-9]{2}/[0-9]{2}/*"
25
+ - "http://*.youtube.com/watch*"
26
+
27
+ :vimeo:
28
+ :format: "json"
29
+ :schemes:
30
+ - "http://www.vimeo.com/groups/*/*"
31
+ - "http://www.vimeo.com/*"
32
+
33
+ :hulu:
34
+ :format: "xml"
35
+ :schemes:
36
+ - "http://www.hulu.com/watch/*"
37
+
38
+ :flickr:
39
+ :format: "xml"
40
+ :schemes:
41
+ - "http://*.flickr.com/*"
42
+
43
+ :viddler:
44
+ :format: "xml"
45
+ :schemes:
46
+ - "http://*.viddler.com/*"
@@ -0,0 +1,7 @@
1
+ require 'oembed_links'
2
+
3
+ yaml_file = File.join(RAILS_ROOT, "config", "oembed_links.yml")
4
+ if File.exists?(yaml_file)
5
+ OEmbed::register_yaml_file(yaml_file)
6
+ end
7
+
@@ -0,0 +1,286 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe OEmbed, "registration tasks" do
4
+ include SpecHelper
5
+
6
+ before(:each) do
7
+ OEmbed.clear_registrations
8
+ OEmbed.load_default_libs
9
+ clear_urls
10
+ end
11
+
12
+ it "should default to NetHTTP if no method is specified in configuration" do
13
+ OEmbed.register()
14
+ OEmbed.instance_variable_get("@fetch_method").should == "NetHTTP"
15
+ end
16
+
17
+ it "should throw an error if you provide provider URLs but no scheme or format information" do
18
+ lambda { OEmbed.register({ }, { :fake => "http://fake" })}.should raise_error
19
+ end
20
+
21
+ it "should throw an error if you don't provide any scheme urls for registration" do
22
+ lambda { OEmbed.register({ }, { :fake => "http://fake" }, { :fake => { :format => "json", :schemes => []}})}.should raise_error
23
+ end
24
+
25
+ it "should default to json format if none is specified" do
26
+ OEmbed.register({ }, { :fake => "http://fake" }, { :fake => { :schemes => ["http://fake/*"]}})
27
+ OEmbed.instance_variable_get("@formats")[:fake].should == "json"
28
+ end
29
+
30
+ it "should support loading a configuration via YAML" do
31
+ OEmbed.register_yaml_file(File.join(File.dirname(__FILE__), "oembed_links_test.yml"))
32
+ OEmbed.instance_variable_get("@urls").size.should == 3
33
+ end
34
+
35
+ it "should support ad hoc addition of providers" do
36
+ OEmbed.register_yaml_file(File.join(File.dirname(__FILE__), "oembed_links_test.yml"))
37
+ OEmbed.instance_variable_get("@urls").size.should == 3
38
+ OEmbed.register_provider(:test4, "http://test4/oembed.{format}", "xml", "http://test4.*/*", "http://test4.*/foo/*")
39
+ OEmbed.instance_variable_get("@urls").size.should == 4
40
+ end
41
+
42
+ it "should support adding new fetchers" do
43
+
44
+ OEmbed.register_fetcher(FakeFetcher)
45
+ OEmbed.register({ :method => "fake_fetcher"},
46
+ { :fake => "http://fake" },
47
+ { :fake => {
48
+ :format => "json",
49
+ :schemes => "http://fake/*"
50
+ }})
51
+ OEmbed.transform("http://fake/bar/baz").should == "fakecontent"
52
+ end
53
+
54
+ it "should support adding new formatters" do
55
+ OEmbed.register_formatter(FakeFormatter)
56
+ OEmbed.register({ :method => "fake_fetcher"},
57
+ { :fake => "http://fake" },
58
+ { :fake => {
59
+ :format => "fake_formatter",
60
+ :schemes => "http://fake/*"
61
+ }})
62
+ url_provides("")
63
+ OEmbed.transform("http://fake/bar/baz").should == "http://fakesville"
64
+ end
65
+
66
+ it "should allow selective loading of formatters through the load_default_libs function" do
67
+ OEmbed.clear_registrations
68
+ OEmbed.load_default_libs
69
+ OEmbed.instance_variable_get("@formatters")["xml"].class.should == OEmbed::Formatters::LibXML
70
+ OEmbed.clear_registrations
71
+ OEmbed.load_default_libs("libxml")
72
+ OEmbed.instance_variable_get("@formatters")["xml"].class.should == OEmbed::Formatters::HpricotXML
73
+ OEmbed.clear_registrations
74
+ OEmbed.load_default_libs("libxml", "hpricot")
75
+ OEmbed.instance_variable_get("@formatters")["xml"].class.should == OEmbed::Formatters::RubyXML
76
+ end
77
+
78
+ it "should support the hpricot formatter" do
79
+ OEmbed.clear_registrations
80
+ OEmbed.load_default_libs("libxml")
81
+ url_provides(<<-EOXML)
82
+ <?xml version="1.0"?>
83
+ <oembed>
84
+ <html>bar</html>
85
+ </oembed>
86
+ EOXML
87
+ OEmbed.register_provider(:test, "http://test4/oembed.{format}", "xml", "http://test.*/*")
88
+ OEmbed.transform("http://test.com/bar/baz").should == "bar"
89
+ end
90
+
91
+ it "should support the rexml formatter" do
92
+ OEmbed.clear_registrations
93
+ OEmbed.load_default_libs("libxml", "hpricot")
94
+ url_provides(<<-EOXML)
95
+ <?xml version="1.0"?>
96
+ <oembed>
97
+ <html>barxml</html>
98
+ </oembed>
99
+ EOXML
100
+ OEmbed.register_provider(:test, "http://test4/oembed.{format}", "xml", "http://test.*/*")
101
+ OEmbed.transform("http://test.com/bar/baz").should == "barxml"
102
+ end
103
+
104
+
105
+
106
+ end
107
+
108
+ describe OEmbed, "transforming functions" do
109
+ include SpecHelper
110
+ before(:each) do
111
+ OEmbed.clear_registrations
112
+ OEmbed.load_default_libs
113
+ clear_urls
114
+ url_provides({
115
+ "html" => "foo",
116
+ "type" => "video"
117
+ }.to_json)
118
+ OEmbed.register_yaml_file(File.join(File.dirname(__FILE__), "oembed_links_test.yml"))
119
+ @current_path = File.dirname(__FILE__)
120
+ @template_path = File.join(@current_path, "templates")
121
+ end
122
+
123
+ it "should always give priority to provider conditional blocks" do
124
+ OEmbed.transform("http://test1.net/foo") do |r, url|
125
+ r.none? { "none" }
126
+ r.any? { |a| "any" }
127
+ r.video? { |v| "video" }
128
+ r.from?(:test1) { |t| "test1" }
129
+ r.matches?(/./) { |m| "regex" }
130
+ end.should == "test1"
131
+ end
132
+
133
+ it "should always give priority regex conditional blocks over all others except provider" do
134
+ OEmbed.transform("http://test1.net/foo") do |r, url|
135
+ r.none? { "none" }
136
+ r.any? { |a| "any" }
137
+ r.video? { |v| "video" }
138
+ r.matches?(/./) { |m| "regex" }
139
+ end.should == "regex"
140
+ OEmbed.transform("http://test1.net/foo") do |r, url|
141
+ r.matches?(/./) { |m| "regex" }
142
+ r.any? { |a| "any" }
143
+ r.video? { |v| "video" }
144
+ end.should == "regex"
145
+ end
146
+
147
+ it "should recognize the type of content and handle the conditional block appropriately" do
148
+ OEmbed.transform("http://test1.net/foo") do |r, url|
149
+ r.none? { "none" }
150
+ r.any? { |a| "any" }
151
+ r.video? { |v| "video" }
152
+ end.should == "video"
153
+ url_provides({
154
+ "html" => "bar",
155
+ "type" => "hedgehog"
156
+ }.to_json)
157
+ OEmbed.transform("http://test1.net/foo") do |r, url|
158
+ r.video? { |v| "video" }
159
+ r.hedgehog? { |v| "hedgey"}
160
+ end.should == "hedgey"
161
+ end
162
+
163
+ it "should still output the content of a url if no transforming blocks match it" do
164
+ OEmbed.transform("http://test1.net/foo") do |r, url|
165
+ r.audio? { |a| "audio" }
166
+ r.hedgehog? { |v| "hedgey"}
167
+ r.from?(:test2) { |t| "test2" }
168
+ r.matches?(/baz/) { |m| "regex" }
169
+ end.should == "http://test1.net/foo"
170
+ end
171
+
172
+ it "should transform only urls which have registered providers" do
173
+ OEmbed.transform("http://test1.net/foo and http://not.a.valid.url.host/fake are urls") do |r, url|
174
+ r.video? { |v| "video" }
175
+ end.should == "video and http://not.a.valid.url.host/fake are urls"
176
+ end
177
+
178
+ it "should pass control to the .none? block if no scheme matched" do
179
+ OEmbed.transform("http://not.a.valid.url.host/fake") do |r, url|
180
+ r.none? { "nomatch" }
181
+ r.audio? { |a| "audio" }
182
+ r.hedgehog? { |v| "hedgey"}
183
+ r.from?(:test2) { |t| "test2" }
184
+ r.matches?(/baz/) { |m| "regex" }
185
+ end.should == "nomatch"
186
+ end
187
+
188
+ it "should allow templates to be used to process the output" do
189
+ url_provides({
190
+ "url" => "template!"
191
+ }.to_json)
192
+
193
+ OEmbed.transform("http://test1.net/foo") do |r, url|
194
+ r.any?(:template => File.join(@template_path, "test.rhtml"))
195
+ end.should == "template! rhtml"
196
+ end
197
+
198
+
199
+ it "should allow a template root to be set programmatically to process the output" do
200
+ url_provides({
201
+ "url" => "template!"
202
+ }.to_json)
203
+ OEmbed::TemplateResolver.template_root = @template_path
204
+ OEmbed.transform("http://test1.net/foo") do |r, url|
205
+ r.any?(:template => "test.html.erb")
206
+ end.should == "template! erb"
207
+ end
208
+
209
+ it "should allow for selection of different template renderers" do
210
+ url_provides({
211
+ "url" => "template!"
212
+ }.to_json)
213
+ defined?(Erubis).should_not == true
214
+ defined?(Haml).should_not == true
215
+ OEmbed::TemplateResolver.template_root = @template_path
216
+ OEmbed::TemplateResolver.template_processor = :erubis
217
+ OEmbed.transform("http://test1.net/foo") do |r, url|
218
+ r.any?(:template => "test.html.erb")
219
+ end.should == "template! erb"
220
+ defined?(Erubis).should == "constant"
221
+ OEmbed::TemplateResolver.template_processor = :haml
222
+ OEmbed.transform("http://test1.net/foo") do |r, url|
223
+ r.any?(:template => "test.haml")
224
+ end.should == "template! haml"
225
+ defined?(Haml).should == "constant"
226
+ end
227
+
228
+ it "should throw an exception if a path to a template specified does not exist" do
229
+ url_provides({
230
+ "url" => "template!"
231
+ }.to_json)
232
+ lambda { OEmbed.transform("http://test1.net/foo") do |r, url|
233
+ r.any?(:template => "does.not.exist")
234
+ end }.should raise_error
235
+ end
236
+
237
+
238
+ end
239
+
240
+
241
+ describe OEmbed, "Rails template resolving functions" do
242
+ include SpecHelper
243
+ before(:each) do
244
+ OEmbed.clear_registrations
245
+ OEmbed.load_default_libs
246
+ clear_urls
247
+ url_provides({
248
+ "html" => "foo",
249
+ "type" => "video",
250
+ "url" => "rails"
251
+ }.to_json)
252
+ OEmbed.register_yaml_file(File.join(File.dirname(__FILE__), "oembed_links_test.yml"))
253
+ OEmbed::TemplateResolver.template_processor = nil
254
+ @current_path = File.dirname(__FILE__)
255
+ @template_path = File.join(@current_path, "templates")
256
+
257
+ require 'actionpack'
258
+ require 'action_controller'
259
+ require 'action_controller/test_process'
260
+
261
+ class ::ApplicationController < ActionController::Base
262
+
263
+ end
264
+ ApplicationController.view_paths += [File.dirname(__FILE__)]
265
+ ActiveSupport::Dependencies.mark_for_unload(ApplicationController)
266
+ end
267
+
268
+ it "should support Rails-like template paths for template selection" do
269
+
270
+ OEmbed.transform("http://test1.net/foo") do |r, url|
271
+ r.any?(:template => ("templates/test"))
272
+ end.should == "rails erb"
273
+ end
274
+
275
+ it "should support Rails-like template paths with extension specified" do
276
+ OEmbed.transform("http://test1.net/foo") do |r, url|
277
+ r.any?(:template => "templates/test.rhtml")
278
+ end.should == "rails rhtml"
279
+ end
280
+
281
+ end
282
+
283
+
284
+
285
+
286
+
@@ -0,0 +1,26 @@
1
+ :config:
2
+ :method: "NetHTTP"
3
+
4
+ :providers:
5
+ :test1: "http://test1/dude/{format}/"
6
+ :test2: "http://test2.{format}"
7
+ :test3: "http://test3"
8
+
9
+
10
+ :test1:
11
+ :format: "json"
12
+ :schemes:
13
+ - "http://test1.*/*"
14
+ - "http://test1/*/test/*"
15
+
16
+ :test2:
17
+ :format: "xml"
18
+ :schemes:
19
+ - "http://test2.*/*/stuff/*"
20
+ - "http://test2/foo/bar/*"
21
+
22
+ :test3:
23
+ :format: "json"
24
+ :schemes:
25
+ - "http://test3/foo/*"
26
+
@@ -0,0 +1,62 @@
1
+ $: << File.join(File.dirname(__FILE__), "..", "lib")
2
+ require 'oembed_links'
3
+
4
+ module SpecHelper
5
+
6
+ def url_provides(content)
7
+ Net::HTTP.fake_content(content)
8
+ end
9
+
10
+ def clear_urls
11
+ Net::HTTP.clear_fakes
12
+ end
13
+
14
+ end
15
+
16
+ require 'net/http'
17
+ module Net
18
+ class HTTP
19
+
20
+ def self.fake_content(content)
21
+ @content = content
22
+ end
23
+
24
+
25
+ def self.clear_fakes
26
+ @content = nil
27
+ end
28
+
29
+ def self.get(uri)
30
+ return @content
31
+ end
32
+
33
+ end
34
+ end
35
+
36
+
37
+ require 'json'
38
+ class FakeFetcher
39
+ def name
40
+ "fake_fetcher"
41
+ end
42
+
43
+ def fetch(url)
44
+ {
45
+ "url" => "fakecontent"
46
+ }.to_json
47
+ end
48
+ end
49
+
50
+ class FakeFormatter
51
+ def name
52
+ "fake_formatter"
53
+ end
54
+
55
+ def format(txt)
56
+ {
57
+ "url" => "http://fakesville"
58
+ }
59
+ end
60
+ end
61
+
62
+
@@ -0,0 +1 @@
1
+ = data['url'] + ' haml'
@@ -0,0 +1 @@
1
+ <%= data['url'] %> erb
@@ -0,0 +1 @@
1
+ <%= data['url'] %> rhtml
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oembed_links
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.9
5
+ platform: ruby
6
+ authors:
7
+ - Indianapolis Star MD&D
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-16 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: json
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Easy OEmbed integration for Ruby (and Rails).
46
+ email:
47
+ - bugs@indy.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - Manifest.txt
55
+ - README.txt
56
+ files:
57
+ - CREDIT
58
+ - History.txt
59
+ - Manifest.txt
60
+ - README.txt
61
+ - Rakefile
62
+ - lib/oembed_links.rb
63
+ - lib/oembed_links/fetchers/net_http.rb
64
+ - lib/oembed_links/fetchers/ruby_tubesday.rb
65
+ - lib/oembed_links/fetchers/curb.rb
66
+ - lib/oembed_links/formatters/hpricot_xml.rb
67
+ - lib/oembed_links/formatters/json.rb
68
+ - lib/oembed_links/formatters/lib_xml.rb
69
+ - lib/oembed_links/formatters/ruby_xml.rb
70
+ - lib/oembed_links/response.rb
71
+ - lib/oembed_links/template_resolver.rb
72
+ - oembed_links.gemspec
73
+ - oembed_links_example.yml
74
+ - rails/init.rb
75
+ - spec/oembed_links_spec.rb
76
+ - spec/oembed_links_test.yml
77
+ - spec/spec_helper.rb
78
+ - spec/templates/test.haml
79
+ - spec/templates/test.html.erb
80
+ - spec/templates/test.rhtml
81
+ has_rdoc: true
82
+ homepage: http://indystar.com/
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options:
87
+ - --main
88
+ - README.txt
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: "0"
96
+ version:
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: "0"
102
+ version:
103
+ requirements: []
104
+
105
+ rubyforge_project: oembed_links
106
+ rubygems_version: 1.3.5
107
+ signing_key:
108
+ specification_version: 2
109
+ summary: ""
110
+ test_files: []
111
+