aga-request 0.0.0 → 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: adb2674873098f2a5a97cf07b8b09306fe7dd233d62cff2b89230787adeaab5d
4
- data.tar.gz: aef7f2ed2e5b23222f8c575bcbe008df8380fff1909f8fc9d7998ee5518a7288
3
+ metadata.gz: 74e39b07b921c9d9fc6e01d788e50ae4f87a3bb8aa0718917548979323fc8f78
4
+ data.tar.gz: 931a6f96c032b1311a7ec368a6fb9177c1394b8cee93451132aa27cf77c24956
5
5
  SHA512:
6
- metadata.gz: ad548f7cf4ebd4f5ee27a8468ba0cf5aa1fc726091b9a40c26e0148375de81c6c98f30761a2628b0bc2be5d8c8c3fd571dee89395418d21e6996bf44ea86f53d
7
- data.tar.gz: '0096873bf9e65a97e33e5e18317fc832e5c57f9635008b8e5e92a033aac4c97a37a7a19e90e6ffbcac94263370c1b48d9c260e7e1550dfd0451e9b4b976d892c'
6
+ metadata.gz: dcf3c02680f2ef14aaa8ca53e82cf162f823ae050d6c159ab54ac2f410545ce6c38a2de1871e9053ac6b0a6a023c07c201a6206bc6b1ff9bb94ed68bd36c28a8
7
+ data.tar.gz: 65190939f949f56bfeb8e441fd34e9d30a8e165199d7e3014efb09f8451fb3d251e7434dcf208cd88928f0f8b3480d72ba1558f98d86d0ebbc4d3860631689b8
data/lib/request/base.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'uri'
4
- require 'net/http'
5
- require 'json'
3
+ require 'faraday'
4
+ require 'active_support'
5
+ require 'active_support/cache'
6
6
 
7
7
  module Request
8
8
  class Base
9
9
  class << self
10
10
  attr_accessor :app_info
11
+
12
+ def initialize; end
11
13
  end
12
14
 
13
15
  self.app_info = ::ENV['APP_INFO'] || "#{::Request::NAME} V#{::Request.version}"
@@ -2,36 +2,53 @@
2
2
 
3
3
  module Request
4
4
  class Http < Base
5
- class << self
6
- def request(uri:, method:, signature: nil, params: {})
7
- req = get_method(name: method, uri: uri)
8
- req['Authorization'] = signature unless signature.nil?
9
- req['Content-Type'] = 'application/json'
5
+ def initialize(api_key: nil, api_secret: nil, cache_options: {})
6
+ super()
7
+ @api_key = api_key
8
+ @api_secret = api_secret
9
+ @cache = ActiveSupport::Cache.lookup_store(:memory_store, cache_options || default_cache)
10
+ end
10
11
 
11
- req.body = JSON.dump(except_params(params)) unless params.empty?
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
16
+ end
12
17
 
13
- res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
14
- http.request(req)
15
- end
18
+ private
16
19
 
17
- JSON.parse(res.body)
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))
18
25
  end
19
26
 
20
- private
27
+ @cache.read(cache_key)
28
+ end
21
29
 
22
- def get_method(name:, uri:)
23
- case name
24
- when 'GET', 'get'
25
- req = Net::HTTP::Get.new(uri)
26
- when 'POST', 'post'
27
- req = Net::HTTP::Post.new(uri)
28
- end
30
+ def data(response)
31
+ { response: JSON.parse(response.body), status: response.success? }
32
+ end
29
33
 
30
- req
34
+ def default_cache
35
+ { size: 64.megabytes, expires_in: 1.minute }
36
+ end
37
+
38
+ def make_request(method, endpoint, params, headers)
39
+ connection = prepare_connection(method, endpoint, params, headers)
40
+
41
+ connection.public_send(method) do |request|
42
+ request.headers.merge!(headers)
43
+ request.url(endpoint)
44
+ request.body = params.to_json if %i[post put].include?(method)
31
45
  end
46
+ end
32
47
 
33
- def except_params(params)
34
- params.reject { |k, v| [:signature].include? k }
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
35
52
  end
36
53
  end
37
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Request
4
- VERSION = '0.0.0'
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.0
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-01-09 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
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 7.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 7.0.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bump
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.7'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: rake
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -172,5 +200,5 @@ requirements: []
172
200
  rubygems_version: 3.3.24
173
201
  signing_key:
174
202
  specification_version: 4
175
- summary: request0.0.0
203
+ summary: request0.0.2
176
204
  test_files: []