snip-snap 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,7 @@ require 'snip_snap/imgly'
11
11
  require 'snip_snap/yfrog'
12
12
  require 'snip_snap/twitpic'
13
13
  require 'snip_snap/flickr'
14
+ require 'snip_snap/image'
14
15
 
15
16
  # = SnipSnap
16
17
  #
@@ -37,11 +38,11 @@ module SnipSnap
37
38
 
38
39
  def self.host_map # :nodoc:
39
40
  {
40
- 'skitch.com' => 'Skitch',
41
- 'img.ly' => 'Imgly',
42
- 'twitpic.com' => 'Twitpic',
43
- 'yfrog.com' => 'Yfrog',
44
- 'flic.kr' => 'Flickr'
41
+ 'Skitch' => ['skitch.com'],
42
+ 'Imgly' => ['img.ly'],
43
+ 'Twitpic' => ['twitpic.com'],
44
+ 'Yfrog' => ['yfrog.com', 'yfrog.us'],
45
+ 'Flickr' => ['flic.kr']
45
46
  }
46
47
  end
47
48
 
@@ -52,7 +53,9 @@ module SnipSnap
52
53
 
53
54
  def self.class_name_for(url) # :nodoc:
54
55
  uri = URI.parse(url)
55
- host_map[uri.host]
56
+ match = host_map.detect {|k,v| v.include?(uri.host) }
57
+
58
+ match.nil? ? 'Image' : match[0]
56
59
  end
57
60
 
58
61
  # Set the Flickr API key for use by the underlying Flickr API library
@@ -8,6 +8,10 @@ module SnipSnap
8
8
  def initialize(url)
9
9
  @url = url
10
10
  end
11
+
12
+ def image?
13
+ true
14
+ end
11
15
 
12
16
  def fetch
13
17
  client = Curl::Easy.new(url) do |config|
@@ -0,0 +1,31 @@
1
+ module SnipSnap
2
+ class Image
3
+
4
+ MIME_TYPES = %w(image/jpeg image/gif image/pjpeg image/png)
5
+ EXTENSIONS = %w(jpeg jpg gif png)
6
+
7
+ include Client
8
+
9
+ request_method :head
10
+
11
+ def mime_type
12
+ response.content_type
13
+ end
14
+
15
+ def extension
16
+ uri = URI.parse(url)
17
+ extension = File.extname(uri.path).sub(/./, '')
18
+
19
+ extension unless extension == ''
20
+ end
21
+
22
+ def image?
23
+ MIME_TYPES.include?(mime_type) || EXTENSIONS.include?(extension)
24
+ end
25
+
26
+ def image_url
27
+ url if image?
28
+ end
29
+
30
+ end
31
+ end
@@ -3,7 +3,7 @@ module SnipSnap
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- TINY = 1
6
+ TINY = 2
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
@@ -9,6 +9,11 @@ module SnipSnap
9
9
  @expanded_url = 'http://www.flickr.com/photos/northernraven/3317998738/'
10
10
  end
11
11
 
12
+ should "know that it is an image" do
13
+ f = SnipSnap::Flickr.new(@url)
14
+ f.should be_image
15
+ end
16
+
12
17
  should "know the identifier for the photo" do
13
18
  response = stub()
14
19
  response.stubs(:last_effective_url).with().returns(@expanded_url)
@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/../../test_helper'
2
+
3
+ module SnipSnap
4
+ class ImageTest < Test::Unit::TestCase
5
+
6
+ MIME_TYPES = %w(image/jpeg image/gif image/pjpeg image/png)
7
+ EXTENSIONS = %w(jpeg jpg gif png)
8
+
9
+ context "An instance of the Image class" do
10
+ setup { @url = 'http://example.com/image.jpg' }
11
+
12
+ should "know the MIME type of the file referenced by the URL" do
13
+ response = stub() {|r| r.stubs(:content_type).with().returns('image/jpeg') }
14
+
15
+ i = SnipSnap::Image.new(@url)
16
+ i.stubs(:response).with().returns(response)
17
+
18
+ i.mime_type.should == 'image/jpeg'
19
+ end
20
+
21
+ should "know the extension of the file referenced by the URL" do
22
+ i = SnipSnap::Image.new('http://example.com/image.jpg')
23
+ i.extension.should == 'jpg'
24
+ end
25
+
26
+ should "know that the extension is nil if the URL has no file extension" do
27
+ i = SnipSnap::Image.new('http://example.com/path')
28
+ i.extension.should be_nil
29
+ end
30
+
31
+ should "know that the URL is not an image if it is of type 'text/html'" do
32
+ i = SnipSnap::Image.new(@url)
33
+ i.stubs(:extension).with().returns(nil)
34
+ i.stubs(:mime_type).with().returns('text/html')
35
+
36
+ i.should_not be_image
37
+ end
38
+
39
+ should "know that the URL is not an image if it has a '.html' extension" do
40
+ i = SnipSnap::Image.new(@url)
41
+ i.stubs(:extension).with().returns('html')
42
+ i.stubs(:mime_type).with().returns(nil)
43
+
44
+ i.should_not be_image
45
+ end
46
+
47
+ MIME_TYPES.each do |type|
48
+ should "know that the URL is an image if it is of type '#{type}'" do
49
+ i = SnipSnap::Image.new(@url)
50
+ i.stubs(:mime_type).with().returns(type)
51
+ i.stubs(:extension).with().returns(nil)
52
+
53
+ i.should be_image
54
+ end
55
+ end
56
+
57
+ EXTENSIONS.each do |extension|
58
+ should "know that the URL is an image if it has a '.#{extension}' extension" do
59
+ i = SnipSnap::Image.new(@url)
60
+ i.stubs(:mime_type).with().returns(nil)
61
+ i.stubs(:extension).with().returns(extension)
62
+
63
+ i.should be_image
64
+ end
65
+ end
66
+
67
+ should "know the URL for the image" do
68
+ i = SnipSnap::Image.new(@url)
69
+ i.stubs(:image?).with().returns(true)
70
+
71
+ i.image_url.should == @url
72
+ end
73
+
74
+ should "return nil for the image's URL if it is not an image" do
75
+ i = SnipSnap::Image.new(@url)
76
+ i.stubs(:image?).with().returns(false)
77
+
78
+ i.image_url.should be_nil
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+ end
@@ -9,6 +9,11 @@ module SnipSnap
9
9
  @expanded_url = 'http://img.ly/show/large/3aa'
