request_via 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d4966442b32fe09b091c1ba940658f3ae1330210
4
- data.tar.gz: d28c20e0ad6709eaa801f7751a3d2947d3e65c2c
3
+ metadata.gz: f958dbee6653251da7b8fb72818eea82aa23f72f
4
+ data.tar.gz: 5389f3b568ff5252afdafc8c23cde8ddf8005190
5
5
  SHA512:
6
- metadata.gz: 470c31a0b38d42987f41eb9cbeff68f333ccdc875efa77098ec7a7fc3e544baa9476471ab7fc52d5b31070726d8d896438d7fedc865b33a667a3a8285afc058a
7
- data.tar.gz: a78cf82f0077abcbd8d312072c5d0a26c32e90d5ee228aefe25dc216ebf2b4e81d3da3403828a83e6efe67bdb8763d154268fecc9eb5e96ece9eb55793d33b2d
6
+ metadata.gz: dd93888e3fbcfa00674033042de2ae7a9c2a6dff4531d4b7c92df651d08d9b3857de47a100e317bc9cebb2b164d938c7269037a29a8f5002c3dabd11b83dd61b
7
+ data.tar.gz: '039a3b536b0d5ef5ef4d905aa347ceee583e73ef4338b89fc35541ea6c63cab9bfe6bb808ce630ee78355cd572d4c220bf9795a7d798f60f771a2d96a610b096'
data/README.md CHANGED
@@ -18,14 +18,21 @@ response.body
18
18
  # --- Make requests over a map iteration
19
19
 
20
20
  dogs = [ 'akita', 'chihuahua', 'beagle' ]
21
- dogs_images = dogs.map { |breed_name| "https://dog.ceo/api/breed/#{breed_name}/images/random" }
22
- dogs_images.map(&RequestVia::Get).map(&:body)
21
+ dog_images = dogs.map { |breed_name| "https://dog.ceo/api/breed/#{breed_name}/images/random" }
22
+ dog_images.map(&RequestVia::Get).map(&:body)
23
+
24
+ # If do you want to pass common arguments to each request use the GetR function (R = reversed arguments)
25
+ # Available options: port, params, headers, open_timeout read_timeout, response_and_request, net_http
26
+ dog_images.map(&RequestVia::GetR.(open_timeout: 10)).map(&:body)
23
27
 
24
28
  # --- Make requests over an ASYNC map iteration
25
29
 
26
30
  require 'parallel' # https://rubygems.org/gems/parallel
27
31
 
28
32
  Parallel.map(dogs_images, &RequestVia::Get).map(&:body)
33
+
34
+ # Apply common options to each request
35
+ Parallel.map(dogs_images, &RequestVia::GetR.(open_timeout: 10)).map(&:body)
29
36
  ```
30
37
 
31
38
  ## Installation
@@ -68,8 +75,18 @@ response, request = RequestVia::Get.('example.com/foo', response_and_request: tr
68
75
 
69
76
  # Request with the reversed arguments order
70
77
  %w[
71
- example.com/foo example.com/bar
78
+ example.com/foo example.com/bar
72
79
  ].map &RequestVia::GetR.(headers: { 'X-Requested-With': 'RequestVia gem' })
80
+
81
+ # Other options
82
+ RequestVia::Get.('example.io', port: 2000,
83
+ open_timeout: 10, # Default: 60
84
+ read_timeout: 120, # Default: 60
85
+ net_http: -> (host, port) {
86
+ net_http = Net::HTTP.new(host, port)
87
+ # Make your customizations
88
+ net_http
89
+ })
73
90
  ```
74
91
 
75
92
  Supported HTTP methods.
@@ -79,7 +96,7 @@ RequestVia::Post.() # RequestVia::PostR.()
79
96
 
80
97
  RequestVia::Put.() # RequestVia::PutR.()
81
98
 
82
- RequestVia::Delete.() # RequestVia::DeteteR.()
99
+ RequestVia::Delete.() # RequestVia::DeleteR.()
83
100
 
84
101
  RequestVia::Options.() # RequestVia::OptionsR.()
85
102
 
data/lib/request_via.rb CHANGED
@@ -3,26 +3,26 @@
3
3
  require 'net/https'
4
4
  require 'uri'
5
5
 
6
+ require "request_via/func"
6
7
  require "request_via/version"
7
8
  require "request_via/http_client"
8
- require "request_via/func"
9
9
 
10
10
  module RequestVia
