imagize 0.0.1 → 0.1.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.
- data/VERSION +1 -1
- data/imagize.gemspec +1 -1
- data/lib/imagize.rb +55 -7
- data/spec/imagize_spec.rb +26 -12
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/imagize.gemspec
CHANGED
data/lib/imagize.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
|
1
|
+
require "net/http"
|
2
|
+
require "uri"
|
3
|
+
|
4
|
+
module Imagize
|
5
|
+
|
6
|
+
|
2
7
|
URL_DEFINITIONS = {
|
3
8
|
:twitpic => {
|
4
9
|
:url => "http://twitpic.com/",
|
@@ -16,29 +21,72 @@ module Imagize
|
|
16
21
|
:url => "http://tweetphoto.com/",
|
17
22
|
:convert => "http://tweetphotoapi.com/api/TPAPI.svc/imagefromurl?size=big&url=http://tweetphoto.com/§ID§"
|
18
23
|
}
|
19
|
-
}
|
24
|
+
}
|
25
|
+
SHORTENERS = {
|
26
|
+
:bitly => {
|
27
|
+
:url=> "http://bit.ly/"
|
28
|
+
},
|
29
|
+
:jmp => {
|
30
|
+
:url=> "http://j.mp/"
|
31
|
+
},
|
32
|
+
:isgd =>{
|
33
|
+
:url=>"http://is.gd/"
|
34
|
+
}
|
35
|
+
|
36
|
+
}
|
37
|
+
|
38
|
+
IMAGE_URL = /http:\/\/[.\w]*\/[\/\w]*/
|
39
|
+
# IMAGE_URL = /http:\/\/[.\w]*\/[\/\w]*[\b.jpg\b|\b\.gif\b|\b\.jp\b|]+/
|
40
|
+
JPG_FINDER = /#{IMAGE_URL}.jpg/
|
41
|
+
GIF_FINDER = /#{IMAGE_URL}.gif/
|
42
|
+
PNG_FINDER = /#{IMAGE_URL}.png/
|
43
|
+
|
44
|
+
IMAGE_FINDERS = [JPG_FINDER, GIF_FINDER, PNG_FINDER]
|
20
45
|
|
21
46
|
class Imagizer
|
22
47
|
|
23
48
|
def imagize(message)
|
24
49
|
tweet = message.clone
|
25
50
|
images = Array.new
|
26
|
-
|
51
|
+
|
52
|
+
#find shortened content
|
53
|
+
SHORTENERS.each do |service, details|
|
54
|
+
currentService = details[:url]
|
55
|
+
tweet.scan /#{currentService}\w*/ do |current|
|
56
|
+
tweet = tweet.sub (current, extract_shortener(current))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
#find image services
|
27
61
|
URL_DEFINITIONS.each do |service, details|
|
28
62
|
currentService = details[:url]
|
29
63
|
tweet.scan /#{currentService}\w*/ do |current|
|
30
64
|
code = current.sub(currentService, "")
|
31
|
-
puts code
|
32
65
|
images << make_url (service, code)
|
33
|
-
end
|
34
|
-
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
IMAGE_FINDERS.each do |finder|
|
70
|
+
images += tweet.scan(finder)
|
35
71
|
end
|
72
|
+
|
36
73
|
|
37
74
|
images
|
38
|
-
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def extract_shortener (short_url)
|
78
|
+
uri = URI.parse(short_url)
|
79
|
+
|
80
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
81
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
82
|
+
|
83
|
+
response = http.request(request)
|
84
|
+
response['location']
|
85
|
+
end
|
39
86
|
|
40
87
|
def make_url (servicename, code)
|
41
88
|
URL_DEFINITIONS[servicename][:convert].gsub "§ID§", code
|
42
89
|
end
|
90
|
+
|
43
91
|
end
|
44
92
|
end
|
data/spec/imagize_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe "Imagize" do
|
|
12
12
|
urls[0].should == "http://twitpic.com/show/large/h5uhc"
|
13
13
|
|
14
14
|
urls= @imagizer.imagize("YG family outing y'all!!! http://twitpic.com/h71ip")
|
15
|
-
urls.size.should ==1
|
15
|
+
urls.size.should ==1
|
16
16
|
urls[0].should == "http://twitpic.com/show/large/h71ip"
|
17
17
|
end
|
18
18
|
|
@@ -31,37 +31,51 @@ describe "Imagize" do
|
|
31
31
|
urls= @imagizer.imagize("Hallo bnfgjfdgbfd http://youtu.be/B5IgPI9Dc7A?a\n Fiets")
|
32
32
|
urls.should include("http://img.youtube.com/vi/B5IgPI9Dc7A/0.jpg")
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
it "should recognize tweetphoto" do
|
36
36
|
urls= @imagizer.imagize("Hallo bnfgjfdgbfd http://tweetphoto.com/16793555 Fiets")
|
37
37
|
urls.should include("http://tweetphotoapi.com/api/TPAPI.svc/imagefromurl?size=big&url=http://tweetphoto.com/16793555")
|
38
38
|
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
40
|
it "should recognize multiple images" do
|
43
41
|
urls= @imagizer.imagize("MitchBHavin @themdudez http://yfrog.com/16y0 @KarenWuvsYou http://twitpic.com/h5uhc - This is so cool:)! lol")
|
44
42
|
urls.size.should ==2
|
45
43
|
urls.should include("http://yfrog.com/16y0:iphone")
|
46
44
|
urls.should include"http://twitpic.com/show/large/h5uhc"
|
47
45
|
end
|
48
|
-
|
46
|
+
|
49
47
|
it "should recognize shortened youtube urls" do
|
50
|
-
urls= @imagizer.imagize "Long text... youtu.be/1acVM7_rWw4 and more text"
|
48
|
+
urls= @imagizer.imagize "Long text... http://youtu.be/1acVM7_rWw4 and more text"
|
51
49
|
urls.size.should == 1
|
52
50
|
|
53
51
|
urls[0].should == "http://img.youtube.com/vi/1acVM7_rWw4/0.jpg"
|
54
52
|
end
|
55
53
|
|
56
|
-
it "should create
|
54
|
+
it "should create normal urls" do
|
57
55
|
@imagizer.make_url(:twitpic, "hi").should == "http://twitpic.com/show/large/hi"
|
58
56
|
@imagizer.make_url(:yfrog, "hi").should == "http://yfrog.com/hi:iphone"
|
59
57
|
end
|
60
58
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
59
|
+
it "should extract urls of shorteners" do
|
60
|
+
@imagizer.extract_shortener("http://bit.ly/3EgFOY").should == "http://www.nu.nl"
|
61
|
+
@imagizer.extract_shortener("http://bit.ly/cxGTkx").should == "http://www.facebook.com/XL1067/posts/116716535027115"
|
62
|
+
@imagizer.extract_shortener("http://j.mp/3EgFOY").should == "http://www.nu.nl"
|
63
|
+
@imagizer.extract_shortener("http://is.gd/bSCVo").should == "http://www.nu.nl"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should extract find image in shortened url" do
|
67
|
+
@imagizer.imagize("http://bit.ly/92mRwT").should include("http://twitpic.com/show/large/h5uhc")
|
68
|
+
end
|
65
69
|
|
66
70
|
|
67
|
-
|
71
|
+
it "should recognize jpg/gif" do
|
72
|
+
@imagizer.imagize("This is some message http://www.hi.com/x.jpg").size.should ==1
|
73
|
+
@imagizer.imagize("This is some message http://www.hi.com/1/2/3/x.jpg").size.should ==1
|
74
|
+
@imagizer.imagize("This is some message http://www.hi.com/1/2/3/x.gif").should include "http://www.hi.com/1/2/3/x.gif"
|
75
|
+
@imagizer.imagize("This is some message http://bit.ly/bxjNbr but shortened").should include "http://www.hi.com/1/2/3/x.gif"
|
76
|
+
@imagizer.imagize("This is some message http://bit.ly/bxjNbr but shortened").should include "http://www.hi.com/1/2/3/x.gif"
|
77
|
+
@imagizer.imagize("This is some message http://is.gd/bSD5l but shortened").should include "http://www.hi.com/1/2/3/x.gif"
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
end
|