acts_as_link 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,71 +3,73 @@ require 'uri'
3
3
  require 'rubygems'
4
4
  require 'bitly'
5
5
 
6
- class Link
7
-
8
- IMAGE_EXTENSIONS = %w(.bmp .gif .ico .jpeg .jpg .png .psd .svg .tif .tiff .xcf)
9
-
10
- def initialize(url)
11
- @uri = URI.parse(url)
12
- raise 'Link is not an url' if (@uri.class != URI::HTTP && @uri.class != URI::HTTPS)
13
- end
14
-
15
- def is_broken?
16
- response = Net::HTTP.start(@uri.host, @uri.port) { |http| response = http.head(@uri.path.size > 0 ? @uri.path : "/")}
17
- response_code_is_not_valid?(response.code)
18
- end
19
-
20
- def is_an_image?
21
- extension = get_extension(@uri.to_s)
22
- verify_if_extension_is_an_image(extension)
23
- end
24
-
25
- def shorten
26
- shorten_link_with_bitly(@uri.to_s)
27
- end
28
-
29
- private
6
+ module ActsAsLink
7
+ class Link
8
+
9
+ IMAGE_EXTENSIONS = %w(.bmp .gif .ico .jpeg .jpg .png .psd .svg .tif .tiff .xcf)
10
+
11
+ def initialize(url)
12
+ @uri = URI.parse(url)
13
+ raise 'Link is not an url' if (@uri.class != URI::HTTP && @uri.class != URI::HTTPS)
14
+ end
15
+
16
+ def is_broken?
17
+ response = Net::HTTP.start(@uri.host, @uri.port) { |http| response = http.head(@uri.path.size > 0 ? @uri.path : "/")}
18
+ response_code_is_not_valid?(response.code)
19
+ end
20
+
21
+ def is_an_image?
22
+ extension = get_extension(@uri.to_s)
23
+ verify_if_extension_is_an_image(extension)
24
+ end
25
+
26
+ def shorten
27
+ shorten_link_with_bitly(@uri.to_s)
28
+ end
29
+
30
+ private
30
31
 
31
- def response_code_is_not_valid?(code)
32
- if code_is_empty(code) || code_is_an_error_code(code)
33
- true
34
- else
35
- false
32
+ def response_code_is_not_valid?(code)
33
+ if code_is_empty(code) || code_is_an_error_code(code)
34
+ true
35
+ else
36
+ false
37
+ end
38
+ end
39
+
40
+ def code_is_empty(code)
41
+ if code.nil?
42
+ true
43
+ else
44
+ code[0..0] == ""
45
+ end
36
46
  end
37
- end
38
-
39
- def code_is_empty(code)
40
- if code.nil?
41
- true
42
- else
43
- code[0..0] == ""
44
- end
45
- end
46
-
47
- def code_is_an_error_code(code)
48
- code[0..0] == "4" || code[0..0] == "5"
49
- end
50
-
51
- def get_extension(uri)
52
- point_position = uri.rindex('.')
53
- uri_length = uri.length
54
- point_position > (uri_length - 6) ? uri[point_position..uri_length] : nil
55
- end
56
-
57
- def verify_if_extension_is_an_image(extension)
58
- return false if extension.nil?
59
- is_an_image = false
60
- IMAGE_EXTENSIONS.each do |ext|
61
- is_an_image = (extension == ext)
62
- break if is_an_image
47
+
48
+ def code_is_an_error_code(code)
49
+ code[0..0] == "4" || code[0..0] == "5"
63
50
  end
64
- is_an_image
65
- end
66
-
67
- def shorten_link_with_bitly(link)
68
- Bitly.use_api_version_3
69
- bitly = Bitly.new('actsaslink', 'R_fc70af6573e064c0f4081317bb592f18')
70
- bitly.shorten(link).short_url
71
- end
72
51
 
52
+ def get_extension(uri)
53
+ point_position = uri.rindex('.')
54
+ uri_length = uri.length
55
+ point_position > (uri_length - 6) ? uri[point_position..uri_length] : nil
56
+ end
57
+
58
+ def verify_if_extension_is_an_image(extension)
59
+ return false if extension.nil?
60
+ is_an_image = false
61
+ IMAGE_EXTENSIONS.each do |ext|
62
+ is_an_image = (extension == ext)
63
+ break if is_an_image
64
+ end
65
+ is_an_image
66
+ end
67
+
68
+ def shorten_link_with_bitly(link)
69
+ Bitly.use_api_version_3
70
+ bitly = Bitly.new('actsaslink', 'R_fc70af6573e064c0f4081317bb592f18')
71
+ bitly.shorten(link).short_url
72
+ end
73
+
74
+ end
73
75
  end
