whiplash-app 0.9.2 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/whiplash/app/api_config.rb +6 -4
- data/lib/whiplash/app/connections.rb +1 -1
- data/lib/whiplash/app/controller_helpers.rb +80 -0
- data/lib/whiplash/app/railtie.rb +19 -2
- data/lib/whiplash/app/version.rb +1 -1
- data/lib/whiplash/app.rb +18 -10
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 228da95848c936a8a771e6f67e6d4aa03899ab722274b2d355917593c9b83a89
|
4
|
+
data.tar.gz: 2f1f50222b2b7a578d23a88efdfd5328e2d0b772bdae0696249be13457e57db5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31ba3a5b17e6679551d961ce009efb489e3a4a49bf0cf5cf8e9d4e978626c6ae825a38dad30d62552cf619e08a78a09066cd3ffb67a04599f5dcb6f7d01bc4a4
|
7
|
+
data.tar.gz: e845ad83845b36f932d21a3fd4fa283e8ef0fc45066cc37a82f80c3c178600a3c4870c76633f9ee88aad1967b16db7e7caf280a55e91b2f0d0d7fcf4ce6965e1
|
@@ -10,6 +10,12 @@ module Whiplash
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
def rate_limit
|
14
|
+
(ENV['WHIPLASH_RATE_LIMIT'] || 25).to_i
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
13
19
|
def production_url
|
14
20
|
ENV["WHIPLASH_API_URL"] || "https://www.getwhiplash.com"
|
15
21
|
end
|
@@ -18,10 +24,6 @@ module Whiplash
|
|
18
24
|
ENV["WHIPLASH_API_URL"] || "https://sandbox.getwhiplash.com"
|
19
25
|
end
|
20
26
|
|
21
|
-
def rate_limit
|
22
|
-
(ENV['WHIPLASH_RATE_LIMIT'] || 25).to_i
|
23
|
-
end
|
24
|
-
|
25
27
|
end
|
26
28
|
end
|
27
29
|
end
|
@@ -27,7 +27,7 @@ module Whiplash
|
|
27
27
|
|
28
28
|
def app_request(options={})
|
29
29
|
return base_app_request(options) unless defined?(Sidekiq)
|
30
|
-
limiter = Sidekiq::Limiter.window('whiplash-core', self.rate_limit, :second, wait_timeout: 15)
|
30
|
+
limiter = Sidekiq::Limiter.window('whiplash-core', self.class.rate_limit, :second, wait_timeout: 15)
|
31
31
|
limiter.within_limit do
|
32
32
|
base_app_request(options)
|
33
33
|
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Whiplash
|
3
|
+
class App
|
4
|
+
module ControllerHelpers
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
helper_method :cookie_domain,
|
9
|
+
:core_url,
|
10
|
+
:core_url_for,
|
11
|
+
:current_user
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def cookie_domain
|
17
|
+
'.' + URI.parse(core_url).host
|
18
|
+
end
|
19
|
+
|
20
|
+
def core_url
|
21
|
+
ENV['WHIPLASH_API_URL']
|
22
|
+
end
|
23
|
+
|
24
|
+
def core_url_for(path)
|
25
|
+
[core_url, path].join('/')
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_user
|
29
|
+
return if cookies[:user].blank?
|
30
|
+
begin
|
31
|
+
@current_user ||= JSON.parse(cookies[:user])
|
32
|
+
rescue StandardError => e
|
33
|
+
Rails.logger.warn "User could not be initialized: #{e.message}"
|
34
|
+
@current_user = nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def http_scheme
|
39
|
+
URI(core_url).scheme
|
40
|
+
end
|
41
|
+
|
42
|
+
def init_whiplash_api(options = {})
|
43
|
+
return redirect_to core_url_for('login') if cookies[:oauth_token].blank?
|
44
|
+
token = {access_token: cookies[:oauth_token]}
|
45
|
+
begin
|
46
|
+
@whiplash_api = Whiplash::App.new(token, options)
|
47
|
+
rescue StandardError => e
|
48
|
+
Rails.logger.warn "API failed to initialize: #{e.message}"
|
49
|
+
@whiplash_api = nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def require_user
|
54
|
+
redirect_to core_url_for('login') if current_user.blank?
|
55
|
+
end
|
56
|
+
|
57
|
+
def set_locale!
|
58
|
+
I18n.default_locale = :en
|
59
|
+
I18n.locale = current_user.try('locale') || I18n.default_locale
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def set_current_user_cookie!(expires_at = nil)
|
64
|
+
user = @whiplash_api.get!("me").body
|
65
|
+
fields_we_care_about = %w(id email role locale first_name last_name partner_id warehouse_id customer_ids)
|
66
|
+
user_hash = user.slice(*fields_we_care_about)
|
67
|
+
expires_at ||= user['current_sign_in_expires_at']
|
68
|
+
|
69
|
+
shared_values = {
|
70
|
+
expires: DateTime.parse(expires_at),
|
71
|
+
secure: http_scheme == 'https',
|
72
|
+
samesite: :strict,
|
73
|
+
domain: cookie_domain
|
74
|
+
}
|
75
|
+
cookies[:user] = shared_values.merge(value: user_hash.to_json)
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/lib/whiplash/app/railtie.rb
CHANGED
@@ -1,14 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Whiplash
|
4
|
-
|
4
|
+
class App
|
5
5
|
class Railtie < Rails::Railtie
|
6
|
+
|
7
|
+
config.before_configuration do |app|
|
8
|
+
# App name/etc, mainly for consistency in logging
|
9
|
+
app_name = app.class.module_parent.name.underscore.dasherize
|
10
|
+
app.config.environment_key = ENV.fetch('ENVIRONMENT_KEY', Rails.env.to_s)
|
11
|
+
app.config.application_key = ENV.fetch('APPLICATION_KEY', app_name)
|
12
|
+
app.config.application_name_space = [config.application_key, config.environment_key].join('-')
|
13
|
+
|
14
|
+
# session settings
|
15
|
+
session_days = 30
|
16
|
+
session_seconds = session_days * 24 * 60 * 60
|
17
|
+
session_length = ENV.fetch('SESSION_LENGTH', session_seconds).to_i
|
18
|
+
app.config.session_length = session_length
|
19
|
+
app.config.session_store :cookie_store, :key => '_session', :expire_after => session_length
|
20
|
+
end
|
21
|
+
|
6
22
|
initializer "whiplash_app.action_controller" do
|
7
23
|
ActiveSupport.on_load(:action_controller) do
|
8
|
-
puts "Extending #{self} with YourGemsModuleName::Controller"
|
9
24
|
include Whiplash::App::CanonicalHost
|
25
|
+
include Whiplash::App::ControllerHelpers
|
10
26
|
end
|
11
27
|
end
|
28
|
+
|
12
29
|
end
|
13
30
|
end
|
14
31
|
end
|
data/lib/whiplash/app/version.rb
CHANGED
data/lib/whiplash/app.rb
CHANGED
@@ -9,16 +9,17 @@ require "faraday"
|
|
9
9
|
|
10
10
|
# Rails app stuff
|
11
11
|
if defined?(Rails::Railtie)
|
12
|
-
require "whiplash/app/canonical_host"
|
13
12
|
require "whiplash/app/railtie"
|
13
|
+
require "whiplash/app/canonical_host"
|
14
|
+
require "whiplash/app/controller_helpers"
|
14
15
|
end
|
15
16
|
|
16
17
|
module Whiplash
|
17
18
|
class App
|
18
|
-
|
19
|
+
extend Whiplash::App::Signing
|
20
|
+
extend Whiplash::App::ApiConfig
|
19
21
|
include Whiplash::App::Connections
|
20
22
|
include Whiplash::App::FinderMethods
|
21
|
-
extend Whiplash::App::Signing
|
22
23
|
|
23
24
|
attr_accessor :customer_id, :shop_id, :token
|
24
25
|
|
@@ -29,16 +30,16 @@ module Whiplash
|
|
29
30
|
@api_version = options[:api_version] || 2 # can be 2_1
|
30
31
|
end
|
31
32
|
|
32
|
-
def client
|
33
|
-
OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: api_url)
|
34
|
-
end
|
35
|
-
|
36
33
|
def versioned_api_url
|
37
34
|
"api/v#{@api_version}"
|
38
35
|
end
|
39
36
|
|
37
|
+
def client
|
38
|
+
OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: self.class.api_url)
|
39
|
+
end
|
40
|
+
|
40
41
|
def connection
|
41
|
-
Faraday.new [api_url, versioned_api_url].join("/") do |conn|
|
42
|
+
Faraday.new [self.class.api_url, versioned_api_url].join("/") do |conn|
|
42
43
|
conn.request :authorization, 'Bearer', token.token
|
43
44
|
conn.request :json
|
44
45
|
conn.response :json, :content_type => /\bjson$/
|
@@ -53,9 +54,9 @@ module Whiplash
|
|
53
54
|
case ENV["WHIPLASH_CLIENT_SCOPE"]
|
54
55
|
when /app_(manage|read)/
|
55
56
|
begin
|
56
|
-
access_token =
|
57
|
+
access_token = self.class.client_credentials_token
|
57
58
|
rescue URI::InvalidURIError => e
|
58
|
-
raise StandardError, "The
|
59
|
+
raise StandardError, "The provided URL (#{ENV["WHIPLASH_API_URL"]}) is not valid"
|
59
60
|
end
|
60
61
|
else
|
61
62
|
raise StandardError, "You must request an access token before you can refresh it" if token.nil?
|
@@ -70,6 +71,13 @@ module Whiplash
|
|
70
71
|
false
|
71
72
|
end
|
72
73
|
|
74
|
+
class << self
|
75
|
+
def client_credentials_token
|
76
|
+
client = OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: api_url)
|
77
|
+
client.client_credentials.get_token(scope: ENV["WHIPLASH_CLIENT_SCOPE"])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
73
81
|
private
|
74
82
|
def format_token(oauth_token)
|
75
83
|
return oauth_token if oauth_token.is_a?(OAuth2::AccessToken)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: whiplash-app
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Don Sullivan, Mark Dickson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth2
|
@@ -117,6 +117,7 @@ files:
|
|
117
117
|
- lib/whiplash/app/api_config.rb
|
118
118
|
- lib/whiplash/app/canonical_host.rb
|
119
119
|
- lib/whiplash/app/connections.rb
|
120
|
+
- lib/whiplash/app/controller_helpers.rb
|
120
121
|
- lib/whiplash/app/finder_methods.rb
|
121
122
|
- lib/whiplash/app/railtie.rb
|
122
123
|
- lib/whiplash/app/signing.rb
|