ncore 3.6.1 → 3.7.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
  SHA256:
3
- metadata.gz: 7768ca94332000d27a96d5eef72643b122d592461be7c46bcd46f0d3ac4cfa37
4
- data.tar.gz: f247238c94a6a94011df71583b7f85f2a04e30b52edf6bbf3c908b06fc5be9ba
3
+ metadata.gz: d88aebda2989bb9112e2e0a6a90a0eb4fb6224db0563560df03fa70f602dc03d
4
+ data.tar.gz: e1933d625a8771f26a6697ad3d4d1bd34cc63c67ae69b07516b935d25389c55b
5
5
  SHA512:
6
- metadata.gz: 896e527a4b7d5dc140dc9b21c51bbb1755488f505fd4815bed349a739d9de170ebb0773786f156857f6a6cd188adf2999982d519a3d9d53762b1a477dd51186f
7
- data.tar.gz: 43f131a9684c8fde7dc088724c0ed5d4ab64e7b3f7f0ed6e150eddcfadf6e97d88f6335e96108d5b341f4051ace9f8fc5dc0b88371a734deb29381a7e55d574d
6
+ metadata.gz: 29288f675a0ba7efe0a79ece9235bb793258c216a48f3874a0129aa089e4fe59eb82572aaf04a9a4810f6bd57d9bcc98b3abab00d516f3c3511ecd0edf73de07
7
+ data.tar.gz: 610ae4cdf2d632b06a4938f22c2f95dd7c2904ae9a98a12b721f6113fa99b0e67125c85fd973a346e3d128121e3f7a4324a204acf987e38a2873ede293462286
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ #### 3.7.0
2
+
3
+ - Allow request params to be or contain ActionController::Parameters
4
+ Unless MultiJson is present, this changes json generation to use the
5
+ configured encoder in ActiveSupport::JSON::Encoding.json_encoder instead
6
+ of directly using ::JSON.
7
+ - Change request header names to lowercase
8
+ - Log 404, 409, 422 responses at :debug instead of :error
9
+
10
+ #### 3.6.2
11
+
12
+ - Ensure cache key is stable
13
+
1
14
  #### 3.6.1
2
15
 
3
16
  - Restore usage of Resource.new(some_attr: 'value')
@@ -27,7 +27,7 @@ module MyApi
27
27
 
28
28
  self.status_page = 'http://my.api.status.page'
29
29
 
30
- self.auth_header_prefix = 'X-MyApi'
30
+ self.auth_header_prefix = 'x-myapi'
31
31
 
32
32
  # self.bearer_credential_key = :api_key
33
33
 
data/lib/ncore/builder.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module NCore
2
2
  module Builder
3
3
  extend ActiveSupport::Concern
4
-
4
+
5
5
  included do
6
6
  class_eval <<-INCL, __FILE__, __LINE__+1
7
7
  include NCore::Exceptions
@@ -24,7 +24,7 @@ module NCore
24
24
 
25
25
  class << self
26
26
  def configure(&block)
27
- Api.instance_eval &block
27
+ Api.instance_eval(&block)
28
28
  end
29
29
  end
30
30
  INCL
data/lib/ncore/client.rb CHANGED
@@ -26,15 +26,15 @@ module NCore
26
26
 
27
27
  path = URI.parse(url).path
28
28
  if [:get, :head, :delete].include? method
29
- qs = build_query_string params
29
+ qs = build_query_string params.as_json
30
30
  url += qs
31
31
  path += qs
32
32
  payload = nil
33
33
  else
34
34
  if defined? MultiJson
35
- payload = MultiJson.encode params
35
+ payload = MultiJson.encode params.as_json
36
36
  else
37
- payload = JSON.generate params
37
+ payload = params.to_json
38
38
  end
39
39
  end
40
40
 
@@ -79,7 +79,7 @@ module NCore
79
79
  h = {}
80
80
  [default_headers, auth_headers(req_credentials), headers].each do |set|
81
81
  set.each do |k,v|
82
- k = k.to_s.tr('_','-').gsub(%r{(^|-)\w}){$&.upcase}
82
+ k = k.to_s.tr('_','-').downcase
83
83
  h[k] = v.respond_to?(:call) ? v.call : v
84
84
  end
85
85
  end
@@ -118,7 +118,7 @@ module NCore
118
118
 
119
119
  # create a new excon client if necessary
120
120
  # keeps a pool of the last 10 urls
121
- # in almost all cases this will be more than enough
121
+ # in almost all cases this will be more than enough
122
122
  def excon_client(uri)
123
123
  u = URI.parse uri
124
124
  u.path = ''
@@ -153,6 +153,7 @@ module NCore
153
153
  end
154
154
 
155
155
  def pool
156
+ # is actually fiber-local
156
157
  Thread.current[:ncore_pool] ||= {}
157
158
  end
158
159
 
@@ -171,7 +172,6 @@ module NCore
171
172
  response = nil
172
173
  begin
173
174
  ActiveSupport::Notifications.instrument(instrument_key, rest_opts) do
174
-
175
175
  connection = excon_client(rest_opts[:url])
176
176
  begin
177
177
  tries += 1
@@ -286,9 +286,9 @@ module NCore
286
286
  creds.inject({}) do |h,(k,v)|
