request_via 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f958dbee6653251da7b8fb72818eea82aa23f72f
4
- data.tar.gz: 5389f3b568ff5252afdafc8c23cde8ddf8005190
3
+ metadata.gz: 789cb78c79d76ab40e032e9c944a68d38c2f4043
4
+ data.tar.gz: fe00331075753cdec333d121fd5cc61a6d98609c
5
5
  SHA512:
6
- metadata.gz: dd93888e3fbcfa00674033042de2ae7a9c2a6dff4531d4b7c92df651d08d9b3857de47a100e317bc9cebb2b164d938c7269037a29a8f5002c3dabd11b83dd61b
7
- data.tar.gz: '039a3b536b0d5ef5ef4d905aa347ceee583e73ef4338b89fc35541ea6c63cab9bfe6bb808ce630ee78355cd572d4c220bf9795a7d798f60f771a2d96a610b096'
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
@@ -5,7 +5,8 @@ require 'uri'
5
5
 
6
6
  require "request_via/func"
7
7
  require "request_via/version"
8
- require "request_via/http_client"
8
+ require "request_via/net_http"
9
+ require "request_via/client"
9
10
 
10
11
  module RequestVia
11
12
  Get = Func::FetchStrategyTo.(Net::HTTP::Get)
@@ -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
@@ -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
- return request unless IsAHash.(headers)
29
- headers.each { |key, value| request[key] = value }
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 = HTTPClient.(uri, port, open_timeout, read_timeout, net_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 HTTPClient
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.is_a?(URI::HTTPS)
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 http.instance_of?(Net::HTTP)
35
+ return http if IsNetHTTP.(http)
30
36
 
31
37
  fail TypeError, 'net_http proc must return a Net:HTTP instance'
32
38
  end
@@ -3,7 +3,7 @@
3
3
  module RequestVia
4
4
  module SemVer
5
5
  MAJOR = 0
6
- MINOR = 4
6
+ MINOR = 5
7
7
  PATCH = 0
8
8
  end
9
9
 
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.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-10-31 00:00:00.000000000 Z
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/http_client.rb
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