11
- Get = Func::FetchStrategyTo.(Net::HTTP::Get).freeze
12
- Head = Func::FetchStrategyTo.(Net::HTTP::Head).freeze
13
- Post = Func::FetchStrategyTo.(Net::HTTP::Post).freeze
14
- Put = Func::FetchStrategyTo.(Net::HTTP::Put).freeze
15
- Delete = Func::FetchStrategyTo.(Net::HTTP::Delete).freeze
16
- Options = Func::FetchStrategyTo.(Net::HTTP::Options).freeze
17
- Trace = Func::FetchStrategyTo.(Net::HTTP::Trace).freeze
18
- Patch = Func::FetchStrategyTo.(Net::HTTP::Patch).freeze
11
+ Get = Func::FetchStrategyTo.(Net::HTTP::Get)
12
+ Head = Func::FetchStrategyTo.(Net::HTTP::Head)
13
+ Post = Func::FetchStrategyTo.(Net::HTTP::Post)
14
+ Put = Func::FetchStrategyTo.(Net::HTTP::Put)
15
+ Delete = Func::FetchStrategyTo.(Net::HTTP::Delete)
16
+ Options = Func::FetchStrategyTo.(Net::HTTP::Options)
17
+ Trace = Func::FetchStrategyTo.(Net::HTTP::Trace)
18
+ Patch = Func::FetchStrategyTo.(Net::HTTP::Patch)
19
19
 
20
- GetR = Func::ReverseRequestArgsTo.(Get).freeze
21
- HeadR = Func::ReverseRequestArgsTo.(Head).freeze
22
- PostR = Func::ReverseRequestArgsTo.(Post).freeze
23
- PutR = Func::ReverseRequestArgsTo.(Put).freeze
24
- DeleteR = Func::ReverseRequestArgsTo.(Delete).freeze
25
- OptionsR = Func::ReverseRequestArgsTo.(Options).freeze
26
- TraceR = Func::ReverseRequestArgsTo.(Trace).freeze
27
- PatchR = Func::ReverseRequestArgsTo.(Patch).freeze
20
+ GetR = Func::ReverseRequestArgsTo.(Get)
21
+ HeadR = Func::ReverseRequestArgsTo.(Head)
22
+ PostR = Func::ReverseRequestArgsTo.(Post)
23
+ PutR = Func::ReverseRequestArgsTo.(Put)
24
+ DeleteR = Func::ReverseRequestArgsTo.(Delete)
25
+ OptionsR = Func::ReverseRequestArgsTo.(Options)
26
+ TraceR = Func::ReverseRequestArgsTo.(Trace)
27
+ PatchR = Func::ReverseRequestArgsTo.(Patch)
28
28
  end
@@ -1,14 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RequestVia
4
+ Freeze = -> object { object.freeze }.freeze
5
+
4
6
  module Func
5
- ReverseRequestArgsTo = -> request {
6
- -> (options, url) {
7
+ ReverseRequestArgsTo = Freeze.(-> request {
8
+ Freeze.(-> (options, url) {
7
9
  request.(url, **options)
8
- }.curry
9
- }.freeze
10
+ }.curry)
11
+ })
10
12
 
11
- ParseURI = -> url {
13
+ ParseURI = Freeze.(-> url {
12
14
  if url.start_with?('http://', 'https://')
13
15
  ::URI.parse(url)
14
16
  elsif /([^:]+)?:?\/\// !~ url
@@ -16,58 +18,69 @@ module RequestVia
16
18
  else
17
19
  fail ::URI::InvalidURIError, 'URI scheme must be http:// or https://'
18
20
  end
19
- }.freeze
21
+ })
20
22
 
21
- IsAHash = -> data { data.is_a?(::Hash) }.freeze
23
+ IsAHash = Freeze.(-> data {
24
+ data.is_a?(::Hash)
25
+ })
22
26
 
23
- SetRequestHeaders = -> (request, headers) {
27
+ SetRequestHeaders = Freeze.(-> (request, headers) {
24
28
  return request unless IsAHash.(headers)
25
29
  headers.each { |key, value| request[key] = value }
26
30
  return request
27
- }.freeze
31
+ })
28
32
 
29
- URIWithoutParams = -> (url, _) { ParseURI.(url) }.freeze
33
+ URIWithoutParams = Freeze.(-> (url, _) {
34
+ ParseURI.(url)
35
+ })
30
36
 
