wcc-data 0.3.1 → 0.4.0.pre

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
  SHA1:
3
- metadata.gz: 27c7b33dc551ef4cfcf077dd1cd034a03b988000
4
- data.tar.gz: 8328780be7f3b99ac8c14a9899e1e16fda22dca0
3
+ metadata.gz: 82f9b734e798bdb01d28323037ba25ea1a87a831
4
+ data.tar.gz: 46b3020ff169cdfa6e687c9933b765a5f7ed26a6
5
5
  SHA512:
6
- metadata.gz: 7ec04ded3ce055bf9bc34dbc9cce85bb90bac2b6752a447b6f14ba2d7b383a4f837fa953e1da83c759a27038b5486d67b1e033b0ba25ee5f2a065668482b21ab
7
- data.tar.gz: 7357cb8d975af17773b9c39986dc5590e950b9e5506b25afd89487221895815b7c8bacb03af2f614052ee0b44dfb51f0317e144b8134425ce1d4012493ccbd07
6
+ metadata.gz: b527b25651b9e1e0930ba6c0f4c4b816e929b9efc664910d1b5dd7ce42936840f2b1ab46e1489654c05490a145426bf3121fb9efd236e4cf03d4d596f8f582d5
7
+ data.tar.gz: 4cf4c852f51a72b46ff553539a72903ef218e314eaafe70651b6945c587efececd064e0f222f2508ba75e6825884954eec81fbf5fc1b64f733ec3e82be292483
@@ -15,6 +15,7 @@ module WCC::Data
15
15
  private
16
16
 
17
17
  def set_default_applications
18
+ raise ArgumentError, "Missing NUCLEUS_URL environment variable" unless ENV['NUCLEUS_URL']
18
19
  applications[:nucleus].uri = ENV['NUCLEUS_URL']
19
20
  if ENV['APP_CLIENT_ID'] && ENV['APP_CLIENT_SECRET']
20
21
  applications[:nucleus].connection
@@ -1,64 +1,83 @@
1
+
2
+ begin
3
+ gem 'faraday'
4
+ require 'faraday'
5
+ rescue Gem::LoadError
6
+ # suppress
7
+ end
8
+
9
+
1
10
  module WCC::Data
2
- class FaradayClientAppTokenAuth < Faraday::Middleware
3
- class RedisCache
4
- DEFAULT_CACHE_KEY = "org.watermark.data.client-app-token-cache-store"
5
- attr_reader :connection, :cache_key
11
+ if defined?(Faraday)
12
+ class FaradayClientAppTokenAuth < Faraday::Middleware
13
+ class RedisCache
14
+ DEFAULT_CACHE_KEY = "org.watermark.data.client-app-token-cache-store"
15
+ attr_reader :connection, :cache_key
6
16
 
7
- def initialize(connection, cache_key: DEFAULT_CACHE_KEY)
8
- @connection = connection
9
- @cache_key = cache_key
10
- end
17
+ def initialize(connection, cache_key: DEFAULT_CACHE_KEY)
18
+ @connection = connection
19
+ @cache_key = cache_key
20
+ end
11
21
 
12
- def [](hostname)
13
- connection.call { |r| r.hget cache_key, hostname }
14
- end
22
+ def [](hostname)
23
+ connection.call { |r| r.hget cache_key, hostname }
24
+ end
15
25
 
16
- def []=(hostname, token)
17
- if token
18
- connection.call { |r| r.hset cache_key, hostname, token }
19
- else
20
- connection.call { |r| r.hdel cache_key, hostname }
26
+ def []=(hostname, token)
27
+ if token
28
+ connection.call { |r| r.hset cache_key, hostname, token }
29
+ else
30
+ connection.call { |r| r.hdel cache_key, hostname }
31
+ end
21
32
  end
22
33
  end
23
- end
24
34
 
25
- attr_reader :cache, :token_factory
35
+ attr_reader :cache, :token_factory
26
36
 
27
- def initialize(app, cache: default_cache, token_factory: method(:default_token_factory))
28
- super(app)
29
- @cache = cache
30
- @token_factory = token_factory
31
- end
37
+ def initialize(app, cache: default_cache, token_factory: method(:default_token_factory))
38
+ super(app)
39
+ @cache = cache
40
+ @token_factory = token_factory
41
+ end
32
42
 
33
- def token_for(host)
34
- @cache[host] ||= token_factory.(host)
35
- end
43
+ def token_for(host)
44
+ @cache[host] ||= token_factory.(host)
45
+ end
36
46
 
37
- def call(env, retry_on_forbidden: true)
38
- request_body = env[:body]
39
- host = env[:url].host
40
- env[:request_headers]["Authorization"] = "Bearer #{token_for(host)}"
41
- @app.call(env).on_complete do |response_env|
42
- response = Faraday::Response.new(response_env)
43
- cache[host] = nil unless response.success?
44
- if response.status == 403 && retry_on_forbidden
45
- env[:body] = request_body
46
- call(env, retry_on_forbidden: false)
47
+ def call(env, retry_on_forbidden: true)
48
+ request_body = env[:body]
49
+ host = env[:url].host
50
+ env[:request_headers]["Authorization"] = "Bearer #{token_for(host)}"
51
+ @app.call(env).on_complete do |response_env|
52
+ response = Faraday::Response.new(response_env)
53
+ cache[host] = nil unless response.success?
54
+ if response.status == 403 && retry_on_forbidden
55
+ env[:body] = request_body
56
+ call(env, retry_on_forbidden: false)
57
+ end
47
58
  end
48
59
  end
49
- end
50
60
 
51
- private
61
+ private
52
62
 
