snip-snap 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,18 +5,18 @@
5
5
  SnipSnap is a Ruby library that will allow you to extract the URL of an image from
6
6
  one of many popular image sharing services. Just give it the shortened URL and it can
7
7
  give you the full URL to the embedded image. This library currently supports Img.ly,
8
- Skitch, Twitpic, and Yfrog - others are coming.
8
+ Skitch, Twitpic, Flickr, and Yfrog - others are coming.
9
9
 
10
10
  == Installation
11
11
 
12
- sudo gem install snip-snap
12
+ sudo gem install snip-snap --source=http://gemcutter.org
13
13
 
14
14
  == Usage
15
15
 
16
16
  require 'rubygems
17
17
  require 'snip_snap'
18
18
 
19
- client = SnipSnap.factory('http://yfrog.com/7hb9lj')
19
+ client = SnipSnap.from_url('http://yfrog.com/7hb9lj')
20
20
  puts client.image_url
21
21
 
22
22
  == License
data/Rakefile CHANGED
@@ -18,6 +18,7 @@ spec = Gem::Specification.new do |s|
18
18
  # s.executables = ['snip-snap']
19
19
 
20
20
  s.add_dependency('curb', '>= 0.5.1.0')
21
+ s.add_dependency('fleakr', '>= 0.5.1')
21
22
  end
22
23
 
23
24
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -1,6 +1,7 @@
1
1
  $:.unshift File.dirname(__FILE__)
2
2
 
3
3
  require 'curb'
4
+ require 'fleakr'
4
5
  require 'uri'
5
6
 
6
7
  require 'snip_snap/client'
@@ -9,6 +10,7 @@ require 'snip_snap/skitch'
9
10
  require 'snip_snap/imgly'
10
11
  require 'snip_snap/yfrog'
11
12
  require 'snip_snap/twitpic'
13
+ require 'snip_snap/flickr'
12
14
 
13
15
  # = SnipSnap
14
16
  #
@@ -19,13 +21,14 @@ require 'snip_snap/twitpic'
19
21
  # * Skitch
20
22
  # * Twitpic
21
23
  # * Yfrog
24
+ # * Flickr
22
25
  #
23
26
  # To use, just point it at a URL:
24
27
  #
25
28
  # require 'rubygems'
26
29
  # require 'snip_snap'
27
30
  #
28
- # client = SnipSnap.factory('http://yfrog.com/7hb9lj')
31
+ # client = SnipSnap.from_url('http://yfrog.com/7hb9lj')
29
32
  # puts client.image_url
30
33
  #
31
34
  # That's it.
@@ -37,12 +40,13 @@ module SnipSnap
37
40
  'skitch.com' => 'Skitch',
38
41
  'img.ly' => 'Imgly',
39
42
  'twitpic.com' => 'Twitpic',
40
- 'yfrog.com' => 'Yfrog'
43
+ 'yfrog.com' => 'Yfrog',
44
+ 'flic.kr' => 'Flickr'
41
45
  }
42
46
  end
43
47
 
44
48
  # Use the correct class to handle image extraction for a given URL
45
- def self.factory(url)
49
+ def self.from_url(url)
46
50
  const_get(class_name_for(url)).new(url)
47
51
  end
48
52
 
@@ -51,4 +55,9 @@ module SnipSnap
51
55
  host_map[uri.host]
52
56
  end
53
57
 
58
+ # Set the Flickr API key for use by the underlying Flickr API library
59
+ def self.flickr_api_key=(key)
60
+ Fleakr.api_key = key
61
+ end
62
+
54
63
  end
@@ -9,35 +9,32 @@ module SnipSnap
9
9
  @url = url
10
10
  end
11
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)
12
+ def fetch
21
13
  client = Curl::Easy.new(url) do |config|
22
14
  config.follow_location = true
23
- config.max_redirects = 5
15
+ config.max_redirects = 5
16
+ config.head = self.class.head?
24
17
  end