287
287
  if v.present?
288
288
  if k == bearer_credential_key
289
- h["Authorization"] = "Bearer #{v}"
289
+ h['authorization'] = "Bearer #{v}"
290
290
  else
291
- h["#{auth_header_prefix}-#{k}"] = v
291
+ h["#{auth_header_prefix}-#{k}"] = v
292
292
  end
293
293
  end
294
294
  h
@@ -325,6 +325,7 @@ module NCore
325
325
 
326
326
  def debug_response(response)
327
327
  return unless logger.debug?
328
+ headers = response.headers.transform_keys(&:downcase)
328
329
  if defined? MultiJson
329
330
  json = MultiJson.load(response.body||'', symbolize_keys: false) rescue response.body
330
331
  else
@@ -332,8 +333,8 @@ module NCore
332
333
  end
333
334
  logger << <<~DBG
334
335
  RESPONSE:
335
- #{response.headers['Status']} | #{response.headers['Content-Type']} | #{response.body.size} bytes
336
- #{response.headers.except('Status', 'Connection', 'Content-Type').map{|h,d| "#{h}: #{d}"}.join("\n ")}
336
+ #{response.status} | #{headers['content-type']} | #{response.body.size} bytes
337
+ #{headers.except('status', 'connection', 'content-length', 'content-type').map{|h,d| "#{h}: #{d}"}.join("\n ")}
337
338
  #{json.pretty_inspect.split("\n").join("\n ")}
338
339
  #{'-=- '*18}
339
340
  DBG
@@ -37,10 +37,11 @@ module NCore
37
37
 
38
38
 
39
39
  def request_cache_key(url:, headers:)
40
+ hdrs = headers.reject{|k,v| k=='x-request-id'}.sort
40
41
  [ 'ncore',
41
42
  url.gsub(/[^a-zA-Z0-9]+/,'-'),
42
- Digest::MD5.hexdigest(headers.sort.to_s)
43
- ].join ':'
43
+ Digest::MD5.hexdigest(hdrs.to_s)
44
+ ].join(':')
44
45
  end
45
46
 
46
47
  end
@@ -38,7 +38,7 @@ module NCore
38
38
  self.status_page = 'the status page'
39
39
 
40
40
  mattr_accessor :auth_header_prefix
41
- self.auth_header_prefix = 'X-Api'
41
+ self.auth_header_prefix = 'x-api'
42
42
 
43
43
  mattr_reader :bearer_credential_key
44
44
  class_eval <<-MTH
@@ -7,7 +7,7 @@ module NCore
7
7
  raise(module_parent::RecordNotFound, "ids must not be empty") if ids.blank?
8
8
  params[:ids] = ids
9
9
  params = parse_request_params(params)
10
- parsed, creds = request(:delete, resource_path, params)
10
+ parsed, _creds = request(:delete, resource_path, params)
11
11
  if parsed[:errors].any?
12
12
  raise module_parent::BulkActionError, parsed[:errors]
13
13
  else
@@ -7,7 +7,7 @@ module NCore
7
7
  raise(module_parent::RecordNotFound, "ids must not be empty") if ids.blank?
8
8
  params = parse_request_params(params, json_root: json_root)
9
9
  params[:ids] = ids
10
- parsed, creds = request(:put, resource_path, params)
10
+ parsed, _creds = request(:put, resource_path, params)
11
11
  if parsed[:errors].any?
12
12
  raise module_parent::BulkActionError, parsed[:errors]
13
13
  else
@@ -26,6 +26,8 @@ module NCore
26
26
  end
27
27
  end
28
28
 
29
+ DEBUG_STATUSES = (200..299).to_a + [404, 409, 422]
30
+
29
31
  def log_request(event)
30
32
  self.class.runtime += event.duration
31
33
 
@@ -38,13 +40,14 @@ module NCore
38
40
  res = " -> %d (%.1f ms)" % [http_status, event.duration]
39
41
 
40
42
  msg = color(msg, :yellow)
43
+ # for rails≤70, must exclude :bold param entirely since a Hash is truthy
41
44
  if (200..299).include? http_status
42
45
  res = color(res, :green, bold: true)
43
46
  else
44
47
  res = color(res, :red, bold: true)
45
48
  end
46
49
 
47
- if (200..299).include? http_status
50
+ if DEBUG_STATUSES.include? http_status
48
51
  debug " #{msg}"
49
52
  debug " #{res}"
50
53
  else
data/lib/ncore/util.rb CHANGED
@@ -53,7 +53,7 @@ module NCore
53
53
  klass_name = key.to_s.camelize.singularize
54
54
  begin
55
55
  "#{module_name}::#{klass_name}".constantize
56
- rescue NameError => e
56
+ rescue NameError
57
57
  default_klass
58
58
  end
59
59
  end
data/lib/ncore/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module NCore
2
- VERSION = '3.6.1'
2
+ VERSION = '3.7.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ncore
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.1
4
+ version: 3.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Notioneer Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-01 00:00:00.000000000 Z
11
+ date: 2023-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  requirements: []
145
- rubygems_version: 3.3.26
145
+ rubygems_version: 3.4.10
146
146
  signing_key:
147
147
  specification_version: 4
148
148
  summary: NCore - Gem for building REST API clients