53
- def default_token_factory(host)
54
- WCC::Data::Nucleus::ClientAppToken.create(hostname: host).token
55
- end
63
+ def default_token_factory(host)
64
+ WCC::Data::Nucleus::ClientAppToken.create(hostname: host).token
65
+ end
56
66
 
57
- def default_cache
58
- RedisCache.new(Sidekiq.method(:redis))
67
+ def default_cache
68
+ RedisCache.new(Sidekiq.method(:redis))
69
+ end
70
+ end
71
+ else
72
+ class FaradayClientAppTokenAuth
73
+ def initialize(app, cache: default_cache, token_factory: method(:default_token_factory))
74
+ raise NotSupportedError, 'Please include the "faraday" gem to use this authentication method'
75
+ end
59
76
  end
60
77
  end
61
78
  end
62
79
 
63
- Faraday::Middleware.register_middleware :client_app_token => lambda { WCC::Data::FaradayClientAppTokenAuth }
80
+ if defined?(Faraday)
81
+ Faraday::Middleware.register_middleware :client_app_token => lambda { WCC::Data::FaradayClientAppTokenAuth }
82
+ end
64
83
 
@@ -5,6 +5,7 @@ module WCC::Data
5
5
  class EndpointUndefined < MapperError; end
6
6
  class InvalidResponse < MapperError; end
7
7
  class RecordNotFound < MapperError; end
8
+ class ServiceUnavailable < MapperError; end
8
9
  end
9
10
 
10
11
  end
@@ -4,6 +4,7 @@ module WCC::Data::Mapper
4
4
 
5
5
  def new_from_response(response)
6
6
  raise RecordNotFound if response.status == 404
7
+ raise ServiceUnavailable if response.status == 503
7
8
  case response.json
8
9
  when Array
9
10
  response.json.collect { |obj| new(obj) }
@@ -6,7 +6,7 @@ module WCC::Data::Nucleus
6
6
  attributes :street, :city, :state, :zip, :geo
7
7
 
8
8
  def matches?(value)
9
- [id, key].include?(value)
9
+ [id, key, code].include?(value)
10
10
  end
11
11
 
12
12
  def self.db
@@ -1,5 +1,5 @@
1
1
  module WCC
2
2
  module Data
3
- VERSION = '0.3.1'.freeze
3
+ VERSION = '0.4.0.pre'.freeze
4
4
  end
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -19,4 +19,6 @@ RSpec.configure do |config|
19
19
  config.filter_run :focus
20
20
 
21
21
  config.order = 'random'
22
+
23
+ ENV['NUCLEUS_URL'] ||= 'http://login.wcc'
22
24
  end
@@ -219,6 +219,19 @@ RSpec.describe WCC::Data::FaradayClientAppTokenAuth do
219
219
  expect(cache).to receive(:[]=).with("example.com", "abc123")
220
220
  auth.token_for("example.com")
221
221
  end
222
+
223
+ it "passes error up the chain when service unavailable" do
224
+ allow(auth.token_factory)
225
+ .to receive(:call)
226
+ .and_raise(WCC::Data::Mapper::ServiceUnavailable)
227
+
228
+ expect {
229
+ auth.call({
230
+ url: URI("http://example.com"),
231
+ request_headers: {},
232
+ })
233
+ }.to raise_error(WCC::Data::Mapper::ServiceUnavailable)
234
+ end
222
235
  end
223
236
  end
224
237
  end
@@ -41,6 +41,13 @@ describe WCC::Data::Mapper::JSONResponse do
41
41
 
42
42
  end
43
43
 
44
+ it "raises ServiceUnavailable when response status is 503" do
45
+ allow(singular_response).to receive(:status).and_return(503)
46
+ expect { klass.new_from_response(singular_response) }
47
+ .to raise_error(WCC::Data::Mapper::ServiceUnavailable)
48
+
49
+ end
50
+
44
51
  it "raises InvalidResponse error for non-json objects" do
45
52
  expect { klass.new_from_response(double(json: 1, status: 200)) }
46
53
  .to raise_error(WCC::Data::Mapper::InvalidResponse)
data/wcc-data.gemspec CHANGED
@@ -20,7 +20,8 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.required_ruby_version = '~> 2.2'
22
22
 
23
- spec.add_dependency "faraday", "~> 0.8"
23
+ # Required if you use faraday_client_app_token_auth but must be included separately
24
+ spec.add_development_dependency "faraday", "~> 0.8"
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.3"
26
27
  spec.add_development_dependency "dotenv", "~> 0.10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wcc-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Watermark Dev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-23 00:00:00.000000000 Z
11
+ date: 2021-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -17,7 +17,7 @@ dependencies:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.8'
20
- type: :runtime
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
@@ -157,7 +157,7 @@ homepage: ''
157
157
  licenses:
158
158
  - MIT
159
159
  metadata: {}
160
- post_install_message:
160
+ post_install_message:
161
161
  rdoc_options: []
162
162
  require_paths:
163
163
  - lib
@@ -168,13 +168,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
168
  version: '2.2'
169
169
  required_rubygems_version: !ruby/object:Gem::Requirement
170
170
  requirements:
171
- - - ">="
171
+ - - ">"
172
172
  - !ruby/object:Gem::Version
173
- version: '0'
173
+ version: 1.3.1
174
174
  requirements: []
175
- rubyforge_project:
176
- rubygems_version: 2.6.11
177
- signing_key:
175
+ rubyforge_project:
176
+ rubygems_version: 2.5.2.3
177
+ signing_key:
178
178
  specification_version: 4
179
179
  summary: Watermark's library for interapp communication via APIs
180
180
  test_files: