iri 0.4.3 → 0.5.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 +4 -4
- data/README.md +2 -0
- data/iri.gemspec +1 -1
- data/lib/iri.rb +22 -5
- data/test/test_iri.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e104227c43194a812146100020f7eaf541095b684ee10b710f00e0446de088a9
|
4
|
+
data.tar.gz: 436424739e4e28f484f8608f913c10d0f589cd75b5788a0f2a3bc3f1cd46c1a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b5cc60c6e2c3741c3f02d29f76ab36998200efb49726eb68221453fd0f4680f0d9a09331d0cf0ef05dc28139fdf17e18cc8d0295bdee243c302696f1ba91652
|
7
|
+
data.tar.gz: bf29b9215bf96c608fccb223003d2f3f4c31f0f2a4d60db3da3e69ceda6fe05d1fc41d0e45afa17316abe80b127161c987157ab2c99a77f9e424685c0f5bce27
|
data/README.md
CHANGED
@@ -29,6 +29,8 @@ url = Iri.new('http://google.com/')
|
|
29
29
|
.scheme('https') # replace 'http' with 'https'
|
30
30
|
.host('localhost') # replace the host name
|
31
31
|
.port('443') # replace the port
|
32
|
+
.path('/new/path') # replace the path of the URI, leaving the query untouched
|
33
|
+
.cut('/q') # replace everything after the host and port
|
32
34
|
.to_s # convert it to a string
|
33
35
|
```
|
34
36
|
|
data/iri.gemspec
CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
s.rubygems_version = '2.5'
|
32
32
|
s.required_ruby_version = '>=2.2'
|
33
33
|
s.name = 'iri'
|
34
|
-
s.version = '0.
|
34
|
+
s.version = '0.5.0'
|
35
35
|
s.license = 'MIT'
|
36
36
|
s.summary = 'Simple Immutable Ruby URI Builder'
|
37
37
|
s.description = 'Class Iri helps you build a URI and then modify its \
|
data/lib/iri.rb
CHANGED
@@ -45,11 +45,19 @@ require 'cgi'
|
|
45
45
|
# Copyright:: Copyright (c) 2019 Yegor Bugayenko
|
46
46
|
# License:: MIT
|
47
47
|
class Iri
|
48
|
+
# When URI is not valid.
|
49
|
+
class InvalidURI < StandardError; end
|
50
|
+
|
48
51
|
# Makes a new object.
|
49
52
|
#
|
50
53
|
# You can even ignore the argument, which will produce an empty URI.
|
51
|
-
|
52
|
-
|
54
|
+
#
|
55
|
+
# By default, this class will never throw any exceptions, even if your URI
|
56
|
+
# is not valid. It will just assume that the URI is"/". However,
|
57
|
+
# you can turn this mode off, by specifying safe as FALSE.
|
58
|
+
def initialize(uri = '', safe: true)
|
59
|
+
@uri = uri
|
60
|
+
@safe = safe
|
53
61
|
end
|
54
62
|
|
55
63
|
# Convert it to a string.
|
@@ -59,7 +67,7 @@ class Iri
|
|
59
67
|
|
60
68
|
# Convert it to an object of class +URI+.
|
61
69
|
def to_uri
|
62
|
-
|
70
|
+
the_uri.clone
|
63
71
|
end
|
64
72
|
|
65
73
|
# Add a few query arguments. For example:
|
@@ -175,15 +183,24 @@ class Iri
|
|
175
183
|
|
176
184
|
private
|
177
185
|
|
186
|
+
def the_uri
|
187
|
+
@the_uri ||= URI(@uri)
|
188
|
+
rescue URI::InvalidURIError => e
|
189
|
+
raise InvalidURI, e.message unless @safe
|
190
|
+
@the_uri = URI('/')
|
191
|
+
end
|
192
|
+
|
178
193
|
def modify
|
179
|
-
c =
|
194
|
+
c = the_uri.clone
|
180
195
|
yield c
|
181
196
|
Iri.new(c)
|
182
197
|
end
|
183
198
|
|
184
199
|
def modify_query
|
185
200
|
modify do |c|
|
186
|
-
params = CGI.parse(
|
201
|
+
params = CGI.parse(the_uri.query || '').map do |p, a|
|
202
|
+
[p.to_s, a.clone]
|
203
|
+
end.to_h
|
187
204
|
yield(params)
|
188
205
|
c.query = URI.encode_www_form(params)
|
189
206
|
end
|
data/test/test_iri.rb
CHANGED
@@ -43,6 +43,16 @@ class IriTest < Minitest::Test
|
|
43
43
|
assert_equal('https://localhost:443/?q=books+about+tennis&limit=10', url)
|
44
44
|
end
|
45
45
|
|
46
|
+
def test_broken_uri
|
47
|
+
assert_raises Iri::InvalidURI do
|
48
|
+
Iri.new('https://example.com/>', safe: false).add(a: 1)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_broken_uri_in_safe_mode
|
53
|
+
Iri.new('https://example.com/>>>').add(a: 1)
|
54
|
+
end
|
55
|
+
|
46
56
|
def test_starts_with_empty_uri
|
47
57
|
assert_equal(
|
48
58
|
'https:',
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iri
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: codecov
|