soapy_cake 2.3.0 → 2.3.1

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: e1a10b55da67455d7850300f6b1da3a3b4321a008df2986bf2de8dc53d05b5e2
4
- data.tar.gz: f74e861ce703df28b9ac771fa6fcaac96b8d6f2c1be2a725c0425f2fe9d240c9
3
+ metadata.gz: 067f6e7655ca70f82bdb56092d19dcfbb0f41879077ede785b6490ee40ba0192
4
+ data.tar.gz: 945cce14ca15a79590bef7b1bef2a636288da4cfb0ba93b6d9f50482ecdc533b
5
5
  SHA512:
6
- metadata.gz: 9fc9818fcecb399918332e5624b753cd7bf74030d4f460b1939f7e8c7859ed2fdd55c2d4d0e18448153f4b284e0c770f22a94feb73def6ab0c8afd487f319afb
7
- data.tar.gz: b6e3e18834d04dfd7548aa86837a2e322d519237635998077aa12f699454d0a0db5e5bf1deb2fbff6fe50246d57eb0c47b53c018e65a9275574b1e510cb79ee3
6
+ metadata.gz: bc1c8bbba8cf6b41ca72aeebe6c516b7a915a8a3fb1bead22794e87c600aba8b4e6d8b7c590e0e6c700e3a327f8eb0264b3f0142d837c6b3e13be7dbbffce967
7
+ data.tar.gz: 9ecf1e1d0d996917d3b079b8e74f0bd3db0f5ea67d27d276c78cb8387572045893b01555877aaab17c09e20ce5e7ba0df6a0b8ebfea7152416597160f917b791
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'net/http'
4
+ require 'active_support/tagged_logging'
4
5
 
6
+ # rubocop:disable Metrics/ClassLength
5
7
  module SoapyCake
6
8
  class Client
7
9
  HEADERS = { 'Content-Type' => 'application/soap+xml;charset=UTF-8' }.freeze
@@ -62,46 +64,58 @@ module SoapyCake
62
64
  Retryable.retryable(opts, &block)
63
65
  end
64
66
 
65
- def logger
66
- @logger ||= opts[:logger] || (defined?(::Rails) && ::Rails.logger)
67
+ def response_body(request)
68
+ request.opts[:response].presence || http_response(request)
67
69
  end
68
70
 
69
- def log_curl_command(request)
70
- curl_headers = HEADERS.map { |k, v| "-H \"#{k}: #{v}\"" }.join(' ')
71
- curl_body = request.xml
72
- .tr("\n", '')
73
- .gsub(/>\s*</, '><')
74
- .sub(request.api_key, '{{{ INSERT API KEY }}}')
71
+ def http_response(request)
72
+ response = nil
73
+ logger.tagged('soapy_cake', unique_id) do
74
+ log_request(request)
75
+ response = perform_http_request(http_request(request))
76
+ log_response(response)
77
+ end
75
78
 
76
- logger&.info("curl --data '#{curl_body}' #{curl_headers} https://#{domain}/#{request.path}")
79
+ raise_if_unsuccessful(response)
80
+ response.body
77
81
  end
78
82
 
79
- def response_body(request)
80
- request.opts[:response].presence || http_response(request)
83
+ def http_request(request)
84
+ http_req = Net::HTTP::Post.new(request.path, HEADERS)
85
+ http_req.body = request.xml
86
+ http_req
81
87
  end
82
88
 
83
- def http_response(request)
84
- log_curl_command(request) if fetch_opt(:log_curl)
85
-
86
- http_request = Net::HTTP::Post.new(request.path, HEADERS)
87
- http_request.body = request.xml
88
- t0 = Time.now
89
- response = perform_http_request(http_request)
90
- response_time = Time.now - t0
91
- logger&.info("soapy_cake:request #{request} took: #{response_time.round(2)} s")
89
+ def unique_id
90
+ SecureRandom.hex(4)
91
+ end
92
92
 
