url_slicer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Simon Chiu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = URL Slicer
2
+
3
+ I wanted a simple library to help me parse elements from a URL string, and thus I
4
+ hacked away for a few hours and came up with URL Slicer.
5
+
6
+ Acknowledgements:
7
+ My regex know-how is a bit rusty, and so bits and pieces of the regex code to
8
+ figure out the URL elements from the string are borrowed from various places
9
+ from the internet. Unfortunately I do not remember where I got what code snippets.
10
+ If you find parts of the regex that I've used here was what you have written, please e-mail me and I'll put your name down as a contributor.
11
+
12
+ = Installation
13
+
14
+ gem install url_slicer
15
+
16
+ If you haven't already set up Gemcutter, simply go to http://gemcutter.org to learn
17
+ what you need to do.
18
+
19
+ = Usage
20
+
21
+ require 'url_slicer'
22
+ include UrlSlicer
23
+
24
+ url = UrlSlicer.new("http://www.google.com")
25
+
26
+ url.valid? # true
27
+
28
+ url.host # "www.google.com"
29
+
30
+ url.domain # "google.com"
31
+
32
+ url.port # 80
33
+
34
+ url.ssl? # false
35
+
36
+ url.subdomain # "www"
37
+
38
+ url.protocol # "http"
39
+
40
+ If you've entered an invalid url, and subsequently try to call a method (eg. @instance_object.subdomain), it will raise UrlSlicer::InvalidUrlError, which you can
41
+ resolve in your code.
42
+
43
+ = Source Code
44
+
45
+ The source can be found at http://github.com/tolatomeow/URL-Slicer. Please feel free
46
+ to fork it and add your own features. Pull requests are welcomed.
47
+
48
+ = License
49
+
50
+ MIT license.
51
+
52
+ = Contact
53
+ skhchiu [at] gmail [dot] com
54
+
55
+ == Copyright
56
+
57
+ Copyright (c) 2009 Simon Chiu. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "url_slicer"
8
+ gem.summary = %Q{Slices and dices URL strings into their elements}
9
+ gem.description = %Q{Easy and convenient methods to determine URL elements from a string}
10
+ gem.email = "skhchiu@gmail.com"
11
+ gem.homepage = "http://github.com/tolatomeow/URL-Slicer"
12
+ gem.authors = ["Simon Chiu"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "url_slicer #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/url_slicer.rb ADDED
@@ -0,0 +1,64 @@
1
+ class UrlSlicer
2
+
3
+ class InvalidUrlError < StandardError; end;
4
+
5
+ attr_reader :url
6
+ def initialize(url)
7
+ @url = url
8
+ end
9
+
10
+ def valid?
11
+ regex = /\A(https?|ftp|file):\/\/.+\Z/
12
+ @valid ||= url.scan(regex).length > 0 ? true : false
13
+ end
14
+
15
+ def host
16
+ raise InvalidUrlError unless valid?
17
+ regex = /\A[a-z][a-z0-9+\-.]*:\/\/([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\])/
18
+ @host ||= url.scan(regex).flatten.compact.to_s
19
+ end
20
+
21
+ def domain
22
+ raise InvalidUrlError unless valid?
23
+ regex = /^(?:(?>[a-z0-9-]*\.)+?|)([a-z0-9-]+\.(?>[a-z]*(?>\.[a-z]{2})?))$/i
24
+ @domain ||= host.scan(regex).to_s
25
+ end
26
+
27
+ def subdomain
28
+ raise InvalidUrlError unless valid?
29
+ @subdomain ||= host.gsub(domain, '').gsub('.', '')
30
+ end
31
+
32
+ def port
33
+ raise InvalidUrlError unless valid?
34
+ regex = /^[a-z][a-z0-9+\-.]*:\/\/([a-z0-9\-._~%!$&'()*+,;=]+@)?([a-z0-9\-._~%]+|\[[a-z0-9\-._~%!$&'()*+,;=:]+\]):([0-9]+)/
35
+ @port ||= url.scan(regex).flatten.compact[1].to_i
36
+
37
+ @port == 0 ? default_port : @port
38
+ end
39
+
40
+ def default_port
41
+ case protocol
42
+ when "http"
43
+ 80
44
+ when "https"
45
+ 443
46
+ when "ftp"
47
+ 21
48
+ else
49
+ nil
50
+ end
51
+ end
52
+
53
+ def protocol
54
+ raise InvalidUrlError unless valid?
55
+ regex = /^(https|http|ftp|file)/
56
+ @protocol ||= url.scan(regex).flatten[0]
57
+ end
58
+
59
+ def ssl?
60
+ raise InvalidUrlError unless valid?
61
+ protocol == "https" ? true : false
62
+ end
63
+
64
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'url_slicer'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,139 @@
1
+ require 'helper'
2
+
3
+ class TestUrlSlicer < Test::Unit::TestCase
4
+ context "validating URL" do
5
+ should "be valid if url is http://www.test.com" do
6
+ url = UrlSlicer.new("http://www.test.com")
7
+ assert url.valid?
8
+ end
9
+
10
+ should "be valid if url is ftp://ftp.test.com" do
11
+ url = UrlSlicer.new("ftp://ftp.test.com")
12
+ assert url.valid?
13
+ end
14
+
15
+ should "not be valid if url is www" do
16
+ url = UrlSlicer.new("www")
17
+ assert !url.valid?
18
+ end
19
+
20
+ should "not be valid if url is http://" do
21
+ url = UrlSlicer.new("http://")
22
+ assert !url.valid?
23
+ end
24
+
25
+ should "be valid if url is http://localhost:3000" do
26
+ url = UrlSlicer.new("http://localhost:3000")
27
+ assert url.valid?
28
+ end
29
+ end
30
+
31
+ context "returning host" do
32
+ should "return www.test.com if url is http://www.test.com" do
33
+ url = UrlSlicer.new("http://www.test.com")
34
+ assert_equal "www.test.com", url.host
35
+ end
36
+
37
+ should "return www.test.com if url is http://www.test.com:80" do
38
+ url = UrlSlicer.new("http://www.test.com")
39
+ assert_equal "www.test.com", url.host
40
+ end
41
+
42
+ should "return localhost if url is http://localhost:3000" do
43
+ url = UrlSlicer.new("http://localhost:3000")
44
+ assert_equal "localhost", url.host
45
+ end
46
+
47
+ should "raise error if invalid url http:/www.test.com is entered" do
48
+ url = UrlSlicer.new("http:/www.test.com")
49
+ assert_raise UrlSlicer::InvalidUrlError do
50
+ assert_equal "www.test.com", url.host
51
+ end
52
+ end
53
+ end
54
+
55
+ context "returning tld domain" do
56
+ should "return test.com if url is http://www.test.com" do
57
+ url = UrlSlicer.new("http://www.test.com")
58
+ assert_equal "test.com", url.domain
59
+ end
60
+
61
+ should "return bbc.co.uk if url is http://www.bbc.co.uk" do
62
+ url = UrlSlicer.new("http://www.bbc.co.uk")
63
+ assert_equal "bbc.co.uk", url.domain
64
+ end
65
+
66
+ should "return test.com if url is http://www.test.com:80" do
67
+ url = UrlSlicer.new("http://www.test.com")
68
+ assert_equal "test.com", url.domain
69
+ end
70
+ end
71
+
72
+ context "returning subdomain" do
73
+ should "return www if url is http://www.test.com" do
74
+ url = UrlSlicer.new("http://www.test.com")
75
+ assert_equal "www", url.subdomain
76
+ end
77
+
78
+ should "return blog if url is http://blog.somewhere.co.uk" do
79
+ url = UrlSlicer.new("http://blog.somewhere.co.uk")
80
+ assert_equal "blog", url.subdomain
81
+ end
82
+ end
83
+
84
+ context "returning port" do
85
+ should "return port 80 if url is http://www.test.com:80" do
86
+ url = UrlSlicer.new("http://www.test.com:80")
87
+ assert_equal 80, url.port
88
+ end
89
+
90
+ should "return port 8080 if url is http://www.bbc.co.uk:8080" do
91
+ url = UrlSlicer.new("http://www.bbc.co.uk:8080")
92
+ assert_equal 8080, url.port
93
+ end
94
+
95
+ should "return port 80 if url is http://www.test.com" do
96
+ url = UrlSlicer.new("http://www.test.com")
97
+ assert_equal 80, url.port
98
+ end
99
+
100
+ should "return port 21 if url is ftp://ftp.ucberkley.edu" do
101
+ url = UrlSlicer.new("ftp://ftp.ucberkley.edu")
102
+ assert_equal 21, url.port
103
+ end
104
+
105
+ should "return port 443 if url is https://gmail.com" do
106
+ url = UrlSlicer.new("https://gmail.com")
107
+ assert_equal 443, url.port
108
+ end
109
+ end
110
+
111
+ context "protocol" do
112
+ should "return http if url is http://www.test.com" do
113
+ url = UrlSlicer.new("http://www.test.com")
114
+ assert_equal "http", url.protocol
115
+ end
116
+
117
+ should "return https if url is https://gmail.com" do
118
+ url = UrlSlicer.new("https://gmail.com")
119
+ assert_equal "https", url.protocol
120
+ end
121
+
122
+ should "return ftp if url is ftp://ftp.ucberkley.edu" do
123
+ url = UrlSlicer.new("ftp://ftp.ucberkley.edu")
124
+ assert_equal "ftp", url.protocol
125
+ end
126
+ end
127
+
128
+ context "ssl" do
129
+ should "return true if url is https://www.test.com" do
130
+ url = UrlSlicer.new("https://www.test.com")
131
+ assert url.ssl?
132
+ end
133
+
134
+ should "return false if url is http://www.test.com" do
135
+ url = UrlSlicer.new("http://www.test.com")
136
+ assert !url.ssl?
137
+ end
138
+ end
139
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url_slicer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Chiu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-29 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Easy and convenient methods to determine URL elements from a string
26
+ email: skhchiu@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/url_slicer.rb
42
+ - test/helper.rb
43
+ - test/test_url_slicer.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/tolatomeow/URL-Slicer
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --charset=UTF-8
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.5
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Slices and dices URL strings into their elements
72
+ test_files:
73
+ - test/helper.rb
74
+ - test/test_url_slicer.rb