acts_as_link 0.0.1

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.
@@ -0,0 +1 @@
1
+ require 'acts_as_link/link'
@@ -0,0 +1,38 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ class Link
5
+
6
+ def initialize(url)
7
+ @uri = URI.parse(url)
8
+ raise 'Link is not an url' if @uri.class != URI::HTTP
9
+ end
10
+
11
+ def is_broken?
12
+ response = Net::HTTP.start(@uri.host, @uri.port) { |http| response = http.head(@uri.path.size > 0 ? @uri.path : "/")}
13
+ response_code_is_not_valid?(response.code)
14
+ end
15
+
16
+ private
17
+
18
+ def response_code_is_not_valid?(code)
19
+ if code_is_empty(code) || code_is_an_error_code(code)
20
+ true
21
+ else
22
+ false
23
+ end
24
+ end
25
+
26
+ def code_is_empty(code)
27
+ if code.nil?
28
+ true
29
+ else
30
+ code[0..0] == ""
31
+ end
32
+ end
33
+
34
+ def code_is_an_error_code(code)
35
+ code[0..0] == "4" || code[0..0] == "5"
36
+ end
37
+
38
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Link do
4
+
5
+ describe 'initialize' do
6
+
7
+ it 'should initialize with uri variable defined' do
8
+ link = Link.new('http://google.com')
9
+ link.instance_variable_get('@uri').should_not be_nil
10
+ end
11
+
12
+ it 'should return an exception when try to initialize with an empty string' do
13
+ lambda{Link.new('')}.should raise_error(RuntimeError,/Link is not an url/)
14
+ end
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/)
18
+ end
19
+
20
+ end
21
+
22
+ describe 'is_broken?' do
23
+
24
+ it 'should return false when link is live' do
25
+ link = Link.new('http://google.com')
26
+ link.is_broken?.should be_false
27
+ end
28
+
29
+ it 'should return true when link is broken' do
30
+ link = Link.new('http://www.vizir.com.br/not_exists_page')
31
+ link.is_broken?.should be_true
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1 @@
1
+ Dir["lib/acts_as_link/*.rb"].each {|file| require file }
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_link
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - "Fabr\xC3\xADcio Ferrari de Campos"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-12 00:00:00 -02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 1
32
+ - 3
33
+ - 0
34
+ version: 1.3.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: For now only verification if link is broken
38
+ email: fabricio@vizir.com.br
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - lib/acts_as_link.rb
47
+ - lib/acts_as_link/link.rb
48
+ - spec/spec_helper.rb
49
+ - spec/acts_as_link/link_spec.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/fabricioffc/acts_as_link
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 19
74
+ segments:
75
+ - 1
76
+ - 3
77
+ - 4
78
+ version: 1.3.4
79
+ requirements: []
80
+
81
+ rubyforge_project: acts_as_link
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: An easy way to make your link acts as link
86
+ test_files: []
87
+