request_via 0.4.0 → 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 +29 -0
- data/lib/request_via.rb +2 -1
- data/lib/request_via/client.rb +79 -0
- data/lib/request_via/func.rb +11 -3
- data/lib/request_via/{http_client.rb → net_http.rb} +9 -3
- data/lib/request_via/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 789cb78c79d76ab40e032e9c944a68d38c2f4043
|
4
|
+
data.tar.gz: fe00331075753cdec333d121fd5cc61a6d98609c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0bc000d11dfd22a76cbd847a5dfac461d8a7a8a5c8e1b9a40dde72648b14899528bd47f09033b0668ca89e450a0d15e0118a2e286bc9535e281d7928725965f9
|
7
|
+
data.tar.gz: 491167b516738b15260eb85814692b842dd3c388289243d838b52f0bf57668bc1649e42e7117b64d7c6585f02189d534080cabb0808a0627fd131bfb22e2f78b
|
data/README.md
CHANGED
@@ -92,6 +92,8 @@ RequestVia::Get.('example.io', port: 2000,
|
|
92
92
|
Supported HTTP methods.
|
93
93
|
(**NOTE:** you can use all arguments of previous examples)
|
94
94
|
```ruby
|
95
|
+
RequestVia::Head.() # RequestVia::HeadR.()
|
96
|
+
|
95
97
|
RequestVia::Post.() # RequestVia::PostR.()
|
96
98
|
|
97
99
|
RequestVia::Put.() # RequestVia::PutR.()
|
@@ -110,6 +112,33 @@ Making a HTTPS request.
|
|
110
112
|
RequestVia::Get.('https://example.com')
|
111
113
|
```
|
112
114
|
|
115
|
+
Create a HTTP(S) client for REST resources.
|
116
|
+
```ruby
|
117
|
+
client = RequestVia::Client.('https://example.com')
|
118
|
+
|
119
|
+
client.get # same of client.get('/')
|
120
|
+
|
121
|
+
# Supported arguments: params:, headers:
|
122
|
+
client.get('foo', params: { a: 1 })
|
123
|
+
|
124
|
+
client.post('/bar', headers: { 'User-Agent' => 'REST Example' })
|
125
|
+
|
126
|
+
# Supported options
|
127
|
+
RequestVia::Client.('example.com/foo/bar', port: 3000,
|
128
|
+
open_timeout: 10,
|
129
|
+
read_timeout: 100)
|
130
|
+
|
131
|
+
# Supported HTTP methods:
|
132
|
+
# client.get
|
133
|
+
# client.head
|
134
|
+
# client.post
|
135
|
+
# client.put
|
136
|
+
# client.delete
|
137
|
+
# client.options
|
138
|
+
# client.trace
|
139
|
+
# client.patch
|
140
|
+
```
|
141
|
+
|
113
142
|
## Development
|
114
143
|
|
115
144
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/request_via.rb
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RequestVia
|
4
|
+
|
5
|
+
class Client
|
6
|
+
attr_reader :address, :net_http
|
7
|
+
|
8
|
+
BuildURL = Freeze.(-> (address, path) {
|
9
|
+
"#{address}#{path.start_with?('/') ? path : "/#{path}"}"
|
10
|
+
}.curry)
|
11
|
+
|
12
|
+
BuildAddress = -> (uri, address) {
|
13
|
+
has_port = uri.port && address =~ /:\d+/
|
14
|
+
Freeze.(%{#{uri.scheme}://#{uri.host}#{":#{uri.port}" if has_port}}.chomp('/'))
|
15
|
+
}
|
16
|
+
|
17
|
+
OptionsBuilder = Freeze.(-> net_http {
|
18
|
+
-> (options) {
|
19
|
+
{
|
20
|
+
params: options[:params],
|
21
|
+
headers: options[:headers],
|
22
|
+
net_http: net_http
|
23
|
+
}
|
24
|
+
}
|
25
|
+
})
|
26
|
+
|
27
|
+
def self.call(address, port: nil, open_timeout: nil, read_timeout: nil)
|
28
|
+
self.new(address, port, open_timeout, read_timeout)
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(address, port, open_timeout, read_timeout)
|
32
|
+
uri = Freeze.(Func::ParseURI.(address))
|
33
|
+
|
34
|
+
@address = BuildAddress.(uri, address)
|
35
|
+
@build_url = BuildURL.(@address)
|
36
|
+
@net_http = NetHTTP.(uri, port, open_timeout, read_timeout)
|
37
|
+
@options = OptionsBuilder.(@net_http)
|
38
|
+
end
|
39
|
+
|
40
|
+
def get(path = '/', **options)
|
41
|
+
fetch(RequestVia::Get, path, options)
|
42
|
+
end
|
43
|
+
|
44
|
+
def head(path = '/', **options)
|
45
|
+
fetch(RequestVia::Head, path, options)
|
46
|
+
end
|
47
|
+
|
48
|
+
def post(path = '/', **options)
|
49
|
+
fetch(RequestVia::Post, path, options)
|
50
|
+
end
|
51
|
+
|
52
|
+
def put(path = '/', **options)
|
53
|
+
fetch(RequestVia::Put, path, options)
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete(path = '/', **options)
|
57
|
+
fetch(RequestVia::Delete, path, options)
|
58
|
+
end
|
59
|
+
|
60
|
+
def options(path = '/', **options)
|
61
|
+
fetch(RequestVia::Options, path, options)
|
62
|
+
end
|
63
|
+
|
64
|
+
def trace(path = '/', **options)
|
65
|
+
fetch(RequestVia::Trace, path, options)
|
66
|
+
end
|
67
|
+
|
68
|
+
def patch(path = '/', **options)
|
69
|
+
fetch(RequestVia::Patch, path, options)
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def fetch(http_method, path, keyword_args)
|
75
|
+
http_method.(@build_url.(path), **@options.(keyword_args))
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
data/lib/request_via/func.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module RequestVia
|
4
|
+
DEFAULT_HEADERS = {
|
5
|
+
'User-Agent' => "RequestVia v#{RequestVia::VERSION}"
|
6
|
+
}.freeze
|
7
|
+
|
4
8
|
Freeze = -> object { object.freeze }.freeze
|
5
9
|
|
6
10
|
module Func
|
@@ -25,8 +29,12 @@ module RequestVia
|
|
25
29
|
})
|
26
30
|
|
27
31
|
SetRequestHeaders = Freeze.(-> (request, headers) {
|
28
|
-
|
29
|
-
|
32
|
+
request_headers = IsAHash.(headers) ? headers : {}
|
33
|
+
|
34
|
+
DEFAULT_HEADERS.merge(request_headers).each do |key, value|
|
35
|
+
request[key] = value
|
36
|
+
end
|
37
|
+
|
30
38
|
return request
|
31
39
|
})
|
32
40
|
|
@@ -62,7 +70,7 @@ module RequestVia
|
|
62
70
|
response_and_request: false) do
|
63
71
|
uri = uri_builder.(url, params)
|
64
72
|
req = SetRequestHeaders.(request_builder.(uri, params), headers)
|
65
|
-
http =
|
73
|
+
http = NetHTTP.(uri, port, open_timeout, read_timeout, net_http)
|
66
74
|
response = http.request(req)
|
67
75
|
response_and_request ? [response, req] : response
|
68
76
|
end
|
@@ -1,20 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module RequestVia
|
4
|
-
module
|
4
|
+
module NetHTTP
|
5
5
|
extend self
|
6
6
|
|
7
7
|
Build = Freeze.(-> (host, port) {
|
8
8
|
Net::HTTP.new(host, port)
|
9
9
|
})
|
10
10
|
|
11
|
+
IsNetHTTP = Freeze.(-> object {
|
12
|
+
object.instance_of?(Net::HTTP)
|
13
|
+
})
|
14
|
+
|
11
15
|
def call(uri, port = nil, open_timeout = nil, read_timeout = nil, net_http = nil)
|
16
|
+
return net_http if IsNetHTTP.(net_http)
|
17
|
+
|
12
18
|
net_http = build!(uri, port, net_http)
|
13
19
|
|
14
20
|
net_http.open_timeout = Integer(open_timeout) unless open_timeout.nil?
|
15
21
|
net_http.read_timeout = Integer(read_timeout) unless read_timeout.nil?
|
16
22
|
|
17
|
-
return net_http unless uri.
|
23
|
+
return net_http unless uri.instance_of?(URI::HTTPS)
|
18
24
|
|
19
25
|
set_https!(net_http)
|
20
26
|
end
|
@@ -26,7 +32,7 @@ module RequestVia
|
|
26
32
|
|
27
33
|
http = strategy.(uri.host, (port || uri.port))
|
28
34
|
|
29
|
-
return http if
|
35
|
+
return http if IsNetHTTP.(http)
|
30
36
|
|
31
37
|
fail TypeError, 'net_http proc must return a Net:HTTP instance'
|
32
38
|
end
|
data/lib/request_via/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: request_via
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,8 +84,9 @@ files:
|
|
84
84
|
- bin/console
|
85
85
|
- bin/setup
|
86
86
|
- lib/request_via.rb
|
87
|
+
- lib/request_via/client.rb
|
87
88
|
- lib/request_via/func.rb
|
88
|
-
- lib/request_via/
|
89
|
+
- lib/request_via/net_http.rb
|
89
90
|
- lib/request_via/version.rb
|
90
91
|
- request_via.gemspec
|
91
92
|
homepage: https://github.com/serradura/request_via
|