25
18
 
26
- block.call(client)
19
+ client.perform
27
20
 
28
21
  client
29
22
  end
23
+
24
+ def response
25
+ @response ||= fetch
26
+ end
30
27
 
31
28
  end
32
29
 
33
30
  module ClassMethods
34
31
 
35
32
  def request_method(method_name)
36
- class_eval <<-CODE
37
- def response
38
- @response ||= #{method_name}
39
- end
40
- CODE
33
+ @request_method = method_name
34
+ end
35
+
36
+ def head?
37
+ @request_method == :head
41
38
  end
42
39
 
43
40
  end
@@ -0,0 +1,22 @@
1
+ module SnipSnap
2
+ class Flickr
3
+
4
+ include Client
5
+
6
+ request_method :head
7
+
8
+ def identifier
9
+ url = response.last_effective_url
10
+ url.match(/([^\/]+)\/$/)[1]
11
+ end
12
+
13
+ def photo
14
+ Fleakr::Objects::Photo.find_by_id(identifier)
15
+ end
16
+
17
+ def image_url
18
+ photo.medium.url
19
+ end
20
+
21
+ end
22
+ end
@@ -3,7 +3,7 @@ module SnipSnap
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 0
6
+ TINY = 1
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -19,84 +19,85 @@ module SnipSnap
19
19
 
20
20
  context "An instance of the ClientImplementation class" do
21
21
 
22
- should "be able to make a get request" do
23
- url = 'http://example.com'
22
+ should "be able to fetch a response" do
23
+ response = stub()
24
24
 
25
- client = mock() do |c|
26
- c.expects(:http_get).with()
27
- end
25
+ c = ClientImplementation.new('http://example.com')
26
+ c.expects(:fetch).once.with().returns(response)
28
27
 
29
- config = mock() do |c|
30
- c.expects(:follow_location=).with(true)
31
- c.expects(:max_redirects=).with(5)
32
- end
28
+ c.response.should == response
29
+ end
30
+
31
+ should "cache the response object" do
32
+ response = stub()
33
33
 
34
- Curl::Easy.expects(:new).with(url).yields(config).returns(client)
34
+ c = ClientImplementation.new('http://example.com')
35
+ c.expects(:fetch).once.with().returns(response)
35
36
 
36
- c = ClientImplementation.new(url)
37
- c.get.should == client
37
+ 2.times { c.response }
38
38
  end
39
39
 
40
- should "be able to make a head request" do
40
+ end
41
+
42
+ context "The ClientGetImplementation class" do
43
+
44
+ should "know that it doesn't make a head request" do
45
+ ClientGetImplementation.head?.should be(false)
46
+ end
47
+
48
+ end
49
+
50
+ context "An instance of the ClientGetImplementation class" do
51
+
52
+ should "fetch a response using a GET request" do
41
53
  url = 'http://example.com'
42
54
 
43
55
  client = mock() do |c|
44
- c.expects(:http_head).with()
56
+ c.expects(:perform).with()
45
57
  end
46
58
 
47
59
  config = mock() do |c|
48
60
  c.expects(:follow_location=).with(true)
49
61
  c.expects(:max_redirects=).with(5)
62
+ c.expects(:head=).with(false)
50
63
  end
51
64
 
52
65
  Curl::Easy.expects(:new).with(url).yields(config).returns(client)
53
66
 
54
- c = ClientImplementation.new(url)
55
- c.head.should == client
67
+ c = ClientGetImplementation.new(url)
68
+ c.fetch.should == client
56
69
  end
57
70
 
58
71
  end
59
72
 
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 }
73
+
74
+ context "The ClientHeadImplementation class" do
75
+
76
+ should "know that it doesn't makes a head request" do
77
+ ClientHeadImplementation.head?.should be(true)
78
78
  end
79
79
 
80
80
  end
81
81
 
82
82
  context "An instance of the ClientHeadImplementation class" do
83
83
 
