iri 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -0
  3. data/iri.gemspec +1 -1
  4. data/lib/iri.rb +22 -5
  5. data/test/test_iri.rb +10 -0
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 868c45e578542b837497a14f57e1c158be827ced06a7aad6be1bb8ac21d166f5
4
- data.tar.gz: f6235d5ddca9789a0161cad29d23ff435f05461855a0fa38b0cd81db2c8d0687
3
+ metadata.gz: e104227c43194a812146100020f7eaf541095b684ee10b710f00e0446de088a9
4
+ data.tar.gz: 436424739e4e28f484f8608f913c10d0f589cd75b5788a0f2a3bc3f1cd46c1a5
5
5
  SHA512:
6
- metadata.gz: 2a5ccbeedf58b255986664220256354e71903a65d7fe381bfd4ff097fea88de1ae813fb1693e9115c88358822bf861ff6efe938e58b0b76a60f563abd1ffef86
7
- data.tar.gz: 21df9fcf433747a5fbccd0319fb8974a650d277e7164884f315301dfbe30fd0c1d192fc3ce4614a58f8dfb8678e4e7767819e5cee384d16a30a49113a93821b7
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
 
@@ -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.4.3'
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
- def initialize(uri = '')
52
- @uri = URI(uri)
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
- @uri.clone
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 = @uri.clone
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(@uri.query || '').map { |p, a| [p.to_s, a.clone] }.to_h
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
@@ -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.3
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-08-08 00:00:00.000000000 Z
11
+ date: 2019-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov