local_uri 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +76 -0
- data/lib/local_uri/query.rb +35 -0
- data/lib/local_uri/uri.rb +36 -0
- data/lib/local_uri/version.rb +3 -0
- data/lib/local_uri.rb +4 -0
- metadata +161 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: caa9d2f25b700ff2a28a8a6886426c7934492675488b0a9f19c128db8ff8a12b
|
4
|
+
data.tar.gz: c1082b30719ec5ce94ea677de1fbe48374568c8dd0f53dda58907339ae34fd75
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6b54b04bbc94c191371896e496f1ae04cf394b66d1282da0aeed621604b2ceb1785c5e41e1e572be99ee0208221c2ac6957fc60146b519c36afb2ab511287dc
|
7
|
+
data.tar.gz: ab14910f84fd4147c6610adf20ece458848032e0b27e6bf7584bbb0839ee5341d6300860d02369e70300012fecc2aa0b8a5837c0478f732b3dcfb7dd9fc7e573
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
## Quick start guide
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
gem install local_uri
|
5
|
+
```
|
6
|
+
|
7
|
+
or add it to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'local_uri'
|
11
|
+
```
|
12
|
+
|
13
|
+
Replace ruby core URI with `LocalUri::URI`.
|
14
|
+
|
15
|
+
In plain ruby:
|
16
|
+
```ruby
|
17
|
+
define_method(:URI, ->(string) { LocalUri::URI.new(string) })
|
18
|
+
```
|
19
|
+
|
20
|
+
In rails:
|
21
|
+
```ruby
|
22
|
+
# config/initalizers/uri.rb
|
23
|
+
|
24
|
+
define_method(:URI, ->(string) { LocalUri::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 `LocalUri::URI` directly:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
LocalUri::URI.new('https://local.ch')
|
31
|
+
```
|
32
|
+
|
33
|
+
## Working with URIs
|
34
|
+
|
35
|
+
`LocalUri::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 LocalUri::URI with given hash being merged into the query params:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
|
45
|
+
uri = URI('https://yellow.local.ch/d/abc')
|
46
|
+
uri.to_s # https://yellow.local.ch/d/abc
|
47
|
+
|
48
|
+
uri.query.merge(tracking_id: 123).to_s # https://yellow.local.ch/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://yellow.local.ch/d/abc').query.merge(tracking_id: 123)} # <a href="https://yellow.local.ch/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://booking-widget.local.ch/entries/abc/bookings/new')
|
66
|
+
uri.query.merge(booking: { people: 2 }).to_s # https://booking-widget.local.ch/entries/abc/bookings/new?booking[people]=2
|
67
|
+
```
|
68
|
+
|
69
|
+
#### Access data in queries
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
|
73
|
+
uri = URI('https://local.ch?id=1&place[name]=casa')
|
74
|
+
uri[:id] # '1'
|
75
|
+
uri.dig(:place, :name) # 'casa'
|
76
|
+
```
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rack/utils'
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
3
|
+
|
4
|
+
module LocalUri
|
5
|
+
class Query
|
6
|
+
|
7
|
+
delegate :[], :dig, to: :parsed_query
|
8
|
+
|
9
|
+
def initialize(uri)
|
10
|
+
@uri = uri
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
@uri.query_string
|
15
|
+
end
|
16
|
+
|
17
|
+
def merge(*args, &block)
|
18
|
+
@uri.dup.tap do |uri|
|
19
|
+
uri.query_string = Rack::Utils.build_nested_query(parsed_query.merge(*args, &block))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def merge!(*args, &block)
|
24
|
+
@uri.tap do |uri|
|
25
|
+
uri.query_string = Rack::Utils.build_nested_query(parsed_query.merge(*args, &block))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def parsed_query
|
32
|
+
Rack::Utils.parse_nested_query(@uri.query_string).with_indifferent_access
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
2
|
+
|
3
|
+
module LocalUri
|
4
|
+
URI_CORE = URI
|
5
|
+
|
6
|
+
class URI < SimpleDelegator
|
7
|
+
|
8
|
+
attr_reader :uri
|
9
|
+
|
10
|
+
def initialize(string)
|
11
|
+
@uri = super(URI_CORE.parse(string.to_s))
|
12
|
+
end
|
13
|
+
|
14
|
+
def empty?
|
15
|
+
@uri.to_s.empty?
|
16
|
+
end
|
17
|
+
|
18
|
+
def dup
|
19
|
+
self.class.new(@uri.to_s)
|
20
|
+
end
|
21
|
+
|
22
|
+
def query_string
|
23
|
+
@uri.query
|
24
|
+
end
|
25
|
+
|
26
|
+
def query_string=(val)
|
27
|
+
@uri.query = val
|
28
|
+
end
|
29
|
+
alias query= query_string=
|
30
|
+
|
31
|
+
def query
|
32
|
+
LocalUri::Query.new(self)
|
33
|
+
end
|
34
|
+
alias params query
|
35
|
+
end
|
36
|
+
end
|
data/lib/local_uri.rb
ADDED
metadata
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: local_uri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- https://github.com/local-ch/local_uri/graphs/contributors
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-09 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.58.2
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.58.2
|
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: 1.26.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.26.0
|
125
|
+
description: Useful and slight extension to Ruby's URI (Uniform Resource Identifiers)
|
126
|
+
core module.
|
127
|
+
email:
|
128
|
+
- web@localsearch.ch
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- README.md
|
134
|
+
- lib/local_uri.rb
|
135
|
+
- lib/local_uri/query.rb
|
136
|
+
- lib/local_uri/uri.rb
|
137
|
+
- lib/local_uri/version.rb
|
138
|
+
homepage: https://github.com/local-ch/local_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: '0'
|
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.0.3
|
157
|
+
signing_key:
|
158
|
+
specification_version: 4
|
159
|
+
summary: Useful and slight extension to Ruby's URI (Uniform Resource Identifiers)
|
160
|
+
core module.
|
161
|
+
test_files: []
|