aga-request 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: a00fbef7308e4a5cd61fd3227a06467cbbe82a01f48c69fedc24f9e846490642
4
- data.tar.gz: e8a7ef87bb251566b9eb87b483211265e90cfeacae28a8d829f9e327e31ddae9
3
+ metadata.gz: 74e39b07b921c9d9fc6e01d788e50ae4f87a3bb8aa0718917548979323fc8f78
4
+ data.tar.gz: 931a6f96c032b1311a7ec368a6fb9177c1394b8cee93451132aa27cf77c24956
5
5
  SHA512:
6
- metadata.gz: b809408a7a31881aaaa11315498b39312d98d23fbeed05f5b62ea747604913c549dedac85667c26938f48d2f0d2b55ab02c941ce282a430335f974d620a1e93d
7
- data.tar.gz: 211dc87b4885f67207fd37299c7f7f9d79f236ffe0fec630bbc08c0007d87f994ccaef7b4b6e44322426781a54f400c241007b866196cac9c2e711c071a67125
6
+ metadata.gz: dcf3c02680f2ef14aaa8ca53e82cf162f823ae050d6c159ab54ac2f410545ce6c38a2de1871e9053ac6b0a6a023c07c201a6206bc6b1ff9bb94ed68bd36c28a8
7
+ data.tar.gz: 65190939f949f56bfeb8e441fd34e9d30a8e165199d7e3014efb09f8451fb3d251e7434dcf208cd88928f0f8b3480d72ba1558f98d86d0ebbc4d3860631689b8
@@ -9,54 +9,47 @@ module Request
9
9
  @cache = ActiveSupport::Cache.lookup_store(:memory_store, cache_options || default_cache)
10
10
  end
11
11
 
12
- def get(endpoint, params = {}, headers = {})
13
- response = cached_request(:get, endpoint, params, headers)
14
- JSON.parse(response.body)
12
+ %i[get post put delete].each do |method|
13
+ define_method(method) do |endpoint, params = {}, headers = {}|
14
+ cached_request(method, endpoint, params, headers)
15
+ end
15
16
  end
16
17
 
17
- def post(endpoint, body = {}, headers = {})
18
- response = cached_request(:post, endpoint, body, headers)
19
- JSON.parse(response.body)
20
- end
18
+ private
21
19
 
22
- def put(endpoint, body = {}, headers = {})
23
- response = cached_request(:put, endpoint, body, headers)
24
- JSON.parse(response.body)
25
- end
20
+ def cached_request(method, endpoint, params = {}, headers = {})
21
+ cache_key = [method, endpoint, params, headers]
22
+ unless @cache.exist?(cache_key)
23
+ response = make_request(method, endpoint, params, headers)
24
+ @cache.write(cache_key, data(response))
25
+ end
26
26
 
27
- def delete(endpoint, params = {}, headers = {})
28
- response = cached_request(:delete, endpoint, params, headers)
29
- JSON.parse(response.body)
27
+ @cache.read(cache_key)
30
28
  end
31
29
 
32
- private
30
+ def data(response)
31
+ { response: JSON.parse(response.body), status: response.success? }
32
+ end
33
33
 
34
- def cached_request(method, endpoint, payload = {}, headers = {})
35
- cache_key = [method, endpoint, payload, headers]
36
- if @cache.exist?(cache_key)
37
- @cache.read(cache_key)
38
- else
39
- response = make_request(method, endpoint, payload, headers)
40
- @cache.write(cache_key, response)
41
- response
42
- end
34
+ def default_cache
35
+ { size: 64.megabytes, expires_in: 1.minute }
43
36
  end
44
37
 
45
- def make_request(method, endpoint, payload, headers)
46
- connection = Faraday.new(endpoint) do |conn|
47
- conn.request :url_encoded
48
- conn.adapter Faraday.default_adapter
49
- end
38
+ def make_request(method, endpoint, params, headers)
39
+ connection = prepare_connection(method, endpoint, params, headers)
50
40
 
51
41
  connection.public_send(method) do |request|
52
42
  request.headers.merge!(headers)
53
43
  request.url(endpoint)
54
- request.body = payload.to_json if %i[post put].include?(method)
44
+ request.body = params.to_json if %i[post put].include?(method)
55
45
  end
56
46
  end
57
47
 
58
- def default_cache
59
- { size: 64.megabytes, expires_in: 1.minutes }
48
+ def prepare_connection(_method, endpoint, _params, _headers)
49
+ Faraday.new(endpoint) do |conn|
50
+ conn.request :url_encoded
51
+ conn.adapter Faraday.default_adapter
52
+ end
60
53
  end
61
54
  end
62
55
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Request
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aga-request
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Baldazzi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-21 00:00:00.000000000 Z
11
+ date: 2023-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -200,5 +200,5 @@ requirements: []
200
200
  rubygems_version: 3.3.24
201
201
  signing_key:
202
202
  specification_version: 4
203
- summary: request0.0.1
203
+ summary: request0.0.2
204
204
  test_files: []