84
- should "fetch a response using a GET request when calling response" do
85
- response = stub()
84
+ should "fetch a response using a HEAD request" do
85
+ url = 'http://example.com'
86
86
 
87
- c = ClientHeadImplementation.new('http://example.com')
88
- c.expects(:head).with().returns(response)
87
+ client = mock() do |c|
88
+ c.expects(:perform).with()
89
+ end
89
90
 
90
- c.response.should == response
91
- end
92
-
93
- should "cache the response object" do
94
- response = stub()
91
+ config = mock() do |c|
92
+ c.expects(:follow_location=).with(true)
93
+ c.expects(:max_redirects=).with(5)
94
+ c.expects(:head=).with(true)
95
+ end
95
96
 
96
- c = ClientHeadImplementation.new('http://example.com')
97
- c.expects(:head).once.with().returns(response)
97
+ Curl::Easy.expects(:new).with(url).yields(config).returns(client)
98
98
 
99
- 2.times { c.response.should }
99
+ c = ClientHeadImplementation.new(url)
100
+ c.fetch.should == client
100
101
  end
101
102
 
102
103
  end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module SnipSnap
4
+ class FlickrTest < Test::Unit::TestCase
5
+
6
+ context "An instance of the Flickr class" do
7
+ setup do
8
+ @url = 'http://flic.kr/p/64cBqN'
9
+ @expanded_url = 'http://www.flickr.com/photos/northernraven/3317998738/'
10
+ end
11
+
12
+ should "know the identifier for the photo" do
13
+ response = stub()
14
+ response.stubs(:last_effective_url).with().returns(@expanded_url)
15
+
16
+ f = SnipSnap::Flickr.new(@url)
17
+ f.stubs(:response).with().returns(response)
18
+
19
+ f.identifier.should == '3317998738'
20
+ end
21
+
22
+ should "be able to find the photo for the identifier" do
23
+ identifier = '3317998738'
24
+ photo = stub()
25
+
26
+ Fleakr::Objects::Photo.expects(:find_by_id).with(identifier).returns(photo)
27
+
28
+ f = SnipSnap::Flickr.new(@url)
29
+ f.stubs(:identifier).with().returns(identifier)
30
+
31
+ f.photo.should == photo
32
+ end
33
+
34
+ should "know the image url" do
35
+ url = 'http://farm.flickr.com/photo.jpg'
36
+
37
+ photo = stub()
38
+ photo.stubs(:medium).with().returns(stub(:url => url))
39
+
40
+ f = SnipSnap::Flickr.new(@url)
41
+ f.stubs(:photo).with().returns(photo)
42
+
43
+ f.image_url.should == url
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
@@ -14,15 +14,6 @@ module SnipSnap
14
14
  i.url.should == @expanded_url
15
15
  end
16
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
17
  should "be able to return an image url for a given url" do
27
18
  response = stub()
28
19
  response.stubs(:last_effective_url).with().returns(@expanded_url)
@@ -11,15 +11,6 @@ module SnipSnap
11
11
  s.url.should == @url
12
12
  end
13
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
14
  should "be able to return an image url for a given url" do
24
15
  s = SnipSnap::Skitch.new(@url)
25
16
 
@@ -14,15 +14,6 @@ module SnipSnap
14
14
  t.url.should == @expanded_url
15
15
  end
16
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
17
  should "be able to return an image url for a given url" do
27
18
  response = stub()
28
19
  response.stubs(:last_effective_url).with().returns(@expanded_url)
@@ -14,15 +14,6 @@ module SnipSnap
14
14
  y.url.should == @expanded_url
15
15
  end
16
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
17
  should "be able to return an image url for a given url" do
27
18
  y = SnipSnap::Yfrog.new(@url)
28
19
 
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../test_helper'
2
2
 
3
3
  class SnipSnapTest < Test::Unit::TestCase
4
4
 
5
- describe "The SnipSnap module" do
5
+ context "The SnipSnap module" do
6
6
 
