snip-snap 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,45 @@
1
+ = SnipSnap
2
+
3
+ == Description
4
+
5
+ SnipSnap is a Ruby library that will allow you to extract the URL of an image from
6
+ one of many popular image sharing services. Just give it the shortened URL and it can
7
+ give you the full URL to the embedded image. This library currently supports Img.ly,
8
+ Skitch, Twitpic, and Yfrog - others are coming.
9
+
10
+ == Installation
11
+
12
+ sudo gem install snip-snap
13
+
14
+ == Usage
15
+
16
+ require 'rubygems
17
+ require 'snip_snap'
18
+
19
+ client = SnipSnap.factory('http://yfrog.com/7hb9lj')
20
+ puts client.image_url
21
+
22
+ == License
23
+
24
+ Copyright (c) 2009 Patrick Reagan (reaganpr@gmail.com)
25
+
26
+ Permission is hereby granted, free of charge, to any person
27
+ obtaining a copy of this software and associated documentation
28
+ files (the "Software"), to deal in the Software without
29
+ restriction, including without limitation the rights to use,
30
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
31
+ copies of the Software, and to permit persons to whom the
32
+ Software is furnished to do so, subject to the following
33
+ conditions:
34
+
35
+ The above copyright notice and this permission notice shall be
36
+ included in all copies or substantial portions of the Software.
37
+
38
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
40
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
42
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
43
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
44
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
45
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/snip_snap/version'
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ s.name = 'snip-snap'
9
+ s.version = SnipSnap::Version.to_s
10
+ s.has_rdoc = true
11
+ s.extra_rdoc_files = %w(README.rdoc)
12
+ s.rdoc_options = %w(--main README.rdoc)
13
+ s.summary = "A ruby library that allows you to extract images from popular image-sharing services"
14
+ s.author = 'Patrick Reagan'
15
+ s.email = 'reaganpr@gmail.com'
16
+ s.homepage = 'http://sneaq.net'
17
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,test}/**/*")
18
+ # s.executables = ['snip-snap']
19
+
20
+ s.add_dependency('curb', '>= 0.5.1.0')
21
+ end
22
+
23
+ Rake::GemPackageTask.new(spec) do |pkg|
24
+ pkg.gem_spec = spec
25
+ end
26
+
27
+ Rake::TestTask.new do |t|
28
+ t.libs << 'test'
29
+ t.test_files = FileList["test/**/*_test.rb"]
30
+ t.verbose = true
31
+ end
32
+
33
+ begin
34
+ require 'rcov/rcovtask'
35
+
36
+ Rcov::RcovTask.new(:coverage) do |t|
37
+ t.libs = ['test']
38
+ t.test_files = FileList["test/**/*_test.rb"]
39
+ t.verbose = true
40
+ t.rcov_opts = ['--text-report', "-x #{Gem.path}", '-x /Library/Ruby', '-x /usr/lib/ruby']
41
+ end
42
+
43
+ task :default => :coverage
44
+
45
+ rescue LoadError
46
+ warn "\n**** Install rcov (sudo gem install relevance-rcov) to get coverage stats ****\n"
47
+ task :default => :test
48
+ end
49
+
50
+
51
+ desc 'Generate the gemspec to serve this Gem from Github'
52
+ task :github do
53
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
54
+ File.open(file, 'w') {|f| f << spec.to_ruby }
55
+ puts "Created gemspec: #{file}"
56
+ end
data/lib/snip_snap.rb ADDED
@@ -0,0 +1,54 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'curb'
4
+ require 'uri'
5
+
6
+ require 'snip_snap/client'
7
+
8
+ require 'snip_snap/skitch'
9
+ require 'snip_snap/imgly'
10
+ require 'snip_snap/yfrog'
11
+ require 'snip_snap/twitpic'
12
+
13
+ # = SnipSnap
14
+ #
15
+ # This is a small Ruby library that allows you to extract images from the more popular
16
+ # image sharing services. Currently supported services are:
17
+ #
18
+ # * Img.ly
19
+ # * Skitch
20
+ # * Twitpic
21
+ # * Yfrog
22
+ #
23
+ # To use, just point it at a URL:
24
+ #
25
+ # require 'rubygems'
26
+ # require 'snip_snap'
27
+ #
28
+ # client = SnipSnap.factory('http://yfrog.com/7hb9lj')
29
+ # puts client.image_url
30
+ #
31
+ # That's it.
32
+ #
33
+ module SnipSnap
34
+
35
+ def self.host_map # :nodoc:
36
+ {
37
+ 'skitch.com' => 'Skitch',
38
+ 'img.ly' => 'Imgly',
39
+ 'twitpic.com' => 'Twitpic',
40
+ 'yfrog.com' => 'Yfrog'
41
+ }
42
+ end
43
+
44
+ # Use the correct class to handle image extraction for a given URL
45
+ def self.factory(url)
46
+ const_get(class_name_for(url)).new(url)
47
+ end
48
+
49
+ def self.class_name_for(url) # :nodoc:
50
+ uri = URI.parse(url)
51
+ host_map[uri.host]
52
+ end
53
+
54
+ end
@@ -0,0 +1,51 @@
1
+ module SnipSnap
2
+ module Client # :nodoc:
3
+
4
+ module InstanceMethods
5
+
6
+ attr_reader :url
7
+
8
+ def initialize(url)
9
+ @url = url
10
+ end
11
+
12
+ def get
13
+ get_response {|c| c.http_get }
14
+ end
15
+
16
+ def head
17
+ get_response {|c| c.http_head }
18
+ end
19
+
20
+ def get_response(&block)
21
+ client = Curl::Easy.new(url) do |config|
22
+ config.follow_location = true
23
+ config.max_redirects = 5
24
+ end
25
+
26
+ block.call(client)
27
+
28
+ client
29
+ end
30
+
31
+ end
32
+
33
+ module ClassMethods
34
+
35
+ def request_method(method_name)
36
+ class_eval <<-CODE
37
+ def response
38
+ @response ||= #{method_name}
39
+ end
40
+ CODE
41
+ end
42
+
43
+ end
44
+
45
+ def self.included(other)
46
+ other.send(:include, SnipSnap::Client::InstanceMethods)
47
+ other.send(:extend, SnipSnap::Client::ClassMethods)
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,18 @@
1
+ module SnipSnap
2
+ class Imgly
3
+
4
+ include Client
5
+
6
+ request_method :head
7
+
8
+ def url
9
+ identifier = @url.match(/([^\/]+)$/)[1]
10
+ "http://img.ly/show/large/#{identifier}"
11
+ end
12
+
13
+ def image_url
14
+ response.last_effective_url
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module SnipSnap
2
+ class Skitch
3
+
4
+ include Client
5
+
6
+ request_method :get
7
+
8
+ def image_url
9
+ body = response.body_str
10
+ body.match(/addImage\(\s*'([^']+)'/)[1]
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module SnipSnap
2
+ class Twitpic
3
+
4
+ include Client
5
+
6
+ request_method :head
7
+
8
+ def url
9
+ identifier = @url.match(/([^\/]+)$/)[1]
10
+ "http://twitpic.com/show/large/#{identifier}"
11
+ end
12
+
13
+ def image_url
14
+ response.last_effective_url
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ module SnipSnap
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 0
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module SnipSnap
2
+ class Yfrog
3
+
4
+ include Client
5
+
6
+ request_method :get
7
+
8
+ def url
9
+ identifier = @url.match(/([^\/]+)$/)[1]
10
+ "http://yfrog.com/api/xmlInfo?path=#{identifier}"
11
+ end
12
+
13
+ def image_url
14
+ body = response.body_str
15
+ body.match(/<image_link>(.+)<\/image_link>/)[1]
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,84 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <meta name="KEYWORDS" content="Skitch Web" />
6
+ <meta name="DESCRIPTION" content="Skitch Web" />
7
+ <meta name="robots" content="index,follow" />
8
+ <link rel="icon" href="/favicon.ico" type="image/x-icon" />
9
+ <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
10
+ <title>Skitch.com &gt; reagent &gt; Clinical Reader: Research articles, news and multimedia for doctors, all in one place</title>
11
+ <style type="text/css" media="screen,projection">
12
+ /*<![CDATA[*/
13
+ @import "/css/mySkitch.css?13363";
14
+ /*]]>*/
15
+ </style>
16
+ <script type="text/javascript" src="/js/src/myskitch.composite.js?13363"></script>
17
+ <!-- stylesheet /-->
18
+ <link rel="alternate" type="application/atom+xml" title="Atom 1.0" href="http://skitch.com/feeds/reagent/atom.xml" />
19
+ </head>
20
+ <body id="body" onload="plasq.mySkitch.browser.loader();" onresize="plasq.mySkitch.browser.resize();">
21
+
22
+ <div id="header">
23
+ <div id="logo"><a href="/"><img src="/images/logo.png" border="0" alt="" /></a></div>
24
+ <div id="menu">
25
+ <span class="shadow"><ul><li>sign-up</li> <span>|</span> <li>login</li></ul></span>
26
+ <span class="overlay"><ul><li><a href="/signup/">sign-up</a></li> <span class="i">|</span> <li><a href="/login/">login</a></li></ul></span>
27
+ </div>
28
+ </div>
29
+
30
+ <div id="content"><!-- content /--></div>
31
+ <div id="bottomad"><!-- adzone1 /--></div>
32
+
33
+ <div id="mtlogo"><div id="rightbar"></div><div id="rightbarbottom"><a href="http://www.mediatemple.net" target="_blank" title="Hosted by (mt)"><img src="/images/mt-hosted.png" alt="Hosted by (mt)" border="0" /></a><br /><br /><!-- adzone2 /--><br /><br /><br /><br /></div></div>
34
+
35
+ <div id="footer">
36
+ <div class="left padding5px">&copy; 2007 - 2009 <a href="http://plasq.com" target="_blank">plasq</a>&nbsp;&nbsp;&bull;&nbsp;&nbsp;<a href="http://skitch.com/ToS">Terms of Service</a>&nbsp;&nbsp;&bull;&nbsp;&nbsp;<a href="http://skitch.com/Privacy">Privacy Policy</a>&nbsp;&nbsp;&bull;&nbsp;&nbsp;<a href="http://skitch.com/DMCA">DMCA Notice</a>&nbsp;&nbsp;&bull;&nbsp;&nbsp;<a href="http://help.skitch.com">Support</a>&nbsp;&nbsp;&bull;&nbsp;&nbsp;<a href="http://plasq.com/skitch">Skitch Info</a>&nbsp;&nbsp;&bull;&nbsp;&nbsp;<a href="http://blog.skitch.com">Skitch Blog</a></div>
37
+ <div class="right marginright"><a href="http://plasq.com" target="_blank" title="Visit plasq!"><img src="/images/footer-plasq.png" border="0" alt="Visit plasq!" /></a></div>
38
+ </div>
39
+
40
+ <script type="text/javascript">
41
+ /*<![CDATA[*/
42
+ plasq.mySkitch.sidebar = new plasq.mySkitch.classes.container( 'content' );
43
+ plasq.mySkitch.sidebar.element.className = 'myskitch-image-strip-right';
44
+ plasq.mySkitch.sidebar.addText( '&nbsp;' );
45
+ plasq.mySkitch.backLink = '';
46
+
47
+ plasq.mySkitch.selectCopyContainer = plasq.mySkitch.sidebar.addSelectCopyContainer();
48
+ plasq.mySkitch.selectCopyContainer.addSelectCopy( 'This page', 'http://skitch.com/reagent/baadt/clinical-reader-research-articles-news-and-multimedia-for-doctors-all-in-one-place', 'Link to the page you are looking at <b>right now</b> (Recommended)' );
49
+ plasq.mySkitch.selectCopyContainer.addSelectCopy( 'Image only', 'http://img.skitch.com/20090830-ejnqt1s9car55ju2sdnfirdsdn.jpg', '<b>Image only</b> link. Comments can\'t be made on these' );
50
+ plasq.mySkitch.selectCopyContainer.addSelectCopy( 'Embed', '<div class="thumbnail"><a href="http://skitch.com/reagent/baadt/clinical-reader-research-articles-news-and-multimedia-for-doctors-all-in-one-place"><img src="http://img.skitch.com/20090830-ejnqt1s9car55ju2sdnfirdsdn.preview.jpg" alt="Clinical Reader: Research articles, news and multimedia for doctors, all in one place" /></a><br /><span style="font-family: Lucida Grande, Trebuchet, sans-serif, Helvetica, Arial; font-size: 10px; color: #808080">Uploaded with <a href="http://plasq.com/">plasq</a>\'s <a href="http://skitch.com">Skitch</a>!</span></div>', 'HTML with zoomable <b>thumbnail</b>. (Great for eBay, MySpace blogs etc)' );
51
+ plasq.mySkitch.selectCopyContainer.addSelectCopy( 'Fullsize', '<img src="http://img.skitch.com/20090830-ejnqt1s9car55ju2sdnfirdsdn.jpg" alt="Clinical Reader: Research articles, news and multimedia for doctors, all in one place"/>', 'HTML to show the <b>fullsize</b> image' );
52
+ plasq.mySkitch.selectCopyContainer.addSelectCopy( 'Forum', '[url=http://skitch.com/reagent/baadt/clinical-reader-research-articles-news-and-multimedia-for-doctors-all-in-one-place][img]http://img.skitch.com/20090830-ejnqt1s9car55ju2sdnfirdsdn.preview.jpg[/img][/url][br][url=http://skitch.com/reagent/baadt/clinical-reader-research-articles-news-and-multimedia-for-doctors-all-in-one-place]Click for full size[/url] - [color=#A7A7A7]Uploaded with [url=http://plasq.com]plasq[/url]\'s [url=http://skitch.com]Skitch[/url][/color]', 'For use in web based <b>forums</b>' );
53
+
54
+ $('mtlogo').style.right = '32px';
55
+ $('mtlogo').style.top = '304px';
56
+
57
+ plasq.mySkitch.container = new plasq.mySkitch.classes.container( 'content' );
58
+ plasq.mySkitch.container.addSection( 'Clinical Reader: Research articles, news and multimedia for doctors, all in one place' );
59
+ plasq.mySkitch.container.addText( '<div style="display:block;" class="myskitch-header-notes">Pssst!.. This page is Secret - it can only be seen if you give people the URL</div>' );
60
+ plasq.mySkitch.container.addImage( 'http://img.skitch.com/20090830-ejnqt1s9car55ju2sdnfirdsdn.jpg', '', '', '', false, '472', '459' ).enableInfo().enableSlurp( 'skitch://img.skitch.com/20090830-ejnqt1s9car55ju2sdnfirdsdn.jpg?skitchtitle=Re%3A%20Clinical%20Reader%3A%20Research%20articles%2C%20news%20and%20multimedia%20for%20doctors%2C%20all%20in%20one%20place' ).enableDescription( '&nbsp;' );
61
+ plasq.mySkitch.container.addText( '&nbsp;<br />&nbsp;' );
62
+ plasq.mySkitch.container.addSection( 'Comments' );
63
+ plasq.mySkitch.container.comments = plasq.mySkitch.container.addComments( '77fdb5-801093-1b64d7-a01c34-df9430-dd', 'This image has no comments yet. Be the first!', '0d171c5663e826463cc1f8eb32cdb0fa', true, '0' );
64
+ /*]]>*/
65
+ </script>
66
+ <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
67
+ </script>
68
+ <script type="text/javascript">
69
+ _uacct = "UA-2305120-1";
70
+ urchinTracker();
71
+ </script>
72
+
73
+ <!-- Start Quantcast tag -->
74
+ <script type="text/javascript" src="https://secure.quantserve.com/quant.js"></script>
75
+ <script type="text/javascript">
76
+ _qacct="p-a3PI6F3Y912Tk";
77
+ quantserve();
78
+ </script>
79
+ <noscript>
80
+ <img src="https://secure.quantserve.com/pixel/p-a3PI6F3Y912Tk.gif" style="display: none" height="1" width="1" alt="Quantcast"/></noscript>
81
+ <!-- End Quantcast tag -->
82
+
83
+ </body>
84
+ </html>
@@ -0,0 +1,35 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?><imginfo xmlns="http://ns.imageshack.us/imginfo/7/" version="7" timestamp="1251516300">
2
+ <rating>
3
+ <ratings>0</ratings>
4
+ <avg>0.0</avg>
5
+ </rating>
6
+ <files server="377" bucket="9665">
7
+ <image size="29599" content-type="image/jpeg">b97.jpg</image>
8
+ <thumb size="4042" content-type="image/jpeg">b97.th.jpg</thumb>
9
+ </files>
10
+ <resolution>
11
+ <width>525</width>
12
+ <height>700</height>
13
+ </resolution>
14
+ <class>r</class>
15
+ <visibility>yes</visibility>
16
+ <uploader>
17
+ <ip>38.99.76.246</ip>
18
+ <cookie>1d5927aee7676b56f5fb5e1c6dc386bf</cookie>
19
+ <username>twitter~urbanflex</username>
20
+ </uploader>
21
+ <links>
22
+ <image_link>http://img377.imageshack.us/img377/9665/b97.jpg</image_link>
23
+ <image_html>&lt;a href=&quot;http://img377.imageshack.us/my.php?image=b97.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://img377.imageshack.us/img377/9665/b97.jpg&quot; alt=&quot;Free Image Hosting at www.ImageShack.us&quot; border=&quot;0&quot;/&gt;&lt;/a&gt;</image_html>
24
+ <image_bb>[URL=http://img377.imageshack.us/my.php?image=b97.jpg][IMG]http://img377.imageshack.us/img377/9665/b97.jpg[/IMG][/URL]</image_bb>
25
+ <image_bb2>[url=http://img377.imageshack.us/my.php?image=b97.jpg][img=http://img377.imageshack.us/img377/9665/b97.jpg][/url]</image_bb2>
26
+ <thumb_link>http://img377.imageshack.us/img377/9665/b97.th.jpg</thumb_link>
27
+ <thumb_html>&lt;a href=&quot;http://img377.imageshack.us/my.php?image=b97.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;http://img377.imageshack.us/img377/9665/b97.th.jpg&quot; alt=&quot;Free Image Hosting at www.ImageShack.us&quot; border=&quot;0&quot;/&gt;&lt;/a&gt;</thumb_html>
28
+ <thumb_bb>[URL=http://img377.imageshack.us/my.php?image=b97.jpg][IMG]http://img377.imageshack.us/img377/9665/b97.th.jpg[/IMG][/URL]</thumb_bb>
29
+ <thumb_bb2>[url=http://img377.imageshack.us/my.php?image=b97.jpg][img=http://img377.imageshack.us/img377/9665/b97.th.jpg][/url]</thumb_bb2>
30
+ <yfrog_link>http://yfrog.com/ahb97j</yfrog_link>
31
+ <yfrog_thumb>http://yfrog.com/ahb97j.th.jpg</yfrog_thumb>
32
+ <ad_link>http://img377.imageshack.us/my.php?image=b97.jpg</ad_link>
33
+ <done_page>http://img377.imageshack.us/content.php?page=done&amp;l=img377/9665/b97.jpg</done_page>
34
+ </links>
35
+ </imginfo>
@@ -0,0 +1,15 @@
1
+ # http://sneaq.net/textmate-wtf
2
+ $:.reject! { |e| e.include? 'TextMate' }
3
+
4
+ require 'rubygems'
5
+ require 'throat_punch'
6
+
7
+ require File.dirname(__FILE__) + '/../lib/snip_snap'
8
+
9
+ class Test::Unit::TestCase
10
+
11
+ def read_fixture(filename)
12
+ File.read(File.dirname(__FILE__) + '/fixtures/' + filename)
13
+ end
14
+
15
+ end
@@ -0,0 +1,106 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ class ClientImplementation
4
+ include SnipSnap::Client
5
+ end
6
+
7
+ class ClientGetImplementation
8
+ include SnipSnap::Client
9
+ request_method :get
10
+ end
11
+
12
+ class ClientHeadImplementation
13
+ include SnipSnap::Client
14
+ request_method :head
15
+ end
16
+
17
+ module SnipSnap
18
+ class ClientTest < Test::Unit::TestCase
19
+
20
+ context "An instance of the ClientImplementation class" do
21
+
22
+ should "be able to make a get request" do
23
+ url = 'http://example.com'
24
+
25
+ client = mock() do |c|
26
+ c.expects(:http_get).with()
27
+ end
28
+
29
+ config = mock() do |c|
30
+ c.expects(:follow_location=).with(true)
31
+ c.expects(:max_redirects=).with(5)
32
+ end
33
+
34
+ Curl::Easy.expects(:new).with(url).yields(config).returns(client)
35
+
36
+ c = ClientImplementation.new(url)
37
+ c.get.should == client
38
+ end
39
+
40
+ should "be able to make a head request" do
41
+ url = 'http://example.com'
42
+
43
+ client = mock() do |c|
44
+ c.expects(:http_head).with()
45
+ end
46
+
47
+ config = mock() do |c|
48
+ c.expects(:follow_location=).with(true)
49
+ c.expects(:max_redirects=).with(5)
50
+ end
51
+
52
+ Curl::Easy.expects(:new).with(url).yields(config).returns(client)
53
+
54
+ c = ClientImplementation.new(url)
55
+ c.head.should == client
56
+ end
57
+
58
+ end
59
+
60
+ context "An instance of the ClientGetImplementation class" do
61
+
62
+ should "fetch a response using a GET request when calling response" do
63
+ response = stub()
64
+
65
+ c = ClientGetImplementation.new('http://example.com')
66
+ c.expects(:get).with().returns(response)
67
+
68
+ c.response.should == response
69
+ end
70
+
71
+ should "cache the response object" do
72
+ response = stub()
73
+
74
+ c = ClientGetImplementation.new('http://example.com')
75
+ c.expects(:get).once.with().returns(response)
76
+
77
+ 2.times { c.response.should }
78
+ end
79
+
80
+ end
81
+
82
+ context "An instance of the ClientHeadImplementation class" do
83
+
84
+ should "fetch a response using a GET request when calling response" do
85
+ response = stub()
86
+
87
+ c = ClientHeadImplementation.new('http://example.com')
88
+ c.expects(:head).with().returns(response)
89
+
90
+ c.response.should == response
91
+ end
92
+
93
+ should "cache the response object" do
94
+ response = stub()
95
+
96
+ c = ClientHeadImplementation.new('http://example.com')
97
+ c.expects(:head).once.with().returns(response)
98
+
99
+ 2.times { c.response.should }
100
+ end
101
+
102
+ end
103
+
104
+
105
+ end
106
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module SnipSnap
4
+ class ImglyTest < Test::Unit::TestCase
5
+
6
+ context "An instance of the Imgly class" do
7
+ setup do
8
+ @url = 'http://img.ly/3aa'
9
+ @expanded_url = 'http://img.ly/show/large/3aa'
10
+ end
11
+
12
+ should "have a url expanded from the source" do
13
+ i = SnipSnap::Imgly.new(@url)
14
+ i.url.should == @expanded_url
15
+ end
16
+
17
+ should "use a HEAD request when retrieving the response" do
18
+ response = stub()
19
+
20
+ i = SnipSnap::Imgly.new(@url)
21
+ i.expects(:head).with().returns(response)
22
+
23
+ i.response.should == response
24
+ end
25
+
26
+ should "be able to return an image url for a given url" do
27
+ response = stub()
28
+ response.stubs(:last_effective_url).with().returns(@expanded_url)
29
+
30
+ i = SnipSnap::Imgly.new(@url)
31
+ i.stubs(:response).with().returns(response)
32
+
33
+ i.image_url.should == @expanded_url
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module SnipSnap
4
+ class SkitchTest < Test::Unit::TestCase
5
+
6
+ context "An instance of the Skitch class" do
7
+ setup { @url = 'http://skitch.com/example' }
8
+
9
+ should "have a URL" do
10
+ s = SnipSnap::Skitch.new(@url)
11
+ s.url.should == @url
12
+ end
13
+
14
+ should "use a GET request when retrieving the response" do
15
+ response = stub()
16
+
17
+ s = SnipSnap::Skitch.new(@url)
18
+ s.expects(:get).with().returns(response)
19
+
20
+ s.response.should == response
21
+ end
22
+
23
+ should "be able to return an image url for a given url" do
24
+ s = SnipSnap::Skitch.new(@url)
25
+
26
+ response = stub()
27
+ response.stubs(:body_str).with().returns(read_fixture('skitch.html'))
28
+
29
+ s.stubs(:response).with().returns(response)
30
+
31
+ s.image_url.should == 'http://img.skitch.com/20090830-ejnqt1s9car55ju2sdnfirdsdn.jpg'
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module SnipSnap
4
+ class TwitpicTest < Test::Unit::TestCase
5
+
6
+ context "An instance of the Twitpic class" do
7
+ setup do
8
+ @url = 'http://twitpic.com/203o0'
9
+ @expanded_url = 'http://twitpic.com/show/large/203o0'
10
+ end
11
+
12
+ should "have a url derived from the source URL" do
13
+ t = SnipSnap::Twitpic.new(@url)
14
+ t.url.should == @expanded_url
15
+ end
16
+
17
+ should "use a HEAD request when retrieving the response" do
18
+ response = stub()
19
+
20
+ t = SnipSnap::Twitpic.new(@url)
21
+ t.expects(:head).with().returns(response)
22
+
23
+ t.response.should == response
24
+ end
25
+
26
+ should "be able to return an image url for a given url" do
27
+ response = stub()
28
+ response.stubs(:last_effective_url).with().returns(@expanded_url)
29
+
30
+ t = SnipSnap::Twitpic.new(@url)
31
+ t.stubs(:response).with().returns(response)
32
+
33
+ t.image_url.should == @expanded_url
34
+ end
35
+
36
+
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module SnipSnap
4
+ class YfrogTest < Test::Unit::TestCase
5
+
6
+ context "An instance of the Yfrog class" do
7
+ setup do
8
+ @url = 'http://yfrog.com/ahb97j'
9
+ @expanded_url = 'http://yfrog.com/api/xmlInfo?path=ahb97j'
10
+ end
11
+
12
+ should "have a url derived from the source URL" do
13
+ y = SnipSnap::Yfrog.new(@url)
14
+ y.url.should == @expanded_url
15
+ end
16
+
17
+ should "use a GET request when retrieving the response" do
18
+ response = stub()
19
+
20
+ y = SnipSnap::Yfrog.new(@url)
21
+ y.expects(:get).with().returns(response)
22
+
23
+ y.response.should == response
24
+ end
25
+
26
+ should "be able to return an image url for a given url" do
27
+ y = SnipSnap::Yfrog.new(@url)
28
+
29
+ response = stub()
30
+ response.stubs(:body_str).with().returns(read_fixture('yfrog.xml'))
31
+
32
+ y.stubs(:response).with().returns(response)
33
+
34
+ y.image_url.should == 'http://img377.imageshack.us/img377/9665/b97.jpg'
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class SnipSnapTest < Test::Unit::TestCase
4
+
5
+ describe "The SnipSnap module" do
6
+
7
+ should "know the correct class name for a Skitch URL" do
8
+ url = 'http://skitch.com/reagent/bh4ei/bleeergh'
9
+ SnipSnap.class_name_for(url).should == 'Skitch'
10
+ end
11
+
12
+ should "know the correct class name for an Imgly URL" do
13
+ url = 'http://img.ly/3ey'
14
+ SnipSnap.class_name_for(url).should == 'Imgly'
15
+ end
16
+
17
+ should "know the correct class name for a Twitpic URL" do
18
+ url = 'http://twitpic.com/203o0'
19
+ SnipSnap.class_name_for(url).should == 'Twitpic'
20
+ end
21
+
22
+ should "know the correct class name for a Yfrog URL" do
23
+ url = 'http://yfrog.com/ahb97j'
24
+ SnipSnap.class_name_for(url).should == 'Yfrog'
25
+ end
26
+
27
+ should "be able to create an instance of the Skitch class with the supplied URL" do
28
+ url = 'http://skitch.com/reagent/bh4ei/bleeergh'
29
+ SnipSnap::Skitch.expects(:new).with(url).returns('skitch')
30
+
31
+ SnipSnap.factory(url).should == 'skitch'
32
+ end
33
+
34
+ should "be able to create an instance of the Imgly class with the supplied URL" do
35
+ url = 'http://img.ly/3ey'
36
+ SnipSnap::Imgly.expects(:new).with(url).returns('imgly')
37
+
38
+ SnipSnap.factory(url).should == 'imgly'
39
+ end
40
+
41
+ should "be able to create an instance of the Twitpic class with the supplied URL" do
42
+ url = 'http://twitpic.com/203o0'
43
+ SnipSnap::Twitpic.expects(:new).with(url).returns('twitpic')
44
+
45
+ SnipSnap.factory(url).should == 'twitpic'
46
+ end
47
+
48
+ should "be able to create an instance of the Yfrog class with the supplied URL" do
49
+ url = 'http://yfrog.com/ahb97j'
50
+ SnipSnap::Yfrog.expects(:new).with(url).returns('yfrog')
51
+
52
+ SnipSnap.factory(url).should == 'yfrog'
53
+ end
54
+
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snip-snap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Reagan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-02 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: curb
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.5.1.0
24
+ version:
25
+ description:
26
+ email: reaganpr@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.rdoc
33
+ files:
34
+ - README.rdoc
35
+ - Rakefile
36
+ - lib/snip_snap/client.rb
37
+ - lib/snip_snap/imgly.rb
38
+ - lib/snip_snap/skitch.rb
39
+ - lib/snip_snap/twitpic.rb
40
+ - lib/snip_snap/version.rb
41
+ - lib/snip_snap/yfrog.rb
42
+ - lib/snip_snap.rb
43
+ - test/fixtures/skitch.html
44
+ - test/fixtures/yfrog.xml
45
+ - test/test_helper.rb
46
+ - test/unit/snip_snap/client_test.rb
47
+ - test/unit/snip_snap/imgly_test.rb
48
+ - test/unit/snip_snap/skitch_test.rb
49
+ - test/unit/snip_snap/twitpic_test.rb
50
+ - test/unit/snip_snap/yfrog_test.rb
51
+ - test/unit/snip_snap_test.rb
52
+ has_rdoc: true
53
+ homepage: http://sneaq.net
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --main
59
+ - README.rdoc
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project:
77
+ rubygems_version: 1.3.4
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: A ruby library that allows you to extract images from popular image-sharing services
81
+ test_files: []
82
+