10
10
  end
11
11
 
12
+ should "know that it is an image" do
13
+ i = SnipSnap::Imgly.new(@url)
14
+ i.should be_image
15
+ end
16
+
12
17
  should "have a url expanded from the source" do
13
18
  i = SnipSnap::Imgly.new(@url)
14
19
  i.url.should == @expanded_url
@@ -6,6 +6,11 @@ module SnipSnap
6
6
  context "An instance of the Skitch class" do
7
7
  setup { @url = 'http://skitch.com/example' }
8
8
 
9
+ should "know that it is an image" do
10
+ s = SnipSnap::Skitch.new(@url)
11
+ s.should be_image
12
+ end
13
+
9
14
  should "have a URL" do
10
15
  s = SnipSnap::Skitch.new(@url)
11
16
  s.url.should == @url
@@ -9,6 +9,11 @@ module SnipSnap
9
9
  @expanded_url = 'http://yfrog.com/api/xmlInfo?path=ahb97j'
10
10
  end
11
11
 
12
+ should "know that it is an image" do
13
+ y = SnipSnap::Yfrog.new(@url)
14
+ y.should be_image
15
+ end
16
+
12
17
  should "have a url derived from the source URL" do
13
18
  y = SnipSnap::Yfrog.new(@url)
14
19
  y.url.should == @expanded_url
@@ -19,16 +19,26 @@ class SnipSnapTest < Test::Unit::TestCase
19
19
  SnipSnap.class_name_for(url).should == 'Twitpic'
20
20
  end
21
21
 
22
- should "know the correct class name for a Yfrog URL" do
22
+ should "know the correct class name for a Yfrog.com URL" do
23
23
  url = 'http://yfrog.com/ahb97j'
24
24
  SnipSnap.class_name_for(url).should == 'Yfrog'
25
25
  end
26
26
 
27
+ should "know the correct class name for a Yfrog.us URL" do
28
+ url = 'http://yfrog.us/ahb97j'
29
+ SnipSnap.class_name_for(url).should == 'Yfrog'
30
+ end
31
+
27
32
  should "know the correct class name for a Flickr URL" do
28
33
  url = 'http://flic.kr/p/64cBqN'
29
34
  SnipSnap.class_name_for(url).should == 'Flickr'
30
35
  end
31
36
 
37
+ should "use the default class when it can't match on other URLs" do
38
+ url = 'http://example.com/image.jpg'
39
+ SnipSnap.class_name_for(url).should == 'Image'
40
+ end
41
+
32
42
  should "be able to create an instance of the Skitch class with the supplied URL" do
33
43
  url = 'http://skitch.com/reagent/bh4ei/bleeergh'
34
44
  SnipSnap::Skitch.expects(:new).with(url).returns('skitch')
@@ -64,6 +74,13 @@ class SnipSnapTest < Test::Unit::TestCase
64
74
  SnipSnap.from_url(url).should == 'flickr'
65
75
  end
66
76
 
77
+ should "be able to create an instance of the Image class with the supplied URL" do
78
+ url = 'http://example.com/image.jpg'
79
+ SnipSnap::Image.expects(:new).with(url).returns('image')
80
+
81
+ SnipSnap.from_url(url).should == 'image'
82
+ end
83
+
67
84
  should "be able to set the Flickr API key" do
68
85
  key = 'abc123'
69
86
 
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.1
4
+ version: 0.1.2
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-06 00:00:00 -04:00
12
+ date: 2009-09-07 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,7 @@ files:
45
45
  - Rakefile
46
46
  - lib/snip_snap/client.rb
47
47
  - lib/snip_snap/flickr.rb
48
+ - lib/snip_snap/image.rb
48
49
  - lib/snip_snap/imgly.rb
49
50
  - lib/snip_snap/skitch.rb
50
51
  - lib/snip_snap/twitpic.rb
@@ -56,6 +57,7 @@ files:
56
57
  - test/test_helper.rb
57
58
  - test/unit/snip_snap/client_test.rb
58
59
  - test/unit/snip_snap/flickr_test.rb
60
+ - test/unit/snip_snap/image_test.rb
59
61
  - test/unit/snip_snap/imgly_test.rb
60
62
  - test/unit/snip_snap/skitch_test.rb
61
63
  - test/unit/snip_snap/twitpic_test.rb