ruby-oembed 0.7.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.
@@ -0,0 +1,59 @@
1
+ module OEmbed
2
+ class Response
3
+ METHODS = [:define_methods!, :provider, :field, :fields]
4
+ attr_reader :fields, :provider, :format
5
+
6
+ def self.create_for(raw, provider, format = :json)
7
+ fields = OEmbed::Formatters.convert(format, raw)
8
+
9
+ resp_type = case fields['type']
10
+ when 'photo' then OEmbed::Response::Photo
11
+ when 'video' then OEmbed::Response::Video
12
+ when 'link' then OEmbed::Response::Link
13
+ when 'rich' then OEmbed::Response::Rich
14
+ else self
15
+ end
16
+
17
+ resp_type.new(fields, provider)
18
+ end
19
+
20
+ def initialize(fields, provider)
21
+ @fields = fields
22
+ @provider = provider
23
+ define_methods!
24
+ end
25
+
26
+ def field(m)
27
+ @fields[m.to_s]
28
+ end
29
+
30
+ def video?
31
+ is_a?(OEmbed::Response::Video)
32
+ end
33
+
34
+ def photo?
35
+ is_a?(OEmbed::Response::Photo)
36
+ end
37
+
38
+ def link?
39
+ is_a?(OEmbed::Response::Link)
40
+ end
41
+
42
+ def rich?
43
+ is_a?(OEmbed::Response::Rich)
44
+ end
45
+
46
+ private
47
+
48
+ def define_methods!
49
+ @fields.keys.each do |key|
50
+ next if METHODS.include?(key.to_sym) || key[0,2]=="__" || key[-1]==??
51
+ class << self
52
+ self
53
+ end.send(:define_method, key) do
54
+ @fields[key]
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,6 @@
1
+ module OEmbed
2
+ class Response
3
+ class Link < self
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ module OEmbed
2
+ class Response
3
+ class Photo < self
4
+ def html
5
+ "<img src='" + self.url + "' />"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ module OEmbed
2
+ class Response
3
+ class Rich < self
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module OEmbed
2
+ class Response
3
+ class Video < self
4
+ end
5
+ end
6
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'oembed'
2
+ OEmbed::Providers.register_all()
3
+ OEmbed::Providers.register_fallback(OEmbed::ProviderDiscovery, OEmbed::Providers::Embedly, OEmbed::Providers::OohEmbed)
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ruby-oembed}
8
+ s.version = "0.7.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Magnus Holm", "Alex Kessinger", "Aris Bartee"]
12
+ s.date = %q{2010-08-19}
13
+ s.description = %q{A fork of a fork. @github[voidfiles,judofyr]. judufyr created the project and voidfiles added support for Embedly. This is just the gem}
14
+ s.email = %q{arisbartee@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ "README.md",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "idea.rb",
23
+ "integration_test/test.rb",
24
+ "integration_test/test_urls.csv",
25
+ "lib/oembed.rb",
26
+ "lib/oembed/embedly_urls.json",
27
+ "lib/oembed/errors.rb",
28
+ "lib/oembed/formatters.rb",
29
+ "lib/oembed/provider.rb",
30
+ "lib/oembed/provider_discovery.rb",
31
+ "lib/oembed/providers.rb",
32
+ "lib/oembed/response.rb",
33
+ "lib/oembed/response/link.rb",
34
+ "lib/oembed/response/photo.rb",
35
+ "lib/oembed/response/rich.rb",
36
+ "lib/oembed/response/video.rb",
37
+ "rails/init.rb",
38
+ "ruby-oembed.gemspec",
39
+ "spec/provider_spec.rb",
40
+ "spec/providers_spec.rb",
41
+ "spec/response_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+ s.homepage = %q{http://github.com/arisbartee/ruby-oembed}
45
+ s.rdoc_options = ["--charset=UTF-8"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = %q{1.3.6}
48
+ s.summary = %q{oEmbed for Ruby}
49
+ s.test_files = [
50
+ "spec/provider_spec.rb",
51
+ "spec/providers_spec.rb",
52
+ "spec/response_spec.rb",
53
+ "spec/spec_helper.rb"
54
+ ]
55
+
56
+ if s.respond_to? :specification_version then
57
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
+ s.add_runtime_dependency(%q<json>, [">= 0"])
62
+ s.add_runtime_dependency(%q<xml-simple>, [">= 0"])
63
+ s.add_development_dependency(%q<rspec>, [">= 0"])
64
+ else
65
+ s.add_dependency(%q<json>, [">= 0"])
66
+ s.add_dependency(%q<xml-simple>, [">= 0"])
67
+ s.add_dependency(%q<rspec>, [">= 0"])
68
+ end
69
+ else
70
+ s.add_dependency(%q<json>, [">= 0"])
71
+ s.add_dependency(%q<xml-simple>, [">= 0"])
72
+ s.add_dependency(%q<rspec>, [">= 0"])
73
+ end
74
+ end
75
+
@@ -0,0 +1,175 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe OEmbed::Provider do
4
+ include OEmbedSpecHelper
5
+
6
+ before(:all) do
7
+ @default = OEmbed::Formatters::DEFAULT
8
+ @flickr = OEmbed::Provider.new("http://www.flickr.com/services/oembed/")
9
+ @qik = OEmbed::Provider.new("http://qik.com/api/oembed.{format}", :xml)
10
+ @viddler = OEmbed::Provider.new("http://lab.viddler.com/services/oembed/", :json)
11
+
12
+ @flickr << "http://*.flickr.com/*"
13
+ @qik << "http://qik.com/video/*"
14
+ @qik << "http://qik.com/*"
15
+ @viddler << "http://*.viddler.com/*"
16
+ end
17
+
18
+ it "should by default use OEmbed::Formatters::DEFAULT" do
19
+ @flickr.format.should == @default
20
+ end
21
+
22
+ it "should allow xml" do
23
+ @qik.format.should == :xml
24
+ end
25
+
26
+ it "should allow json" do
27
+ @viddler.format.should == :json
28
+ end
29
+
30
+ it "should not allow random formats" do
31
+ proc { OEmbed::Provider.new("http://www.hulu.com/api/oembed.{format}", :yml) }.
32
+ should raise_error(OEmbed::FormatNotSupported)
33
+ end
34
+
35
+ it "should add URL schemes" do
36
+ @flickr.urls.should == [%r{^http://([^\.]+\.)?flickr\.com/(.*?)}]
37
+ @qik.urls.should == [%r{^http://qik\.com/video/(.*?)},
38
+ %r{^http://qik\.com/(.*?)}]
39
+ end
40
+
41
+ it "should match URLs" do
42
+ @flickr.include?(example_url(:flickr)).should be_true
43
+ @qik.include?(example_url(:qik)).should be_true
44
+ end
45
+
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
+ 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)
54
+ end
55
+
56
+ describe "#build" do
57
+ it "should return a proper URL" do
58
+ uri = @flickr.build(example_url(:flickr))
59
+ uri.host.should == "www.flickr.com"
60
+ uri.path.should == "/services/oembed/"
61
+ uri.query.include?("format=#{@flickr.format}").should be_true
62
+ uri.query.include?("url=http://flickr.com/photos/bees/2362225867/").should be_true
63
+
64
+ uri = @qik.build(example_url(:qik))
65
+ uri.host.should == "qik.com"
66
+ uri.path.should == "/api/oembed.xml"
67
+ uri.query.should == "url=http://qik.com/video/49565"
68
+ end
69
+
70
+ it "should accept parameters" do
71
+ uri = @flickr.build(example_url(:flickr),
72
+ :maxwidth => 600,
73
+ :maxheight => 200,
74
+ :format => :xml,
75
+ :another => "test")
76
+
77
+ uri.query.include?("maxwidth=600").should be_true
78
+ uri.query.include?("maxheight=200").should be_true
79
+ uri.query.include?("format=xml").should be_true
80
+ uri.query.include?("another=test").should be_true
81
+ end
82
+
83
+ it "should build correctly when format is in the endpoint URL" do
84
+ uri = @qik.build(example_url(:qik), :format => :json)
85
+ uri.path.should == "/api/oembed.json"
86
+ end
87
+ end
88
+
89
+ describe "#raw" do
90
+ it "should return the body on 200" do
91
+ res = Net::HTTPOK.new("1.1", 200, "OK").instance_eval do
92
+ @body = "raw content"
93
+ @read = true
94
+ self
95
+ end
96
+ Net::HTTP.stub!(:start).and_return(res)
97
+
98
+ @flickr.raw(example_url(:flickr)).should == "raw content"
99
+ end
100
+
101
+ it "should raise error on 501" do
102
+ res = Net::HTTPNotImplemented.new("1.1", 501, "Not Implemented")
103
+ Net::HTTP.stub!(:start).and_return(res)
104
+
105
+ proc do
106
+ @flickr.raw(example_url(:flickr))
107
+ end.should raise_error(OEmbed::UnknownFormat)
108
+ end
109
+
110
+ it "should raise error on 404" do
111
+ res = Net::HTTPNotFound.new("1.1", 404, "Not Found")
112
+ Net::HTTP.stub!(:start).and_return(res)
113
+
114
+ proc do
115
+ @flickr.raw(example_url(:flickr))
116
+ end.should raise_error(OEmbed::NotFound)
117
+ end
118
+
119
+ it "should raise error on all other responses" do
120
+ Net::HTTPResponse::CODE_TO_OBJ.delete_if do |code, res|
121
+ ["200", "404", "501"].include?(code)
122
+ end.each do |code, res|
123
+ r = res.new("1.1", code, "Message")
124
+ Net::HTTP.stub!(:start).and_return(r)
125
+
126
+ proc do
127
+ @flickr.raw(example_url(:flickr))
128
+ end.should raise_error(OEmbed::UnknownResponse)
129
+ end
130
+ end
131
+ end
132
+
133
+ describe "#get" do
134
+ it "should send the specified format" do
135
+ @flickr.should_receive(:raw).
136
+ with(example_url(:flickr), {:format=>:json}).
137
+ and_return(valid_response(:json))
138
+ @flickr.get(example_url(:flickr), :format=>:json)
139
+
140
+ @flickr.should_receive(:raw).
141
+ with(example_url(:flickr), {:format=>:xml}).
142
+ and_return(valid_response(:xml))
143
+ @flickr.get(example_url(:flickr), :format=>:xml)
144
+
145
+ lambda do
146
+ @flickr.should_receive(:raw).
147
+ with(example_url(:flickr), {:format=>:yml}).
148
+ and_return(valid_response(:json))
149
+ @flickr.get(example_url(:flickr), :format=>:yml)
150
+ end.should raise_error(OEmbed::FormatNotSupported)
151
+ end
152
+
153
+ it "should send the provider's format if none is specified" do
154
+ @flickr.should_receive(:raw).
155
+ with(example_url(:flickr), :format => @default).
156
+ and_return(valid_response(@default))
157
+ @flickr.get(example_url(:flickr))
158
+
159
+ @qik.should_receive(:raw).
160
+ with(example_url(:qik), :format=>:xml).
161
+ and_return(valid_response(:xml))
162
+ @qik.get(example_url(:qik))
163
+
164
+ @viddler.should_receive(:raw).
165
+ with(example_url(:viddler), :format=>:json).
166
+ and_return(valid_response(:json))
167
+ @viddler.get(example_url(:viddler))
168
+ 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
+ end
175
+ end
@@ -0,0 +1,125 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe OEmbed::Providers do
4
+ include OEmbedSpecHelper
5
+
6
+ before(:all) do
7
+ @flickr = OEmbed::Provider.new("http://www.flickr.com/services/oembed/")
8
+ @qik = OEmbed::Provider.new("http://qik.com/api/oembed.{format}")
9
+
10
+ @flickr << "http://*.flickr.com/*"
11
+ @qik << "http://qik.com/video/*"
12
+ @qik << "http://qik.com/*"
13
+ end
14
+
15
+ it "should register providers" do
16
+ OEmbed::Providers.register(@flickr, @qik)
17
+ urls = OEmbed::Providers.urls.dup
18
+
19
+ @flickr.urls.each do |regexp|
20
+ urls.delete(regexp).should == @flickr
21
+ end
22
+
23
+ @qik.urls.each do |regexp|
24
+ urls.delete(regexp).should == @qik
25
+ end
26
+
27
+ urls.length.should == 0
28
+ end
29
+
30
+ it "should find by URLs" do
31
+ OEmbed::Providers.find(example_url(:flickr)).should == @flickr
32
+ OEmbed::Providers.find(example_url(:qik)).should == @qik
33
+ end
34
+
35
+ it "should unregister providers" do
36
+ OEmbed::Providers.unregister(@flickr)
37
+ urls = OEmbed::Providers.urls.dup
38
+
39
+ @qik.urls.each do |regexp|
40
+ urls.delete(regexp).should == @qik
41
+ end
42
+
43
+ urls.length.should == 0
44
+ end
45
+
46
+ it "should use the OEmbed::ProviderDiscovery fallback provider correctly" do
47
+
48
+
49
+ # None of the registered providers should match
50
+ # all_example_urls.each do |url|
51
+ provider = OEmbed::Providers.find('http://www.mobypicture.com/user/IadoreChrisette/view/7275748')
52
+ provider.should_not_receive(:raw)
53
+ provider.should_not_receive(:get)
54
+ # end
55
+
56
+ # Register the fallback
57
+ # OEmbed::Providers.register_fallback(OEmbed::ProviderDiscovery)
58
+
59
+ # provider = OEmbed::ProviderDiscovery
60
+ # provider.should_receive(:raw).
61
+ # with(url, {}).
62
+ # and_return(valid_response(:raw))
63
+ # provider.should_receive(:get).
64
+ # with(url, {}).
65
+ # and_return(valid_response(:object))
66
+ #asdf
67
+
68
+
69
+ end
70
+
71
+ it "should bridge #get and #raw to the right provider" do
72
+ OEmbed::Providers.register_all
73
+ all_example_urls.each do |url|
74
+ provider = OEmbed::Providers.find(url)
75
+ provider.should_receive(:raw).
76
+ with(url, {})
77
+ provider.should_receive(:get).
78
+ with(url, {})
79
+ OEmbed::Providers.raw(url)
80
+ OEmbed::Providers.get(url)
81
+ end
82
+ end
83
+
84
+ it "should raise an error if no embeddable content is found" do
85
+ ["http://fake.com/", example_url(:google_video)].each do |url|
86
+ proc { OEmbed::Providers.get(url) }.should raise_error(OEmbed::NotFound)
87
+ proc { OEmbed::Providers.raw(url) }.should raise_error(OEmbed::NotFound)
88
+ end
89
+ end
90
+
91
+ it "should register fallback providers" do
92
+ OEmbed::Providers.register_fallback(OEmbed::Providers::Hulu)
93
+ OEmbed::Providers.register_fallback(OEmbed::Providers::OohEmbed)
94
+
95
+ OEmbed::Providers.fallback.should == [ OEmbed::Providers::Hulu, OEmbed::Providers::OohEmbed]
96
+ end
97
+
98
+ it "should fallback to the appropriate provider when URL isn't found" do
99
+ url = example_url(:google_video)
100
+
101
+ provider = OEmbed::Providers.fallback.last
102
+ provider.should_receive(:raw).
103
+ with(url, {}).
104
+ and_return(valid_response(:raw))
105
+ provider.should_receive(:get).
106
+ with(url, {}).
107
+ and_return(valid_response(:object))
108
+
109
+ OEmbed::Providers.fallback.each do |p|
110
+ next if p == provider
111
+ p.should_receive(:raw).and_raise(OEmbed::NotFound)
112
+ p.should_receive(:get).and_raise(OEmbed::NotFound)
113
+ end
114
+
115
+ OEmbed::Providers.raw(url)
116
+ OEmbed::Providers.get(url)
117
+ end
118
+
119
+ it "should still raise an error if no embeddable content is found" do
120
+ ["http://fake.com/"].each do |url|
121
+ proc { OEmbed::Providers.get(url) }.should raise_error(OEmbed::NotFound)
122
+ proc { OEmbed::Providers.raw(url) }.should raise_error(OEmbed::NotFound)
123
+ end
124
+ end
125
+ end