@@ -1,25 +1,25 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Link do
3
+ describe ActsAsLink::Link do
4
4
 
5
5
  describe 'initialize' do
6
6
 
7
7
  it 'should initialize with uri variable defined' do
8
- link = Link.new('http://google.com')
8
+ link = ActsAsLink::Link.new('http://google.com')
9
9
  link.instance_variable_get('@uri').should_not be_nil
10
10
  end
11
11
 
12
12
  it 'should initialize with uri variable defined when link has https' do
13
- link = Link.new('https://rubygems.org/gems/acts_as_link')
13
+ link = ActsAsLink::Link.new('https://rubygems.org/gems/acts_as_link')
14
14
  link.instance_variable_get('@uri').should_not be_nil
15
15
  end
16
16
 
17
17
  it 'should return an exception when try to initialize with an empty string' do
18
- lambda{Link.new('')}.should raise_error(RuntimeError,/Link is not an url/)
18
+ lambda{ActsAsLink::Link.new('')}.should raise_error(RuntimeError,/Link is not an url/)
19
19
  end
20
20
 
21
21
  it 'should return an exception when try to initialize with a string is not a link' do
22
- lambda{Link.new('test.com.br')}.should raise_error(RuntimeError,/Link is not an url/)
22
+ lambda{ActsAsLink::Link.new('test.com.br')}.should raise_error(RuntimeError,/Link is not an url/)
23
23
  end
24
24
 
25
25
  end
@@ -27,12 +27,12 @@ describe Link do
27
27
  describe 'is_broken?' do
28
28
 
29
29
  it 'should return false when link is live' do
30
- link = Link.new('http://google.com')
30
+ link = ActsAsLink::Link.new('http://google.com')
31
31
  link.is_broken?.should be_false
32
32
  end
33
33
 
34
34
  it 'should return true when link is broken' do
35
- link = Link.new('http://www.vizir.com.br/not_exists_page')
35
+ link = ActsAsLink::Link.new('http://www.vizir.com.br/not_exists_page')
36
36
  link.is_broken?.should be_true
37
37
  end
38
38
 
@@ -41,28 +41,28 @@ describe Link do
41
41
  describe 'is_an_image?' do
42
42
 
43
43
  it 'should return true when url links to an image' do
44
- link = Link.new('http://www.google.com/images/srpr/nav_logo35.png')
44
+ link = ActsAsLink::Link.new('http://www.google.com/images/srpr/nav_logo35.png')
45
45
  link.is_an_image?.should be_true
46
46
  end
47
47
 
48
48
  it 'should return false when url does not link to an image' do
49
- link = Link.new('http://www.google.com')
49
+ link = ActsAsLink::Link.new('http://www.google.com')
50
50
  link.is_an_image?.should be_false
51
51
  end
52
52
 
53
53
  it 'should return false when url links to a pdf file' do
54
- link = Link.new('http://qualidadebr.files.wordpress.com/2009/06/livro-qualidadebr.pdf')
54
+ link = ActsAsLink::Link.new('http://qualidadebr.files.wordpress.com/2009/06/livro-qualidadebr.pdf')
55
55
  link.is_an_image?.should be_false
56
56
  end
57
57
 
58
58
  it 'should return false when url extension image is not in the end' do
59
- link = Link.new('http://www.google.com/images/srpr/nav_.pnglogo35')
59
+ link = ActsAsLink::Link.new('http://www.google.com/images/srpr/nav_.pnglogo35')
60
60
  link.is_an_image?.should be_false
61
61
  end
62
62
 
63
63
  it 'should return true for all image extensions defined' do
64
- Link::IMAGE_EXTENSIONS.each do |ext|
65
- link = Link.new("http://www.google.com/images/srpr/nav_logo35#{ext}")
64
+ ActsAsLink::Link::IMAGE_EXTENSIONS.each do |ext|
65
+ link = ActsAsLink::Link.new("http://www.google.com/images/srpr/nav_logo35#{ext}")
66
66
  link.is_an_image?.should be_true
67
67
  end
68
68
  end
@@ -72,7 +72,7 @@ describe Link do
72
72
  describe 'shorten' do
73
73
 
74
74
  it 'should return shortened link' do
75
- link = Link.new('http://www.google.com')
75
+ link = ActsAsLink::Link.new('http://www.google.com')
76
76
  link.shorten.include?('bit.ly').should be_true
77
77
  end
78
78
 
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: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Fabr\xC3\xADcio Ferrari de Campos"