better_uri 1.0.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 +7 -0
- data/README.md +88 -0
- data/lib/better_uri/query.rb +53 -0
- data/lib/better_uri/uri.rb +37 -0
- data/lib/better_uri/version.rb +5 -0
- data/lib/better_uri.rb +6 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a849e6946f0715f5e7892fa480d9c5c221fe3b50fe0b39b3dd486344154c8801
|
4
|
+
data.tar.gz: 416b7ca94556d681e140d79d9d5b772df9cf1be89d955b6726262b2c792fb30e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ced0f502e074206efcc64ca625d723d8fe91b57c669ccd9259226ff430d01871ef0a55fccecef9f146492e3385314c0620daac1f793eeb4d94c93bac79d1d58
|
7
|
+
data.tar.gz: e1fb486d9d91d2144c72c9008f90c9055eb2c71f78678b032afc20bdb97c308096736ae7729e73c619cb730b732cbabc9ede3cbfd4a3b7d4c3be1ff84cf3c1ff
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
## Quick start guide
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
gem install better_uri
|
5
|
+
```
|
6
|
+
|
7
|
+
or add it to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'better_uri'
|
11
|
+
```
|
12
|
+
|
13
|
+
Replace ruby core URI with `BetterUri::URI`.
|
14
|
+
|
15
|
+
In plain ruby:
|
16
|
+
```ruby
|
17
|
+
define_method(:URI, ->(string) { BetterUri::URI.new(string) })
|
18
|
+
```
|
19
|
+
|
20
|
+
In rails:
|
21
|
+
```ruby
|
22
|
+
# config/initalizers/uri.rb
|
23
|
+
|
24
|
+
define_method(:URI, ->(string) { BetterUri::URI.new(string) })
|
25
|
+
```
|
26
|
+
|
27
|
+
If you already have code in place that uses ruby core `URI` you can skip the replacement and just use `BetterUri::URI` directly:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
BetterUri::URI.new('https://depay.fi')
|
31
|
+
```
|
32
|
+
|
33
|
+
## Working with URIs
|
34
|
+
|
35
|
+
`BetterUri::URI` has the following capabilities:
|
36
|
+
|
37
|
+
### Queries
|
38
|
+
|
39
|
+
#### Merge hashes into url queries
|
40
|
+
|
41
|
+
`merge(hash)` - returns a copy of the base BetterUri::URI with given hash being merged into the query params:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
|
45
|
+
uri = URI('https://depay.fi/d/abc')
|
46
|
+
uri.to_s # https://depay.fi/d/abc
|
47
|
+
|
48
|
+
uri.query.merge(tracking_id: 123).to_s # https://depay.fi/d/abc?tracking_id=123
|
49
|
+
```
|
50
|
+
|
51
|
+
If you place those urls directly in the view or api response, an explicit to_s is not necessary:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
|
55
|
+
%a{href: URI('https://depay.fi/d/abc').query.merge(tracking_id: 123)} # <a href="https://depay.fi/d/abc?tracking_id=123"/>
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
`merge!(hash)` - the same as `merge` but changes the original URI.
|
60
|
+
|
61
|
+
`merge(hash)` and `merge!(hash)` also support nested query:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
|
65
|
+
uri = URI('https://depay.fi/entries/abc/bookings/new')
|
66
|
+
uri.query.merge(booking: { people: 2 }).to_s # https://depay.fi/entries/abc/bookings/new?booking[people]=2
|
67
|
+
```
|
68
|
+
|
69
|
+
#### Access data in queries
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
|
73
|
+
uri = URI('https://depay.fi?id=1&place[name]=casa')
|
74
|
+
uri.query[:id] # '1'
|
75
|
+
uri.query.dig(:place, :name) # 'casa'
|
76
|
+
```
|
77
|
+
|
78
|
+
#### Remove parts of a query
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
uri = URI('https://depay.fi?one=1&two=2&three=3')
|
82
|
+
uri.query.except(:two, :three).to_s # https://depay.fi?one=1
|
83
|
+
```
|
84
|
+
|
85
|
+
`except(keys)` - Returns a uri which query includes everything except given keys.
|
86
|
+
|
87
|
+
`except!(keys)` - Removes the given keys from the query of the original uri and returns the uri itself.
|
88
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rack/utils'
|
4
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
5
|
+
|
6
|
+
module BetterUri
|
7
|
+
class Query
|
8
|
+
delegate :[], :dig, to: :parsed_query
|
9
|
+
|
10
|
+
def initialize(uri)
|
11
|
+
@uri = uri
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
@uri.query_string
|
16
|
+
end
|
17
|
+
|
18
|
+
def merge(*args, &block)
|
19
|
+
@uri.dup.tap do |uri|
|
20
|
+
uri.query_string = build_query_string(parsed_query.merge(*args, &block))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def merge!(*args, &block)
|
25
|
+
@uri.tap do |uri|
|
26
|
+
uri.query_string = build_query_string(parsed_query.merge(*args, &block))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def except(*args, &block)
|
31
|
+
@uri.dup.tap do |uri|
|
32
|
+
uri.query_string = build_query_string(parsed_query.except(*args, &block))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def except!(*args, &block)
|
37
|
+
@uri.tap do |uri|
|
38
|
+
uri.query_string = build_query_string(parsed_query.except(*args, &block))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def parsed_query
|
45
|
+
Rack::Utils.parse_nested_query(@uri.query_string).with_indifferent_access
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_query_string(query)
|
49
|
+
query_string = Rack::Utils.build_nested_query(query)
|
50
|
+
query_string.empty? ? nil : query_string
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/module/delegation'
|
4
|
+
|
5
|
+
module BetterUri
|
6
|
+
URI_CORE = URI
|
7
|
+
|
8
|
+
class URI < SimpleDelegator
|
9
|
+
attr_reader :uri
|
10
|
+
|
11
|
+
def initialize(string)
|
12
|
+
@uri = super(URI_CORE.parse(string.to_s))
|
13
|
+
end
|
14
|
+
|
15
|
+
def empty?
|
16
|
+
@uri.to_s.empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
def dup
|
20
|
+
self.class.new(@uri.to_s)
|
21
|
+
end
|
22
|
+
|
23
|
+
def query_string
|
24
|
+
@uri.query
|
25
|
+
end
|
26
|
+
|
27
|
+
def query_string=(val)
|
28
|
+
@uri.query = val
|
29
|
+
end
|
30
|
+
alias query= query_string=
|
31
|
+
|
32
|
+
def query
|
33
|
+
BetterUri::Query.new(self)
|
34
|
+
end
|
35
|
+
alias params query
|
36
|
+
end
|
37
|
+
end
|
data/lib/better_uri.rb
ADDED
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: better_uri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- https://github.com/depayfi/better_uri/graphs/contributors
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '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'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Better URI gem to improve Ruby's URI (Uniform Resource Identifiers) core
|
126
|
+
module.
|
127
|
+
email:
|
128
|
+
- engineering@depay.fi
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- README.md
|
134
|
+
- lib/better_uri.rb
|
135
|
+
- lib/better_uri/query.rb
|
136
|
+
- lib/better_uri/uri.rb
|
137
|
+
- lib/better_uri/version.rb
|
138
|
+
homepage: https://github.com/depayfi/better_uri
|
139
|
+
licenses: []
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
142
|
+
rdoc_options: []
|
143
|
+
require_paths:
|
144
|
+
- lib
|
145
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: 3.0.2
|
150
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ">="
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
requirements: []
|
156
|
+
rubygems_version: 3.2.32
|
157
|
+
signing_key:
|
158
|
+
specification_version: 4
|
159
|
+
summary: Better URI gem to improve Ruby's URI (Uniform Resource Identifiers) core
|
160
|
+
module.
|
161
|
+
test_files: []
|