31
- URIWithParams = -> (url, params) {
37
+ URIWithParams = Freeze.(-> (url, params) {
32
38
  ParseURI.(url).tap do |uri|
33
39
  uri.query = ::URI.encode_www_form(params) if IsAHash.(params)
34
40
  end
35
- }.freeze
41
+ })
36
42
 
37
- RequestWithoutBody = -> http_method {
43
+ RequestWithoutBody = Freeze.(-> http_method {
38
44
  -> (uri, _) { http_method.new(uri) }
39
- }.freeze
45
+ })
40
46
 
41
- RequestWithBody = -> http_method {
47
+ RequestWithBody = Freeze.(-> http_method {
42
48
  -> (uri, params) {
43
49
  req = http_method.new(uri)
44
50
  req.set_form_data(params) if IsAHash.(params)
45
51
  return req
46
52
  }
47
- }.freeze
53
+ })
48
54
 
49
- FetchWith = -> (uri_builder, request_builder) {
50
- -> (url, params: nil, headers: nil, response_and_request: false) {
55
+ FetchWith = Freeze.(-> (uri_builder, request_builder) {
56
+ -> (url, port: nil,
57
+ params: nil,
58
+ headers: nil,
59
+ net_http: nil,
60
+ open_timeout: nil,
61
+ read_timeout: nil,
62
+ response_and_request: false) do
51
63
  uri = uri_builder.(url, params)
52
64
  req = SetRequestHeaders.(request_builder.(uri, params), headers)
53
- res = HTTPClient.(uri).request(req)
54
- response_and_request ? [res, req] : res
55
- }
56
- }.freeze
65
+ http = HTTPClient.(uri, port, open_timeout, read_timeout, net_http)
66
+ response = http.request(req)
67
+ response_and_request ? [response, req] : response
68
+ end
69
+ })
57
70
 
58
- FetchWithBodyVia = -> http_method {
71
+ FetchWithBodyVia = Freeze.(-> http_method {
59
72
  FetchWith.(URIWithoutParams, RequestWithBody.(http_method))
60
- }.freeze
73
+ })
61
74
 
62
- FetchWithQueryStringVia = -> http_method {
75
+ FetchWithQueryStringVia = Freeze.(-> http_method {
63
76
  FetchWith.(URIWithParams, RequestWithoutBody.(http_method))
64
- }.freeze
77
+ })
65
78
 
66
- FetchStrategyTo = -> http_method {
79
+ FetchStrategyTo = Freeze.(-> http_method {
67
80
  strategy_to = \
68
81
  http_method::REQUEST_HAS_BODY ? FetchWithBodyVia : FetchWithQueryStringVia
69
82
 
70
- strategy_to.(http_method)
71
- }
83
+ Freeze.(strategy_to.(http_method))
84
+ })
72
85
  end
73
86
  end
@@ -4,8 +4,15 @@ module RequestVia
4
4
  module HTTPClient
5
5
  extend self
6
6
 
7
- def call(uri)
8
- net_http = Net::HTTP.new(uri.host, uri.port)
7
+ Build = Freeze.(-> (host, port) {
8
+ Net::HTTP.new(host, port)
9
+ })
10
+
11
+ def call(uri, port = nil, open_timeout = nil, read_timeout = nil, net_http = nil)
12
+ net_http = build!(uri, port, net_http)
13
+
14
+ net_http.open_timeout = Integer(open_timeout) unless open_timeout.nil?
15
+ net_http.read_timeout = Integer(read_timeout) unless read_timeout.nil?
9
16
 
10
17
  return net_http unless uri.is_a?(URI::HTTPS)
11
18
 
@@ -14,6 +21,16 @@ module RequestVia
14
21
 
15
22
  private
16
23
 
24
+ def build!(uri, port, net_http)
25
+ strategy = net_http.is_a?(Proc) ? net_http : Build
26
+
27
+ http = strategy.(uri.host, (port || uri.port))
28
+
29
+ return http if http.instance_of?(Net::HTTP)
30
+
31
+ fail TypeError, 'net_http proc must return a Net:HTTP instance'
32
+ end
33
+
17
34
  def set_https!(net_http)
18
35
  net_http.verify_mode = OpenSSL::SSL::VERIFY_NONE
19
36
  net_http.use_ssl = true
@@ -3,7 +3,7 @@
3
3
  module RequestVia
4
4
  module SemVer
5
5
  MAJOR = 0
6
- MINOR = 3
6
+ MINOR = 4
7
7
  PATCH = 0
8
8
  end
9
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_via
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura