thornbed 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/thornbed/provider.rb +2 -0
- data/lib/thornbed/providers/imgur.rb +22 -6
- data/lib/thornbed/version.rb +1 -1
- data/spec/providers/imgur_spec.rb +10 -7
- data/thornbed.gemspec +1 -0
- metadata +20 -4
data/lib/thornbed/provider.rb
CHANGED
@@ -1,25 +1,41 @@
|
|
1
1
|
require "uri"
|
2
2
|
require "thornbed"
|
3
|
+
require "httparty"
|
4
|
+
|
3
5
|
|
4
6
|
module Thornbed::Providers
|
5
7
|
class Imgur < Thornbed::Providers::Provider
|
6
8
|
attr_accessor :pattern
|
7
9
|
|
8
10
|
def initialize
|
9
|
-
self.pattern = /^https?:\/\/(www\.|i\.)?imgur\.com\/(gallery\/)?(?<pid>\w{5,})(
|
11
|
+
self.pattern = /^https?:\/\/(www\.|i\.)?imgur\.com\/(gallery\/)?(?<pid>\w{5,})(?<ptype>\.gif|\.jpg|\.jpeg|\.png)?(\?full)?$/
|
10
12
|
end
|
11
13
|
|
12
14
|
def get(url)
|
13
15
|
raise Thornbed::NotValid, url if !valid?(url)
|
14
|
-
res = pattern.match(url)
|
15
|
-
pid = res[:pid]
|
16
|
-
md = /(?<pid>\w{5,})(s|l|b|m|t)$/.match(pid)
|
17
|
-
pid = md[:pid] if md else pid
|
18
|
-
ptype = res[:ptype] || ".jpg"
|
19
16
|
|
17
|
+
# imgur pic_id is ambiguous since the chars for thumb etc... can be part of the id
|
18
|
+
# so we check the embed page
|
19
|
+
# first get the pseudo id
|
20
|
+
md = pattern.match(url)
|
21
|
+
pid = md[:pid]
|
22
|
+
# TODO: improve this code
|
23
|
+
if pid.length > 5
|
24
|
+
pid = pid.gsub(/(h|m|l|s)?$/, '')
|
25
|
+
end
|
26
|
+
raise Thornbed::NotValid, url if not pid
|
27
|
+
|
28
|
+
res = HTTParty.get("http://imgur.com/#{pid}?tags", headers: {"User-Agent" => USER_AGENT})
|
29
|
+
|
30
|
+
# now get the real pid and ptype from that page
|
31
|
+
md = /id\=\"nondirect\"[ ]+value\=\"http:\/\/imgur.com\/(?<pid>\w+)/.match(res.body)
|
32
|
+
pid = md[:pid]
|
20
33
|
|
21
34
|
raise Thornbed::NotValid, url if not pid
|
22
35
|
|
36
|
+
md = /id\=\"direct\"[ ]+value\=\"http:\/\/i.imgur.com\/\w+(?<ptype>\.jpg|\.jpeg|\.png|\.gif)/.match(res.body)
|
37
|
+
ptype = md[:ptype] || ".jpg"
|
38
|
+
|
23
39
|
{
|
24
40
|
url: "http://i.imgur.com/#{pid}#{ptype}",
|
25
41
|
type: "photo",
|
data/lib/thornbed/version.rb
CHANGED
@@ -18,6 +18,7 @@ describe "Imgur" do
|
|
18
18
|
imgur.valid?('http://i.imgur.com/A0TP9s.jpg').should be_true
|
19
19
|
imgur.valid?('http://www.imgur.com/RUUuR.jpeg').should be_true
|
20
20
|
imgur.valid?('http://imgur.com/LcbbCHv').should be_true
|
21
|
+
imgur.valid?('http://imgur.com/0TczaPb').should be_true
|
21
22
|
imgur.valid?('http://imgur.com/none').should be_false
|
22
23
|
imgur.valid?('http://imgur.com/none.jpg').should be_false
|
23
24
|
imgur.valid?('http://imgur.com/a/pq0N2').should be_false
|
@@ -26,14 +27,13 @@ describe "Imgur" do
|
|
26
27
|
it "should return OEmbed hash for imgur urls" do
|
27
28
|
imgur = Thornbed::Providers::Imgur.new
|
28
29
|
res = imgur.get('http://imgur.com/gallery/A0TP9')
|
29
|
-
puts "*** #{res}"
|
30
30
|
res.should include(url: 'http://i.imgur.com/A0TP9.jpg')
|
31
31
|
|
32
32
|
res = imgur.get('http://i.imgur.com/A0TP9.png')
|
33
|
-
res.should include(url: 'http://i.imgur.com/A0TP9.
|
33
|
+
res.should include(url: 'http://i.imgur.com/A0TP9.jpg')
|
34
34
|
|
35
35
|
res = imgur.get('http://i.imgur.com/A0TP9.gif')
|
36
|
-
res.should include(url: 'http://i.imgur.com/A0TP9.
|
36
|
+
res.should include(url: 'http://i.imgur.com/A0TP9.jpg')
|
37
37
|
|
38
38
|
res = imgur.get('http://i.imgur.com/A0TP9.jpg')
|
39
39
|
res.should include(url: 'http://i.imgur.com/A0TP9.jpg')
|
@@ -54,13 +54,16 @@ describe "Imgur" do
|
|
54
54
|
res.should include(url: 'http://i.imgur.com/A0TP9.jpg')
|
55
55
|
|
56
56
|
res = imgur.get('http://www.imgur.com/RUUuR.jpeg')
|
57
|
-
res.should include(url: 'http://i.imgur.com/RUUuR.
|
57
|
+
res.should include(url: 'http://i.imgur.com/RUUuR.jpg')
|
58
58
|
|
59
59
|
res = imgur.get('http://imgur.com/LcbbCHv')
|
60
|
-
res.should include(url: 'http://i.imgur.com/LcbbCHv.
|
60
|
+
res.should include(url: 'http://i.imgur.com/LcbbCHv.png')
|
61
61
|
|
62
62
|
res = imgur.get('http://i.imgur.com/LcbbCHvs.jpg')
|
63
|
-
res.should include(url: 'http://i.imgur.com/LcbbCHv.
|
63
|
+
res.should include(url: 'http://i.imgur.com/LcbbCHv.png')
|
64
|
+
|
65
|
+
res = imgur.get('http://imgur.com/0TczaPb')
|
66
|
+
res.should include(url: 'http://i.imgur.com/0TczaPb.jpg')
|
64
67
|
|
65
68
|
lambda { imgur.get('http://imgur.com/none') }.should raise_error(Thornbed::NotValid)
|
66
69
|
lambda { imgur.get('http://imgur.com/none.jpg') }.should raise_error(Thornbed::NotValid)
|
@@ -69,7 +72,7 @@ describe "Imgur" do
|
|
69
72
|
|
70
73
|
it "should discover Imgur provider" do
|
71
74
|
res = Thornbed.get('http://imgur.com/N2IDN')
|
72
|
-
res.should include(url: 'http://i.imgur.com/N2IDN.
|
75
|
+
res.should include(url: 'http://i.imgur.com/N2IDN.gif')
|
73
76
|
res.should include(provider_name: "Imgur")
|
74
77
|
end
|
75
78
|
end
|
data/thornbed.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
19
|
gem.add_runtime_dependency "ruby-oembed", "~> 0.8.7"
|
20
|
+
gem.add_runtime_dependency "httparty", "~> 0.10.2"
|
20
21
|
gem.add_development_dependency "rspec", "~> 2.12.0"
|
21
22
|
gem.add_development_dependency "guard-rspec", "~> 2.1.1"
|
22
23
|
gem.add_development_dependency "rb-fsevent", "~> 0.9.2"
|
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.3.
|
4
|
+
version: 0.3.2
|
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: 2013-02-
|
12
|
+
date: 2013-02-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: ruby-oembed
|
@@ -27,6 +27,22 @@ dependencies:
|
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 0.8.7
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: httparty
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.10.2
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.10.2
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: rspec
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -170,7 +186,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
170
186
|
version: '0'
|
171
187
|
segments:
|
172
188
|
- 0
|
173
|
-
hash:
|
189
|
+
hash: 2668191655535075852
|
174
190
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
175
191
|
none: false
|
176
192
|
requirements:
|
@@ -179,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
179
195
|
version: '0'
|
180
196
|
segments:
|
181
197
|
- 0
|
182
|
-
hash:
|
198
|
+
hash: 2668191655535075852
|
183
199
|
requirements: []
|
184
200
|
rubyforge_project:
|
185
201
|
rubygems_version: 1.8.24
|