http_url 0.9.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/http_url.rb +82 -0
  3. metadata +58 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c4027f6d2367720ae30302f239ee3cd2040c82f
4
+ data.tar.gz: 46e6b500cd3f2f67e5b18f56266a1a6f1650d2e2
5
+ SHA512:
6
+ metadata.gz: 2bf92a71f5abc786fa489d9561f81da2821b7e0bacda2ab59eace7c0a369af194bba69754563147d2b61d1c16979ef50a9274f5db03237d6c9e26bef33639271
7
+ data.tar.gz: 9de84169de34b847dd92cd5bd7201c763e2ba70b24b64cf48f37f04f66bf5b3e7f0c28c624b66e18f40001f326d7d9352dd910b4e196a74e2eb557cf9d18074a
@@ -0,0 +1,82 @@
1
+ require 'uri'
2
+
3
+ class HttpUrl
4
+
5
+ HTTP = 'http'
6
+ HTTPS = 'https'
7
+
8
+ def initialize(url)
9
+ url = normalize(url)
10
+ if url =~ /^#{URI::regexp}$/
11
+ begin
12
+ @uri = URI.parse(url)
13
+ rescue
14
+ end
15
+ end
16
+ @uri ||= nil
17
+ end
18
+
19
+ def valid?
20
+ @uri && !@uri.to_s.include?(' ') && @uri.host.include?('.')
21
+ end
22
+
23
+ def http?
24
+ valid? && @uri.scheme && @uri.scheme.downcase == HTTP
25
+ end
26
+
27
+ def https?
28
+ valid? && @uri.scheme && @uri.scheme.downcase == HTTPS
29
+ end
30
+
31
+ def scheme
32
+ @uri.scheme if valid?
33
+ end
34
+
35
+ def host
36
+ if valid? && @uri.host.start_with?('www.')
37
+ return @uri.host[4..-1]
38
+ end
39
+ @uri.host if valid?
40
+ end
41
+
42
+ def port
43
+ if valid? && @uri.port.nil?
44
+ return 80
45
+ end
46
+ @uri.port.to_i if valid?
47
+ end
48
+
49
+ def path
50
+ @uri.path if valid?
51
+ end
52
+
53
+ def query
54
+ @uri.query if valid?
55
+ end
56
+
57
+ def fragment
58
+ @uri.fragment if valid?
59
+ end
60
+
61
+ def to_s
62
+ @uri.to_s if valid?
63
+ end
64
+
65
+ private
66
+
67
+ def normalize(url)
68
+ unless url.empty?
69
+ url = encode_utf8(url)
70
+ if !url.start_with?(HTTP) && !url.start_with?(HTTPS)
71
+ url = "http://#{url}"
72
+ end
73
+ end
74
+ url.downcase
75
+ end
76
+
77
+ def encode_utf8(url)
78
+ url = url.dup.force_encoding('UTF-8')
79
+ url.encode!('UTF-8', :invalid => :replace, :undef => :replace, :replace => "")
80
+ end
81
+
82
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Marc Marcet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: A simple and lightweight library to work with http urls.
28
+ email: marcmr87@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/http_url.rb
34
+ homepage: https://github.com/marcmr87/http_url
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.9.3
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.2.2
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: A simple and lightweight library to work with http urls.
58
+ test_files: []