7
7
  should "know the correct class name for a Skitch URL" do
8
8
  url = 'http://skitch.com/reagent/bh4ei/bleeergh'
@@ -24,32 +24,52 @@ class SnipSnapTest < Test::Unit::TestCase
24
24
  SnipSnap.class_name_for(url).should == 'Yfrog'
25
25
  end
26
26
 
27
+ should "know the correct class name for a Flickr URL" do
28
+ url = 'http://flic.kr/p/64cBqN'
29
+ SnipSnap.class_name_for(url).should == 'Flickr'
30
+ end
31
+
27
32
  should "be able to create an instance of the Skitch class with the supplied URL" do
28
33
  url = 'http://skitch.com/reagent/bh4ei/bleeergh'
29
34
  SnipSnap::Skitch.expects(:new).with(url).returns('skitch')
30
35
 
31
- SnipSnap.factory(url).should == 'skitch'
36
+ SnipSnap.from_url(url).should == 'skitch'
32
37
  end
33
38
 
34
39
  should "be able to create an instance of the Imgly class with the supplied URL" do
35
40
  url = 'http://img.ly/3ey'
36
41
  SnipSnap::Imgly.expects(:new).with(url).returns('imgly')
37
42
 
38
- SnipSnap.factory(url).should == 'imgly'
43
+ SnipSnap.from_url(url).should == 'imgly'
39
44
  end
40
45
 
41
46
  should "be able to create an instance of the Twitpic class with the supplied URL" do
42
47
  url = 'http://twitpic.com/203o0'
43
48
  SnipSnap::Twitpic.expects(:new).with(url).returns('twitpic')
44
49
 
45
- SnipSnap.factory(url).should == 'twitpic'
50
+ SnipSnap.from_url(url).should == 'twitpic'
46
51
  end
47
52
 
48
53
  should "be able to create an instance of the Yfrog class with the supplied URL" do
49
54
  url = 'http://yfrog.com/ahb97j'
50
55
  SnipSnap::Yfrog.expects(:new).with(url).returns('yfrog')
51
56
 
52
- SnipSnap.factory(url).should == 'yfrog'
57
+ SnipSnap.from_url(url).should == 'yfrog'
58
+ end
59
+
60
+ should "be able to create an instance of the Flickr class with the supplied URL" do
61
+ url = 'http://flic.kr/p/64cBqN'
62
+ SnipSnap::Flickr.expects(:new).with(url).returns('flickr')
63
+
64
+ SnipSnap.from_url(url).should == 'flickr'
65
+ end
66
+
67
+ should "be able to set the Flickr API key" do
68
+ key = 'abc123'
69
+
70
+ Fleakr.expects(:api_key=).with(key)
71
+
72
+ SnipSnap.flickr_api_key = key
53
73
  end
54
74
 
55
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snip-snap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Reagan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-02 00:00:00 -04:00
12
+ date: 2009-09-06 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 0.5.1.0
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: fleakr
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.1
34
+ version:
25
35
  description:
26
36
  email: reaganpr@gmail.com
27
37
  executables: []
@@ -34,6 +44,7 @@ files:
34
44
  - README.rdoc
35
45
  - Rakefile
36
46
  - lib/snip_snap/client.rb
47
+ - lib/snip_snap/flickr.rb
37
48
  - lib/snip_snap/imgly.rb
38
49
  - lib/snip_snap/skitch.rb
39
50
  - lib/snip_snap/twitpic.rb
@@ -44,6 +55,7 @@ files:
44
55
  - test/fixtures/yfrog.xml
45
56
  - test/test_helper.rb
46
57
  - test/unit/snip_snap/client_test.rb
58
+ - test/unit/snip_snap/flickr_test.rb
47
59
  - test/unit/snip_snap/imgly_test.rb
48
60
  - test/unit/snip_snap/skitch_test.rb
49
61
  - test/unit/snip_snap/twitpic_test.rb