93
- unless response.is_a?(Net::HTTPSuccess)
94
- raise RequestFailed.new(
95
- "Request failed with HTTP #{response.code}",
96
- response_body: response.body
93
+ def log_request(request)
94
+ logger.tagged('request') do
95
+ logger.info(
96
+ request
97
+ .xml
98
+ .gsub(/>[\n\s]+</, '><')
99
+ .sub(request.api_key, '[REDACTED]')
97
100
  )
98
101
  end
102
+ end
99
103
 
100
- response.body
104
+ def log_response(response)
105
+ logger.tagged('response', response.code) do
106
+ logger.info(response.body)
107
+ end
108
+ end
109
+
110
+ def logger
111
+ @logger ||= ActiveSupport::TaggedLogging.new(
112
+ opts[:logger] || (defined?(::Rails) && ::Rails.logger) || Logger.new('/dev/null')
113
+ )
101
114
  end
102
115
 
103
116
  def perform_http_request(http_request)
104
- Net::HTTP.start(
117
+ t0 = Time.now
118
+ response = Net::HTTP.start(
105
119
  domain,
106
120
  use_ssl: true,
107
121
  open_timeout: NET_TIMEOUT,
@@ -109,6 +123,18 @@ module SoapyCake
109
123
  ) do |http|
110
124
  http.request(http_request)
111
125
  end
126
+ logger.info("took: #{(Time.now - t0).round(2)} s")
127
+ response
128
+ end
129
+
130
+ def raise_if_unsuccessful(response)
131
+ return if response.is_a?(Net::HTTPSuccess)
132
+
133
+ raise RequestFailed.new(
134
+ "Request failed with HTTP #{response.code}",
135
+ response_body: response.body
136
+ )
112
137
  end
113
138
  end
114
139
  end
140
+ # rubocop:enable Metrics/ClassLength
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SoapyCake
4
- VERSION = '2.3.0'
4
+ VERSION = '2.3.1'
5
5
  end
@@ -3,18 +3,21 @@
3
3
  RSpec.describe SoapyCake::Admin do
4
4
  around { |example| Timecop.freeze(Time.utc(2015, 6, 15, 12), &example) }
5
5
 
6
- let(:logger) { instance_spy(Logger) }
6
+ let(:logger) { Logger.new('/dev/null') }
7
7
 
8
8
  subject(:admin) { described_class.new(logger: logger) }
9
9
 
10
10
  it 'returns an affiliate with correct data types', :vcr do
11
- result = admin.affiliates(affiliate_id: 16027)
12
-
13
- expect(logger).to have_received(:info).with(
14
- a_string_matching(
15
- /soapy_cake:request admin:export:affiliates:5 {"affiliate_id":16027} took: \d+.\d+ s/
16
- )
11
+ # rubocop:disable RSpec/AnyInstance
12
+ expect_any_instance_of(Logger).to receive(:info).twice.with(
13
+ a_string_matching(/^<\?xml/)
17
14
  )
15
+ expect_any_instance_of(Logger).to receive(:info).with(
16
+ a_string_matching(/took: \d+.\d+ s/)
17
+ )
18
+ # rubocop:enable RSpec/AnyInstance
19
+
20
+ result = admin.affiliates(affiliate_id: 16027)
18
21
 
19
22
  expect(result.count).to eq(1)
20
23
  expect(result.first).to include(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soapy_cake
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 2.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ad2games GmbH
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-14 00:00:00.000000000 Z
11
+ date: 2020-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -272,7 +272,7 @@ homepage: http://ad2games.com
272
272
  licenses:
273
273
  - MIT
274
274
  metadata: {}
275
- post_install_message:
275
+ post_install_message:
276
276
  rdoc_options: []
277
277
  require_paths:
278
278
  - lib
@@ -287,9 +287,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
287
287
  - !ruby/object:Gem::Version
288
288
  version: '0'
289
289
  requirements: []
290
- rubyforge_project:
290
+ rubyforge_project:
291
291
  rubygems_version: 2.7.6
292
- signing_key:
292
+ signing_key:
293
293
  specification_version: 4
294
294
  summary: Simple client for the CAKE API
295
295
  test_files: