linkhum-url 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2499da4f4ea690201eb85d3f2e8942b90655629e
4
+ data.tar.gz: c4738dc13d7dab8dba1008b89cce1c09d360fe13
5
+ SHA512:
6
+ metadata.gz: c7dc9f9ec8fa62d742ff51a3cdeac046353f83833341f57c76d57b0e726dd54719bb22c70bf969f4109d3fdb08660fb376d7155c0b7e02ef4f24e3c55dc08bb2
7
+ data.tar.gz: 1359bf86a29b802371fa7411068adda60fb7db20fd7793e533c27620867aac71de6cdc4450f17a68ca8046b56a10ed58fb80585bf235d4b16addac4b1e37ed88
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Alexey Makhotkin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # linkhum-url
2
+
3
+ URLs which users provide could be both in human-readable form and in
4
+ URL-encoded form. `linkhum-url` gem parses URLs in any representation
5
+ and returns both representations: human-readable and URL-encoded.
6
+
7
+ `linkhum-url` handles IDNs and Punycode in domain names. Behind the
8
+ scenes `addressable` gem is used to handle actual URL parsing.
9
+
10
+ This is a sister gem to
11
+ [`linkhum`](https://github.com/zverok/linkhum).
12
+
13
+ ## Usage
14
+
15
+ `linkhum-url` has only one method: `Linkhum::URL.parse(url)`. It
16
+ accepts URL in any representation (usually provided by user) and
17
+ returns `Hash` with two keys: `:human_readable` and `:url_encoded`.
18
+ Human-readable representation should be displayed to the user, while
19
+ URL-encoded representation should be used for HTTP queries, HTML
20
+ embedding etc.
21
+
22
+ $ irb
23
+ 2.3.0 :001 > require 'linkhum-url'
24
+ => true
25
+ 2.3.0 :002 > Linkhum::URL.parse("https://example.org")
26
+ => {:human_readable=>"https://example.org", :url_encoded=>"https://example.org"}
27
+ 2.3.0 :003 > Linkhum::URL.parse("https://пивбар.рф/поиск.html?q=пивбар")
28
+ => {:human_readable=>"https://пивбар.рф/поиск.html?q=пивбар", :url_encoded=>"https://xn--80abcx8ak.xn--p1ai/%D0%BF%D0%BE%D0%B8%D1%81%D0%BA.html?q=%D0%BF%D0%B8%D0%B2%D0%B1%D0%B0%D1%80"}
29
+ 2.3.0 :004 > Linkhum::URL.parse("https://xn--80abcx8ak.xn--p1ai/%D0%BF%D0%BE%D0%B8%D1%81%D0%BA.html?q=%D0%BF%D0%B8%D0%B2%D0%B1%D0%B0%D1%80")
30
+ => {:human_readable=>"https://пивбар.рф/поиск.html?q=пивбар", :url_encoded=>"https://xn--80abcx8ak.xn--p1ai/%D0%BF%D0%BE%D0%B8%D1%81%D0%BA.html?q=%D0%BF%D0%B8%D0%B2%D0%B1%D0%B0%D1%80"}
31
+
32
+ ## Install
33
+
34
+ $ sudo apt-get install idn # Debian/Ubuntu
35
+ $ brew install libidn # OS X
36
+
37
+ $ gem install linkhum-url
38
+
39
+ ## License
40
+
41
+ `linkhum-url` released under MIT License. Copyright (C) Alexey
42
+ Makhotkin, 2016.
@@ -0,0 +1,57 @@
1
+ module Linkhum
2
+ class URL
3
+ require 'addressable'
4
+ require 'idn'
5
+
6
+ include IDN
7
+
8
+ def self.parse(url)
9
+ au = Addressable::URI.parse(url)
10
+ human_readable = { scheme: au.scheme, userinfo: au.userinfo }
11
+ url_encoded = { scheme: au.scheme, userinfo: au.userinfo }
12
+
13
+ if au.host =~ /\Axn--/
14
+ human_readable[:host] = Idna.toUnicode(au.host)
15
+ else
16
+ human_readable[:host] = au.host
17
+ end
18
+ url_encoded[:host] = au.normalized_host
19
+
20
+ human_readable[:path] = Addressable::URI.unencode_component(au.path)
21
+ # this code handles bug in Addressable::URI (up to 2.5.0), which
22
+ # converts paths to Unicode NFKC (it should only do that for
23
+ # hostnames). Patch to Addressable::URI pending.
24
+ au_path = au.path.dup
25
+ if au_path =~ /\A[\x00-\x7F]*\z/
26
+ au_path = Addressable::URI.unencode_component(au_path)
27
+ end
28
+ au_path.force_encoding(Encoding::ASCII_8BIT)
29
+ url_encoded[:path] = Addressable::URI.encode_component(au_path)
30
+
31
+ human_readable[:query] = Addressable::URI.unencode_component(au.query)
32
+ if au.query
33
+ # see above
34
+ au_query = au.query.dup
35
+ if au_query =~ /\A[\x00-\x7F]*\z/
36
+ au_query = Addressable::URI.unencode_component(au_query)
37
+ end
38
+ au_query.force_encoding(Encoding::ASCII_8BIT)
39
+ end
40
+ url_encoded[:query] = Addressable::URI.encode_component(au_query)
41
+
42
+ # fragments do not need to be encoded
43
+ human_readable[:fragment] = au.fragment
44
+ url_encoded[:fragment] = au.fragment
45
+
46
+ { human_readable: generate_url(human_readable),
47
+ url_encoded: generate_url(url_encoded) }
48
+ end
49
+
50
+ private
51
+ def self.generate_url(parts)
52
+ query_part = parts[:query] ? "?#{parts[:query]}" : ""
53
+ fragment_part = parts[:fragment] ? "##{parts[:fragment]}" : ""
54
+ "#{parts[:scheme]}://#{parts[:userinfo]}#{parts[:host]}#{parts[:path]}#{query_part}#{fragment_part}"
55
+ end
56
+ end
57
+ end
@@ -0,0 +1 @@
1
+ require 'linkhum/url'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linkhum-url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexey Makhotkin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: addressable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: idn-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: 'Input URL could be both human-readable and URL-encoded. Two URLs are
56
+ returned as result: human-readable and URL-encoded'
57
+ email: squadette@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - README.md
64
+ - lib/linkhum-url.rb
65
+ - lib/linkhum/url.rb
66
+ homepage: http://rubygems.org/gems/linkhum-url
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.5.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Linkhum-URL creates both URL-encoded and readable versions of URLs
90
+ test_files: []