acts_as_link 0.0.1 → 0.0.2

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.
@@ -3,6 +3,8 @@ require 'uri'
3
3
 
4
4
  class Link
5
5
 
6
+ IMAGE_EXTENSIONS = %w(.bmp .gif .ico .jpeg .jpg .png .psd .svg .tif .tiff .xcf)
7
+
6
8
  def initialize(url)
7
9
  @uri = URI.parse(url)
8
10
  raise 'Link is not an url' if @uri.class != URI::HTTP
@@ -13,6 +15,11 @@ class Link
13
15
  response_code_is_not_valid?(response.code)
14
16
  end
15
17
 
18
+ def is_an_image?
19
+ extension = get_extension(@uri.to_s)
20
+ verify_if_extension_is_an_image(extension)
21
+ end
22
+
16
23
  private
17
24
 
18
25
  def response_code_is_not_valid?(code)
@@ -34,5 +41,21 @@ private
34
41
  def code_is_an_error_code(code)
35
42
  code[0..0] == "4" || code[0..0] == "5"
36
43
  end
44
+
45
+ def get_extension(uri)
46
+ point_position = uri.rindex('.')
47
+ uri_length = uri.length
48
+ point_position > (uri_length - 6) ? uri[point_position..uri_length] : nil
49
+ end
50
+
51
+ def verify_if_extension_is_an_image(extension)
52
+ return false if extension.nil?
53
+ is_an_image = false
54
+ IMAGE_EXTENSIONS.each do |ext|
55
+ is_an_image = (extension == ext)
56
+ break if is_an_image
57
+ end
58
+ is_an_image
59
+ end
37
60
 
38
61
  end
@@ -13,8 +13,8 @@ describe Link do
13
13
  lambda{Link.new('')}.should raise_error(RuntimeError,/Link is not an url/)
14
14
  end
15
15
 
16
- it 'should return an exception when try to initialize with a number' do
17
- lambda{Link.new('123')}.should raise_error(RuntimeError,/Link is not an url/)
16
+ it 'should return an exception when try to initialize with a string is not a link' do
17
+ lambda{Link.new('test.com.br')}.should raise_error(RuntimeError,/Link is not an url/)
18
18
  end
19
19
 
20
20
  end
@@ -32,4 +32,35 @@ describe Link do
32
32
  end
33
33
 
34
34
  end
35
+
36
+ describe 'is_an_image?' do
37
+
38
+ it 'should return true when url links to an image' do
39
+ link = Link.new('http://www.google.com/images/srpr/nav_logo35.png')
40
+ link.is_an_image?.should be_true
41
+ end
42
+
43
+ it 'should return false when url does not link to an image' do
44
+ link = Link.new('http://www.google.com')
45
+ link.is_an_image?.should be_false
46
+ end
47
+
48
+ it 'should return false when url links to a pdf file' do
49
+ link = Link.new('http://qualidadebr.files.wordpress.com/2009/06/livro-qualidadebr.pdf')
50
+ link.is_an_image?.should be_false
51
+ end
52
+
53
+ it 'should return false when url extension image is not in the end' do
54
+ link = Link.new('http://www.google.com/images/srpr/nav_.pnglogo35')
55
+ link.is_an_image?.should be_false
56
+ end
57
+
58
+ it 'should return true for all image extensions defined' do
59
+ Link::IMAGE_EXTENSIONS.each do |ext|
60
+ link = Link.new("http://www.google.com/images/srpr/nav_logo35#{ext}")
61
+ link.is_an_image?.should be_true
62
+ end
63
+ end
64
+
65
+ end
35
66
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_link
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Fabr\xC3\xADcio Ferrari de Campos"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-12 00:00:00 -02:00
18
+ date: 2011-02-13 00:00:00 -02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -34,7 +34,7 @@ dependencies:
34
34
  version: 1.3.0
35
35
  type: :development
36
36
  version_requirements: *id001
37
- description: For now only verification if link is broken
37
+ description: For now only verification if link is broken and if it is to an image
38
38
  email: fabricio@vizir.com.br
39
39
  executables: []
40
40