thornbed 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -0
- data/lib/thornbed/provider.rb +12 -4
- data/lib/thornbed/providers/9gag.rb +32 -0
- data/lib/thornbed/providers/imgur.rb +7 -7
- data/lib/thornbed/providers/memegenerator.rb +7 -7
- data/lib/thornbed/providers/quickmeme.rb +7 -7
- data/lib/thornbed/version.rb +1 -1
- data/spec/provider_spec.rb +8 -0
- data/spec/providers/9gag_spec.rb +44 -0
- data/spec/thornbed_spec.rb +5 -0
- metadata +9 -4
data/README.md
CHANGED
@@ -52,3 +52,8 @@ You should inherit from `Thornbed::Providers::Provider`. See imgur.rb for an exa
|
|
52
52
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
53
|
4. Push to the branch (`git push origin my-new-feature`)
|
54
54
|
5. Create new Pull Request
|
55
|
+
|
56
|
+
|
57
|
+
## Contributors
|
58
|
+
|
59
|
+
[frodsan](https://github.com/frodsan)
|
data/lib/thornbed/provider.rb
CHANGED
@@ -10,21 +10,29 @@ module Thornbed
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.inherited(subclass)
|
13
|
-
|
13
|
+
providers << subclass
|
14
|
+
end
|
15
|
+
|
16
|
+
def pattern
|
17
|
+
raise NotImplementedError,
|
18
|
+
'Subclasses must implement a pattern method'
|
14
19
|
end
|
15
20
|
|
16
21
|
def valid?(url)
|
17
|
-
|
22
|
+
!!pattern.match(url)
|
18
23
|
end
|
19
24
|
|
20
25
|
def provider_name
|
21
26
|
self.class.to_s.split('::')[-1]
|
22
27
|
end
|
28
|
+
|
29
|
+
def get(url)
|
30
|
+
raise NotImplementedError, "Subclasses must implement a get method."
|
31
|
+
end
|
23
32
|
end
|
24
33
|
|
25
34
|
def self.get(url)
|
26
|
-
|
27
|
-
provider = providers.detect { |p| p.new.valid?(url) }
|
35
|
+
provider = Provider.providers.detect { |p| p.new.valid?(url) }
|
28
36
|
raise Thornbed::NotFound, url if provider.nil?
|
29
37
|
provider.new.get(url)
|
30
38
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "uri"
|
2
|
+
require "thornbed"
|
3
|
+
|
4
|
+
module Thornbed::Providers
|
5
|
+
class NineGag < Thornbed::Providers::Provider
|
6
|
+
attr_accessor :pattern
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
self.pattern = /^http(s)?:\/\/(\w+\.)?(9gag\.com\/|d24w6bsrhbeh9d\.cloudfront\.net\/photo\/)(gag\/|fast#)?(\d+)(\?[\w=]+)?(_\d+b\.jpg)?$/
|
10
|
+
end
|
11
|
+
|
12
|
+
def get(url)
|
13
|
+
raise Thornbed::NotValid, url if !valid?(url)
|
14
|
+
url = URI.parse(url)
|
15
|
+
ptype = "jpg"
|
16
|
+
|
17
|
+
pic_id = url.path == '/fast' ? url.fragment : /(\d+)/.match(url.path)[0]
|
18
|
+
|
19
|
+
{
|
20
|
+
url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/#{pic_id}_700b.#{ptype}",
|
21
|
+
type: "photo",
|
22
|
+
provider_name: provider_name,
|
23
|
+
provider_url: "http://9gag.com",
|
24
|
+
thumbnail_url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/#{pic_id}_fbthumbnail.#{ptype}",
|
25
|
+
version: "1.0",
|
26
|
+
page: "http://9gag.com/gag/#{pic_id}",
|
27
|
+
width: nil,
|
28
|
+
height: nil
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -3,14 +3,14 @@ require "thornbed"
|
|
3
3
|
|
4
4
|
module Thornbed::Providers
|
5
5
|
class Imgur < Thornbed::Providers::Provider
|
6
|
+
attr_accessor :pattern
|
7
|
+
|
6
8
|
def initialize
|
7
|
-
|
8
|
-
@type = "photo"
|
9
|
-
@provider_url = "http://imgur.com"
|
9
|
+
self.pattern = /^http(s)?:\/\/((i\.)|(www\.))?imgur.com\/(gallery\/)?(\w){5}(s|l|b|m|t)?(\.gif|\.jpg|.jpeg|\.png)?(\?full)?$/
|
10
10
|
end
|
11
11
|
|
12
12
|
def get(url)
|
13
|
-
raise Thornbed::NotValid, url if !
|
13
|
+
raise Thornbed::NotValid, url if !valid?(url)
|
14
14
|
url = URI.parse(url)
|
15
15
|
gallery = /gallery/ =~ url.path
|
16
16
|
direct = /\/(\w){5}(\.gif|\.jpg|\.jpeg|\.png)$/.match(url.path)
|
@@ -31,9 +31,9 @@ module Thornbed::Providers
|
|
31
31
|
|
32
32
|
{
|
33
33
|
url: "http://i.imgur.com/#{pic_id}.#{ptype}",
|
34
|
-
type: "
|
35
|
-
provider_name:
|
36
|
-
provider_url:
|
34
|
+
type: "photo",
|
35
|
+
provider_name: provider_name,
|
36
|
+
provider_url: "http://imgur.com",
|
37
37
|
thumbnail_url: "http://i.imgur.com/#{pic_id}s.#{ptype}",
|
38
38
|
version: "1.0",
|
39
39
|
page: "http://imgur.com/#{pic_id}",
|
@@ -3,22 +3,22 @@ require "thornbed"
|
|
3
3
|
|
4
4
|
module Thornbed::Providers
|
5
5
|
class Memegenerator < Thornbed::Providers::Provider
|
6
|
+
attr_accessor :pattern
|
7
|
+
|
6
8
|
def initialize
|
7
|
-
|
8
|
-
@type = "photo"
|
9
|
-
@provider_url = "http://memegenerator.com"
|
9
|
+
self.pattern = /^http(s)?:\/\/(cdn\.)?memegenerator\.net\/instance(s)?\/(\d+x\/)?\d+(\.jpg)?(\?[\w\W]*)?$/
|
10
10
|
end
|
11
11
|
|
12
12
|
def get(url)
|
13
|
-
raise Thornbed::NotValid, url if !
|
13
|
+
raise Thornbed::NotValid, url if !valid?(url)
|
14
14
|
url = URI.parse(url)
|
15
15
|
ptype = "jpg"
|
16
16
|
pic_id = /(\d+x\/)?\d+(\.jpg)?(\?[\w\W]*)?$/.match(url.path)
|
17
17
|
{
|
18
18
|
url: "http://cdn.memegenerator.net/instances/400x/#{pic_id}.#{ptype}",
|
19
|
-
type: "
|
20
|
-
provider_name:
|
21
|
-
provider_url:
|
19
|
+
type: "photo",
|
20
|
+
provider_name: provider_name,
|
21
|
+
provider_url: "http://memegenerator.com",
|
22
22
|
thumbnail_url: "http://cdn.memegenerator.net/instances/100x/#{pic_id}.#{ptype}",
|
23
23
|
version: "1.0",
|
24
24
|
page: "http://memegenerator.net/instance/#{pic_id}",
|
@@ -3,14 +3,14 @@ require "thornbed"
|
|
3
3
|
|
4
4
|
module Thornbed::Providers
|
5
5
|
class QuickMeme < Thornbed::Providers::Provider
|
6
|
+
attr_accessor :pattern
|
7
|
+
|
6
8
|
def initialize
|
7
|
-
|
8
|
-
@type = "photo"
|
9
|
-
@provider_url = "http://quickmeme.com"
|
9
|
+
self.pattern = /http(s)?:\/\/(i|t)?(\.)?(www\.)?(quickmeme.com|qkme.me)\/(meme\/)?(\w){6}(\/)?(.jpg)?((\?|#).*)?$/
|
10
10
|
end
|
11
11
|
|
12
12
|
def get(url)
|
13
|
-
raise Thornbed::NotValid, url if !
|
13
|
+
raise Thornbed::NotValid, url if !valid?(url)
|
14
14
|
url = URI.parse(url)
|
15
15
|
normal = /quickmeme.com/ =~ url.to_s
|
16
16
|
direct = /qkme.me/ =~ url.to_s
|
@@ -25,9 +25,9 @@ module Thornbed::Providers
|
|
25
25
|
|
26
26
|
{
|
27
27
|
url: "http://i.qkme.me/#{pic_id}.#{ptype}",
|
28
|
-
type: "
|
29
|
-
provider_name:
|
30
|
-
provider_url:
|
28
|
+
type: "photo",
|
29
|
+
provider_name: provider_name,
|
30
|
+
provider_url: "http://quickmeme.com",
|
31
31
|
thumbnail_url: "http://t.qkme.me/#{pic_id}.#{ptype}",
|
32
32
|
version: "1.0",
|
33
33
|
page: "http://quickmeme.com/#{pic_id}/",
|
data/lib/thornbed/version.rb
CHANGED
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Thornbed::Providers::Provider do
|
4
|
+
it "raises NotImplementedError when pattern is not implemented in a subclass" do
|
5
|
+
klass = Class.new(Thornbed::Providers::Provider)
|
6
|
+
-> { klass.new.pattern }.should raise_error(NotImplementedError)
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "NineGag" do
|
4
|
+
it "should have NineGag as a provider" do
|
5
|
+
Thornbed::Providers.constants.should include(:NineGag)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should accept different NineGag urls" do
|
9
|
+
ninegag = Thornbed::Providers::NineGag.new
|
10
|
+
ninegag.valid?("http://9gag.com/gag/5931764").should be_true
|
11
|
+
ninegag.valid?("http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_700b.jpg").should be_true
|
12
|
+
ninegag.valid?("http://9gag.com/gag/5929873?ref=popular").should be_true
|
13
|
+
ninegag.valid?("http://9gag.com/fast#5931764").should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return OEmbed hash for NineGag urls" do
|
17
|
+
ninegag = Thornbed::Providers::NineGag.new
|
18
|
+
res = ninegag.get("http://9gag.com/gag/5931764")
|
19
|
+
res.should include(url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_700b.jpg")
|
20
|
+
|
21
|
+
res = ninegag.get("http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_700b.jpg")
|
22
|
+
res.should include(url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_700b.jpg")
|
23
|
+
|
24
|
+
res = ninegag.get("http://9gag.com/gag/5929873?ref=popular")
|
25
|
+
res.should include(url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/5929873_700b.jpg")
|
26
|
+
|
27
|
+
res = ninegag.get("http://9gag.com/fast#5931764")
|
28
|
+
res.should include(url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_700b.jpg")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should discover NineGag provider" do
|
32
|
+
res = Thornbed.get("http://9gag.com/gag/5931764")
|
33
|
+
res.should include(url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_700b.jpg")
|
34
|
+
res.should include(thumbnail_url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_fbthumbnail.jpg")
|
35
|
+
res.should include(page: "http://9gag.com/gag/5931764")
|
36
|
+
res.should include(provider_name: "NineGag")
|
37
|
+
|
38
|
+
res = Thornbed.get("http://9gag.com/fast#5931764")
|
39
|
+
res.should include(url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_700b.jpg")
|
40
|
+
res.should include(thumbnail_url: "http://d24w6bsrhbeh9d.cloudfront.net/photo/5931764_fbthumbnail.jpg")
|
41
|
+
res.should include(page: "http://9gag.com/gag/5931764")
|
42
|
+
res.should include(provider_name: "NineGag")
|
43
|
+
end
|
44
|
+
end
|
data/spec/thornbed_spec.rb
CHANGED
@@ -29,4 +29,9 @@ describe Thornbed do
|
|
29
29
|
imgur = Thornbed::Providers::Imgur.new
|
30
30
|
lambda { imgur.get('http://imgur.com/none.jpg') }.should raise_error(Thornbed::NotValid)
|
31
31
|
end
|
32
|
+
|
33
|
+
it "should raise an error when pattern is not set" do
|
34
|
+
foo = Thornbed::Providers::Provider.new
|
35
|
+
lambda { foo.get('http://imgur.com/none.jpg') }.should raise_error(NotImplementedError)
|
36
|
+
end
|
32
37
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thornbed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ruby-oembed
|
@@ -141,10 +141,13 @@ files:
|
|
141
141
|
- lib/thornbed/errors.rb
|
142
142
|
- lib/thornbed/provider.rb
|
143
143
|
- lib/thornbed/providers.rb
|
144
|
+
- lib/thornbed/providers/9gag.rb
|
144
145
|
- lib/thornbed/providers/imgur.rb
|
145
146
|
- lib/thornbed/providers/memegenerator.rb
|
146
147
|
- lib/thornbed/providers/quickmeme.rb
|
147
148
|
- lib/thornbed/version.rb
|
149
|
+
- spec/provider_spec.rb
|
150
|
+
- spec/providers/9gag_spec.rb
|
148
151
|
- spec/providers/imgur_spec.rb
|
149
152
|
- spec/providers/memegenerator_spec.rb
|
150
153
|
- spec/providers/quickmeme_spec.rb
|
@@ -165,7 +168,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
165
168
|
version: '0'
|
166
169
|
segments:
|
167
170
|
- 0
|
168
|
-
hash: -
|
171
|
+
hash: -965882264884968861
|
169
172
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
173
|
none: false
|
171
174
|
requirements:
|
@@ -174,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
177
|
version: '0'
|
175
178
|
segments:
|
176
179
|
- 0
|
177
|
-
hash: -
|
180
|
+
hash: -965882264884968861
|
178
181
|
requirements: []
|
179
182
|
rubyforge_project:
|
180
183
|
rubygems_version: 1.8.24
|
@@ -183,6 +186,8 @@ specification_version: 3
|
|
183
186
|
summary: Thornbed provides a single interface to get embeddable content from many
|
184
187
|
sites, even sites with no OEmbed support.
|
185
188
|
test_files:
|
189
|
+
- spec/provider_spec.rb
|
190
|
+
- spec/providers/9gag_spec.rb
|
186
191
|
- spec/providers/imgur_spec.rb
|
187
192
|
- spec/providers/memegenerator_spec.rb
|
188
193
|
- spec/providers/quickmeme_spec.rb
|