hubrise_app 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +2 -0
  4. data/app/controllers/hubrise_app/application_controller.rb +3 -66
  5. data/app/controllers/hubrise_app/application_controller/app_instance_methods.rb +28 -0
  6. data/app/controllers/hubrise_app/application_controller/hubrise_gateway_methods.rb +25 -0
  7. data/app/controllers/hubrise_app/application_controller/session_methods.rb +33 -0
  8. data/app/controllers/hubrise_app/callback_controller.rb +8 -11
  9. data/app/controllers/hubrise_app/callback_controller/action_disconnect.rb +5 -0
  10. data/app/controllers/hubrise_app/callback_controller/action_event.rb +5 -0
  11. data/app/controllers/hubrise_app/oauth_controller.rb +5 -32
  12. data/app/controllers/hubrise_app/oauth_controller/action_authorize_callback.rb +13 -0
  13. data/app/controllers/hubrise_app/oauth_controller/action_connect_callback.rb +12 -0
  14. data/app/controllers/hubrise_app/oauth_controller/action_login_callback.rb +7 -0
  15. data/app/controllers/hubrise_app/sessions_controller.rb +9 -0
  16. data/app/lib/hubrise_app/config_loader.rb +5 -0
  17. data/app/lib/hubrise_app/hubrise_gateway.rb +36 -24
  18. data/app/lib/hubrise_app/refresher/account.rb +23 -0
  19. data/app/lib/hubrise_app/refresher/app_instance.rb +31 -0
  20. data/app/lib/hubrise_app/refresher/location.rb +18 -0
  21. data/app/lib/hubrise_app/refresher/user.rb +16 -0
  22. data/app/lib/hubrise_app/services/assign_app_instance.rb +8 -0
  23. data/app/lib/hubrise_app/services/connect_app_instance.rb +5 -4
  24. data/app/lib/hubrise_app/services/resolve_app_instance.rb +5 -0
  25. data/app/models/hubrise_app/hr_account.rb +1 -15
  26. data/app/models/hubrise_app/hr_app_instance.rb +0 -27
  27. data/app/models/hubrise_app/hr_location.rb +1 -7
  28. data/app/models/hubrise_app/hr_user.rb +6 -24
  29. data/app/models/hubrise_app/hr_user_app_instance.rb +0 -4
  30. data/config/initializers/hubrise_app_config.rb +1 -1
  31. data/config/routes.rb +2 -0
  32. data/lib/hubrise_app/spec_support.rb +3 -0
  33. data/lib/hubrise_app/version.rb +1 -1
  34. metadata +27 -17
  35. data/MIT-LICENSE +0 -20
  36. data/app/lib/hubrise_app/services/disconnect_app_instance.rb +0 -6
  37. data/app/lib/hubrise_app/services/handle_event.rb +0 -22
  38. data/app/lib/hubrise_app/services/override/connect_app_instance.rb +0 -1
  39. data/app/lib/hubrise_app/services/override/disconnect_app_instance.rb +0 -1
  40. data/app/lib/hubrise_app/services/override/handle_event.rb +0 -1
  41. data/app/models/concerns/hubrise_app/hr_api_resource.rb +0 -29
  42. data/app/models/hubrise_app/override/hr_user.rb +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7967fa6921a0293b0bb9ba4dc2e34573fc34721bf62732c626d4e3ed7746cd1b
4
- data.tar.gz: 0cfcc2aea23f782afcfd4b59019d63fbc38e9a77682df8130a3fe8273087bd35
3
+ metadata.gz: 9c1a7fcf2cdf500f0f88f966e88a651ea31995d37de7aaae211510ad8786fa31
4
+ data.tar.gz: 38011db1a46d6bd19acdbf05aa79694a07022b586ee3e921e070ac0a600f9142
5
5
  SHA512:
6
- metadata.gz: 778100fedf85bc588497d93161813e2eabe8c18f83d565d1fa691a949e85917a37a549a3fb38a96d7a6193094dd44bdd5d6eb759edf113bd0758573a3375ddd5
7
- data.tar.gz: 84994e316f35f455815dd0d8ad6d735b2d2a7d1b96d84ba568027820498fbf727a4d9fda81e85a165f479556827f0a8e4d61d056e7afdce61503583110a03551
6
+ metadata.gz: 1a045f2108e00c6287b6dabdce36221992c459af25eb69343f24dd95e6c5debb3597ebc37f427e8506a4cb5b4275626341fa53f82596a95cd8ebedd84eee6c44
7
+ data.tar.gz: 3d2fa4490ea440597306760a2e009275d317038755e257c83b0786ee30345ca565ab1cd9f515f4db5033e6f8e1787b5b89e87b0a6cdded807da7d7c22536edf0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2016- HubRise (https://www.hubrise.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ ![](https://github.com/hubrise/ruby-app/workflows/spec/badge.svg)
2
+
1
3
  ## Customization
2
4
  To override `oauth_controller` add `controllers/hubrise_app/override/oauth_controller.rb` to the host app
3
5
 
@@ -1,70 +1,7 @@
1
1
  module HubriseApp
2
2
  class ApplicationController < ActionController::Base
3
- protect_from_forgery with: :reset_session
4
- helper_method :current_hr_user, :current_hr_app_instance, :logged_in?
5
-
6
- protected
7
-
8
- ###########
9
- # SESSION #
10
- ###########
11
-
12
- def login(hr_user)
13
- session[:user_id] = hr_user.id
14
- @current_hr_user = hr_user
15
- end
16
-
17
- def logout
18
- session[:user_id] = nil
19
- end
20
-
21
- def current_hr_user
22
- @current_hr_user ||= HrUser.where(id: session[:user_id]).take
23
- end
24
-
25
- def logged_in?
26
- !!current_hr_user
27
- end
28
-
29
- def ensure_authenticated!
30
- redirect_to(build_hubrise_oauth_login_url) unless logged_in?
31
- end
32
-
33
- ##############
34
- # HR METHODS #
35
- ##############
36
-
37
- def hr_app_instance_id
38
- params[:app_instance_id]
39
- end
40
-
41
- def default_url_options
42
- super.merge(app_instance_id: hr_app_instance_id || current_hr_app_instance&.hr_id)
43
- end
44
-
45
- def current_hr_app_instance
46
- @hr_app_instance ||= current_hr_user && begin
47
- current_hr_user.hr_app_instances.where(hr_id: hr_app_instance_id).includes(:hr_account, :hr_location).take
48
- end
49
- end
50
-
51
- def build_hubrise_oauth_login_url
52
- HubriseGateway.build_login_authorization_url(
53
- hubrise_app.hubrise_oauth_login_callback_url
54
- )
55
- end
56
-
57
- def build_hubrise_oauth_authorize_url
58
- HubriseGateway.build_app_authorization_url(hr_app_instance_id,
59
- hubrise_app.hubrise_oauth_authorize_callback_url)
60
- end
61
-
62
- def ensure_hr_app_instance_found!
63
- if hr_app_instance_id.blank?
64
- render(plain: "Something went wrong. Please try to reopen from Hubrise Dashboard.")
65
- elsif current_hr_app_instance.nil?
66
- redirect_to(build_hubrise_oauth_authorize_url)
67
- end
68
- end
3
+ include AppInstanceMethods
4
+ include HubriseGatewayMethods
5
+ include SessionMethods
69
6
  end
70
7
  end
@@ -0,0 +1,28 @@
1
+ module HubriseApp::ApplicationController::AppInstanceMethods
2
+ extend ActiveSupport::Concern
3
+ included do
4
+ helper_method :current_hr_app_instance
5
+ end
6
+
7
+ def hr_app_instance_id
8
+ params[:app_instance_id]
9
+ end
10
+
11
+ def current_hr_app_instance
12
+ if current_hr_user
13
+ @hr_app_instance ||= HubriseApp::Services::ResolveAppInstance.run(current_hr_user.hr_app_instances, hr_app_instance_id, self)
14
+ end
15
+ end
16
+
17
+ def ensure_hr_app_instance_found!
18
+ if hr_app_instance_id.blank?
19
+ render(plain: "Something went wrong. Please try to reopen from Hubrise Dashboard.")
20
+ elsif current_hr_app_instance.nil?
21
+ redirect_to(build_hubrise_oauth_authorize_url)
22
+ end
23
+ end
24
+
25
+ def default_url_options
26
+ super.merge(app_instance_id: hr_app_instance_id || current_hr_app_instance&.hr_id)
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ module HubriseApp::ApplicationController::HubriseGatewayMethods
2
+ def hubrise_gateway
3
+ @hubrise_gateway ||= HubriseApp::HubriseGateway.new
4
+ end
5
+
6
+ def build_hubrise_oauth_login_url
7
+ hubrise_gateway.build_login_authorization_url(build_hubrise_oauth_login_callback_url)
8
+ end
9
+
10
+ def build_hubrise_oauth_authorize_url
11
+ hubrise_gateway.build_app_authorization_url(hr_app_instance_id, build_hubrise_oauth_authorize_callback_url)
12
+ end
13
+
14
+ def build_hubrise_oauth_login_callback_url
15
+ hubrise_app.hubrise_oauth_login_callback_url
16
+ end
17
+
18
+ def build_hubrise_oauth_authorize_callback_url
19
+ hubrise_app.hubrise_oauth_authorize_callback_url
20
+ end
21
+
22
+ def build_hubrise_open_url
23
+ main_app.hubrise_open_path
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ module HubriseApp::ApplicationController::SessionMethods
2
+ extend ActiveSupport::Concern
3
+ included do
4
+ protect_from_forgery with: :reset_session
5
+ helper_method :current_hr_user, :logged_in?
6
+ end
7
+
8
+ def login(hr_user)
9
+ session[:user_id] = hr_user.id
10
+ @current_hr_user = hr_user
11
+ end
12
+
13
+ def logout
14
+ session[:user_id] = nil
15
+ end
16
+
17
+ def current_hr_user
18
+ @current_hr_user ||= HubriseApp::HrUser.where(id: session[:user_id]).take
19
+ end
20
+
21
+ def logged_in?
22
+ !!current_hr_user
23
+ end
24
+
25
+ def ensure_authenticated!
26
+ unless logged_in?
27
+ redirect_to(build_hubrise_oauth_login_url)
28
+ return
29
+ end
30
+
31
+ yield if block_given?
32
+ end
33
+ end
@@ -1,26 +1,23 @@
1
1
  module HubriseApp
2
- class CallbackController < ActionController::Base
2
+ class CallbackController < ApplicationController
3
3
  skip_before_action :verify_authenticity_token
4
4
  before_action :ensure_hr_app_instance_found!
5
5
 
6
- def event
7
- Services::HandleEvent.run(current_hr_app_instance, params.permit!.to_h)
8
- head 200
9
- end
10
-
11
- def disconnect
12
- Services::DisconnectAppInstance.run(current_hr_app_instance)
13
- head 200
14
- end
6
+ include ActionEvent
7
+ include ActionDisconnect
15
8
 
16
9
  protected
17
10
 
18
11
  def current_hr_app_instance
19
- @hr_app_instance ||= HubriseApp::HrAppInstance.where(hr_id: params[:app_instance_id]).take
12
+ HubriseApp::Services::ResolveAppInstance.run(HrAppInstance, params[:app_instance_id], self)
20
13
  end
21
14
 
22
15
  def ensure_hr_app_instance_found!
23
16
  head(404) unless current_hr_app_instance
24
17
  end
18
+
19
+ def event_params
20
+ params.require(:callback).permit!
21
+ end
25
22
  end
26
23
  end
@@ -0,0 +1,5 @@
1
+ module HubriseApp::CallbackController::ActionDisconnect
2
+ def disconnect
3
+ head 200
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module HubriseApp::CallbackController::ActionEvent
2
+ def event
3
+ head 200
4
+ end
5
+ end
@@ -1,44 +1,17 @@
1
1
  module HubriseApp
2
2
  class OauthController < ApplicationController
3
- before_action :ensure_authenticated!, only: :authorize_callback
4
-
5
- def login_callback
6
- hr_user = HrUser.refresh_or_create_via_api_client(api_client_from_oauth_code)
7
- login(hr_user)
8
- redirect_to(main_app.hubrise_open_path)
9
- end
10
-
11
- def connect_callback
12
- @hr_app_instance = HrAppInstance.refresh_or_create_via_api_client(api_client_from_oauth_code)
13
-
14
- Services::ConnectAppInstance.run(current_hr_app_instance, hubrise_callback_event_url: hubrise_callback_event_url)
15
-
16
- if logged_in?
17
- current_hr_user.assign_hr_app_instance(current_hr_app_instance)
18
- redirect_to(main_app.hubrise_open_path)
19
- else
20
- redirect_to(build_hubrise_oauth_login_url)
21
- end
22
- end
23
-
24
- # authorize access to specific app_instance (expirable)
25
- def authorize_callback
26
- if current_hr_app_instance
27
- current_hr_user.assign_hr_app_instance(current_hr_app_instance)
28
- redirect_to(main_app.hubrise_open_path)
29
- else
30
- render(plain: "Something went wrong. Please try to reinstall the app")
31
- end
32
- end
3
+ include ActionLoginCallback
4
+ include ActionConnectCallback
5
+ include ActionAuthorizeCallback
33
6
 
34
7
  protected
35
8
 
36
9
  def current_hr_app_instance
37
- @hr_app_instance ||= HrAppInstance.where(hr_id: api_client_from_oauth_code.app_instance_id).take
10
+ @hr_app_instance ||= HubriseApp::Services::ResolveAppInstance.run(HrAppInstance, api_client_from_oauth_code.app_instance_id, self)
38
11
  end
39
12
 
40
13
  def api_client_from_oauth_code
41
- @api_client_from_oauth_code ||= HubriseGateway.build_api_client_from_authorization_code(params[:code])
14
+ @api_client ||= hubrise_gateway.build_api_client_from_authorization_code(params[:code])
42
15
  end
43
16
  end
44
17
  end
@@ -0,0 +1,13 @@
1
+ module HubriseApp::OauthController::ActionAuthorizeCallback
2
+ # authorize access to specific app_instance (expirable)
3
+ def authorize_callback
4
+ ensure_authenticated! do
5
+ if current_hr_app_instance
6
+ HubriseApp::Services::AssignAppInstance.run(current_hr_user, current_hr_app_instance, self)
7
+ redirect_to(build_hubrise_open_url)
8
+ else
9
+ render(plain: "Something went wrong. Please try to reinstall the app")
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module HubriseApp::OauthController::ActionConnectCallback
2
+ def connect_callback
3
+ @hr_app_instance = HubriseApp::Services::ConnectAppInstance.run(api_client_from_oauth_code, self)
4
+
5
+ if logged_in?
6
+ HubriseApp::Services::AssignAppInstance.run(current_hr_user, @hr_app_instance, self)
7
+ redirect_to(build_hubrise_open_url)
8
+ else
9
+ redirect_to(build_hubrise_oauth_login_url)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module HubriseApp::OauthController::ActionLoginCallback
2
+ def login_callback
3
+ hr_user = HubriseApp::Refresher::User.run(api_client_from_oauth_code)
4
+ login(hr_user)
5
+ redirect_to(build_hubrise_open_url)
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module HubriseApp
2
+ class SessionsController < ApplicationController
3
+ before_action :ensure_authenticated!
4
+
5
+ def destroy
6
+ logout
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class HubriseApp::ConfigLoader
2
+ def self.load
3
+ Rails.application.config_for("hubrise_app/config").deep_symbolize_keys
4
+ end
5
+ end
@@ -1,34 +1,46 @@
1
1
  class HubriseApp::HubriseGateway
2
2
  HUBRISE_LOGIN_SCOPE = "profile_with_email".freeze
3
- HUBRISE_API_VERSION = :v1
4
3
 
5
- class << self
6
- def build_api_client(params = {})
7
- HubriseClient::V1.new(
8
- HubriseApp::CONFIG[:hubrise_client_id],
9
- HubriseApp::CONFIG[:hubrise_client_secret],
10
- params.merge(
11
- oauth_host: HubriseApp::CONFIG[:hubrise_oauth_host],
12
- oauth_port: HubriseApp::CONFIG[:hubrise_oauth_port],
13
- api_host: HubriseApp::CONFIG[:hubrise_api_host],
14
- api_port: HubriseApp::CONFIG[:hubrise_api_port],
15
- use_https: HubriseApp::CONFIG[:hubrise_use_https]
16
- )
4
+ def initialize(config = HubriseApp::CONFIG)
5
+ @config = config
6
+ end
7
+
8
+ def build_api_client(params = {})
9
+ HubriseClient::V1.new(
10
+ @config[:hubrise_client_id],
11
+ @config[:hubrise_client_secret],
12
+ params.merge(
13
+ oauth_host: @config[:hubrise_oauth_host],
14
+ oauth_port: @config[:hubrise_oauth_port],
15
+ api_host: @config[:hubrise_api_host],
16
+ api_port: @config[:hubrise_api_port],
17
+ use_https: @config[:hubrise_use_https]
17
18
  )
18
- end
19
+ )
20
+ end
19
21
 
20
- def build_api_client_from_authorization_code(authorization_code)
21
- build_api_client.tap do |api_client|
22
- api_client.authorize!(authorization_code)
23
- end
24
- end
22
+ def build_api_client_from_app_instance(app_instance)
23
+ build_api_client(
24
+ access_token: app_instance.hr_access_token,
25
+ app_instance_id: app_instance.hr_id,
26
+ account_id: app_instance.hr_account_id,
27
+ location_id: app_instance.hr_location_id,
28
+ catalog_id: app_instance.hr_catalog_id,
29
+ customer_list_id: app_instance.hr_customer_list_id
30
+ )
31
+ end
25
32
 
26
- def build_login_authorization_url(redirect_uri)
27
- build_api_client.build_authorization_url(redirect_uri, HUBRISE_LOGIN_SCOPE)
33
+ def build_api_client_from_authorization_code(authorization_code)
34
+ build_api_client.tap do |api_client|
35
+ api_client.authorize!(authorization_code)
28
36
  end
37
+ end
29
38
 
30
- def build_app_authorization_url(hr_app_instance_id, redirect_uri)
31
- build_api_client.build_authorization_url(redirect_uri, nil, app_instance_id: hr_app_instance_id)
32
- end
39
+ def build_login_authorization_url(redirect_uri)
40
+ build_api_client.build_authorization_url(redirect_uri, HUBRISE_LOGIN_SCOPE)
41
+ end
42
+
43
+ def build_app_authorization_url(hr_app_instance_id, redirect_uri)
44
+ build_api_client.build_authorization_url(redirect_uri, nil, app_instance_id: hr_app_instance_id)
33
45
  end
34
46
  end
@@ -0,0 +1,23 @@
1
+ class HubriseApp::Refresher::Account
2
+ REFRESH_THRESHOLD = 1.day
3
+
4
+ class << self
5
+ def run(hr_account, api_client)
6
+ return unless stale?(hr_account)
7
+ raise if api_client.account_id != hr_account.hr_id
8
+
9
+ hr_account.update!(
10
+ refreshed_at: Time.now,
11
+ hr_api_data: if api_client.location_id
12
+ api_client.get_location(api_client.location_id).data["account"]
13
+ else
14
+ api_client.get_account(hr_account.hr_id).data
15
+ end.except("id")
16
+ )
17
+ end
18
+
19
+ def stale?(hr_account)
20
+ hr_account.refreshed_at.nil? || Time.now - hr_account.refreshed_at > REFRESH_THRESHOLD
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ class HubriseApp::Refresher::AppInstance
2
+ class << self
3
+ def run(hr_app_instance, api_client)
4
+ hr_app_instance.update!(
5
+ hr_account: build_hr_account(api_client),
6
+ hr_location: build_hr_location(api_client),
7
+ hr_access_token: api_client.access_token,
8
+ hr_catalog_id: api_client.catalog_id,
9
+ hr_customer_list_id: api_client.customer_list_id
10
+ )
11
+ end
12
+
13
+ protected
14
+
15
+ def build_hr_account(api_client)
16
+ if api_client.account_id
17
+ hr_account = HubriseApp::HrAccount.find_or_initialize_by(hr_id: api_client.account_id)
18
+ HubriseApp::Refresher::Account.run(hr_account, api_client)
19
+ hr_account
20
+ end
21
+ end
22
+
23
+ def build_hr_location(api_client)
24
+ if api_client.location_id
25
+ hr_location = HubriseApp::HrLocation.find_or_initialize_by(hr_id: api_client.location_id)
26
+ HubriseApp::Refresher::Location.run(hr_location, api_client)
27
+ hr_location
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ class HubriseApp::Refresher::Location
2
+ REFRESH_THRESHOLD = 1.day
3
+
4
+ class << self
5
+ def run(hr_location, api_client)
6
+ return unless stale?(hr_location)
7
+
8
+ hr_location.update!(
9
+ refreshed_at: Time.now,
10
+ hr_api_data: api_client.get_location(hr_location.hr_id).data.except("id")
11
+ )
12
+ end
13
+
14
+ def stale?(hr_location)
15
+ hr_location.refreshed_at.nil? || Time.now - hr_location.refreshed_at > REFRESH_THRESHOLD
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ class HubriseApp::Refresher::User
2
+ REFRESH_THRESHOLD = 1.day
3
+
4
+ class << self
5
+ def run(api_client)
6
+ api_data = api_client.get_user.data
7
+ HubriseApp::HrUser.find_or_initialize_by(hr_id: api_data.delete("id")).tap do |hr_user|
8
+ hr_user.update!(
9
+ refreshed_at: Time.now,
10
+ hr_api_data: api_data,
11
+ hr_access_token: api_client.access_token
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ class HubriseApp::Services::AssignAppInstance
2
+ def self.run(hr_user, hr_app_instance, _ctx)
3
+ HubriseApp::HrUserAppInstance.find_or_initialize_by(
4
+ hr_app_instance_id: hr_app_instance.hr_id,
5
+ hr_user_id: hr_user.hr_id
6
+ ).update!(refreshed_at: Time.now)
7
+ end
8
+ end
@@ -1,6 +1,7 @@
1
1
  class HubriseApp::Services::ConnectAppInstance
2
- prepend HubriseApp::Services::Override::ConnectAppInstance
3
-
4
- class << self; delegate :run, to: :new; end
5
- def run(hr_app_instance, hubrise_callback_event_url:); end
2
+ def self.run(api_client, _ctx)
3
+ hr_app_instance = HubriseApp::HrAppInstance.find_or_initialize_by(hr_id: api_client.app_instance_id)
4
+ HubriseApp::Refresher::AppInstance.run(hr_app_instance, api_client)
5
+ hr_app_instance
6
+ end
6
7
  end
@@ -0,0 +1,5 @@
1
+ class HubriseApp::Services::ResolveAppInstance
2
+ def self.run(scope, id, _ctx)
3
+ scope.where(hr_id: id).take
4
+ end
5
+ end
@@ -1,19 +1,5 @@
1
1
  class HubriseApp::HrAccount < HubriseApp::ApplicationRecord
2
2
  self.table_name = :hr_accounts
3
3
 
4
- include HubriseApp::HrApiResource
5
- class << self
6
- def fetch_hr_attrs(api_client, hr_id)
7
- raise if api_client.account_id != hr_id
8
-
9
- {
10
- hr_id: hr_id,
11
- hr_api_data: if api_client.location_id
12
- api_client.get_location(api_client.location_id).data["account"]
13
- else
14
- api_client.get_account(hr_id).data
15
- end.except("id")
16
- }
17
- end
18
- end
4
+ store_accessor :hr_api_data, :name
19
5
  end
@@ -4,32 +4,5 @@ module HubriseApp
4
4
 
5
5
  belongs_to :hr_account, optional: true, primary_key: :hr_id
6
6
  belongs_to :hr_location, optional: true, primary_key: :hr_id
7
-
8
- def api_client
9
- HubriseApp::HubriseGateway.build_api_client(
10
- access_token: hr_access_token,
11
- app_instance_id: hr_id,
12
- account_id: hr_account_id,
13
- location_id: hr_location_id,
14
- catalog_id: hr_catalog_id,
15
- customer_list_id: hr_customer_list_id
16
- )
17
- end
18
-
19
- def self.refresh_or_create_via_api_client(api_client)
20
- hr_app_instance = find_or_initialize_by(hr_id: api_client.app_instance_id)
21
- hr_account = api_client.account_id && HrAccount.refresh_or_create_via_api_client(api_client, api_client.account_id) # rubocop:disable Metrics/LineLength
22
- hr_location = api_client.location_id && HrLocation.refresh_or_create_via_api_client(api_client, api_client.location_id) # rubocop:disable Metrics/LineLength
23
-
24
- hr_app_instance.update!(
25
- hr_account: hr_account,
26
- hr_location: hr_location,
27
- hr_access_token: api_client.access_token,
28
- hr_catalog_id: api_client.catalog_id,
29
- hr_customer_list_id: api_client.customer_list_id
30
- )
31
-
32
- hr_app_instance
33
- end
34
7
  end
35
8
  end
@@ -1,11 +1,5 @@
1
1
  class HubriseApp::HrLocation < HubriseApp::ApplicationRecord
2
2
  self.table_name = :hr_locations
3
3
 
4
- include HubriseApp::HrApiResource
5
- def self.fetch_hr_attrs(api_client, hr_id)
6
- {
7
- hr_id: hr_id,
8
- hr_api_data: api_client.get_location(hr_id).data.except("id")
9
- }
10
- end
4
+ store_accessor :hr_api_data, :name
11
5
  end
@@ -1,35 +1,17 @@
1
1
  module HubriseApp
2
2
  class HrUser < HubriseApp::ApplicationRecord
3
- include HubriseApp::Override::HrUser
4
-
5
3
  self.table_name = :hr_users
6
4
 
7
5
  has_many :hr_user_app_instances, -> { fresh }, primary_key: :hr_id
8
6
  has_many :hr_app_instances, through: :hr_user_app_instances
9
7
 
10
- include HubriseApp::HrApiResource
11
- class << self
12
- def fetch_hr_attrs(api_client)
13
- data = api_client.get_user.data
14
- {
15
- hr_id: data.delete("id"),
16
- hr_api_data: data,
17
- hr_access_token: api_client.access_token
18
- }
19
- end
20
-
21
- def refresh_or_create_via_api_client(api_client)
22
- hr_attrs = fetch_hr_attrs(api_client)
23
- hr_user = find_or_initialize_by(hr_id: hr_attrs[:hr_id])
24
- hr_user.refresh_with(hr_attrs)
25
- hr_user
26
- end
27
- end
8
+ store_accessor :hr_api_data, :first_name,
9
+ :last_name,
10
+ :email,
11
+ :locales
28
12
 
29
- def assign_hr_app_instance(hr_app_instance)
30
- hr_user_app_instance = HrUserAppInstance.find_or_initialize_by(hr_app_instance_id: hr_app_instance.hr_id,
31
- hr_user_id: hr_id)
32
- hr_user_app_instance.refresh!
13
+ def primary_locale
14
+ locales&.first
33
15
  end
34
16
  end
35
17
  end
@@ -7,8 +7,4 @@ class HubriseApp::HrUserAppInstance < HubriseApp::ApplicationRecord
7
7
  def self.fresh(time: Time.now)
8
8
  where("hr_user_app_instances.refreshed_at > ?", time - REFRESH_THRESHOLD)
9
9
  end
10
-
11
- def refresh!(time: Time.now)
12
- update!(refreshed_at: time)
13
- end
14
10
  end
@@ -1 +1 @@
1
- HubriseApp::CONFIG = Rails.application.config_for("hubrise_app/config").deep_symbolize_keys
1
+ HubriseApp::CONFIG = HubriseApp::ConfigLoader.load
@@ -9,4 +9,6 @@ HubriseApp::Engine.routes.draw do
9
9
  post :event
10
10
  get :disconnect
11
11
  end
12
+
13
+ resource :sessions, only: :destroy
12
14
  end
@@ -0,0 +1,3 @@
1
+ class HubriseApp::SpecSupport
2
+ FACTORIES_PATH = File.expand_path("../../spec/factories", __dir__)
3
+ end
@@ -1,3 +1,3 @@
1
1
  module HubriseApp
2
- VERSION = "0.1.2".freeze
2
+ VERSION = "1.0.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hubrise_app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antoine Monnier
@@ -9,34 +9,34 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-05-31 00:00:00.000000000 Z
12
+ date: 2020-02-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hubrise_client
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
- version: 2.0.0
20
+ version: 2.0.2
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - "~>"
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
- version: 2.0.0
27
+ version: 2.0.2
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rails
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">"
33
33
  - !ruby/object:Gem::Version
34
34
  version: 5.2.2
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ">"
40
40
  - !ruby/object:Gem::Version
41
41
  version: 5.2.2
42
42
  description:
@@ -45,29 +45,38 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - MIT-LICENSE
48
+ - LICENSE
49
49
  - README.md
50
50
  - Rakefile
51
51
  - app/assets/config/hubrise_app_manifest.js
52
52
  - app/assets/stylesheets/hubrise_app/application.css
53
53
  - app/controllers/hubrise_app/application_controller.rb
54
+ - app/controllers/hubrise_app/application_controller/app_instance_methods.rb
55
+ - app/controllers/hubrise_app/application_controller/hubrise_gateway_methods.rb
56
+ - app/controllers/hubrise_app/application_controller/session_methods.rb
54
57
  - app/controllers/hubrise_app/callback_controller.rb
58
+ - app/controllers/hubrise_app/callback_controller/action_disconnect.rb
59
+ - app/controllers/hubrise_app/callback_controller/action_event.rb
55
60
  - app/controllers/hubrise_app/oauth_controller.rb
61
+ - app/controllers/hubrise_app/oauth_controller/action_authorize_callback.rb
62
+ - app/controllers/hubrise_app/oauth_controller/action_connect_callback.rb
63
+ - app/controllers/hubrise_app/oauth_controller/action_login_callback.rb
64
+ - app/controllers/hubrise_app/sessions_controller.rb
65
+ - app/lib/hubrise_app/config_loader.rb
56
66
  - app/lib/hubrise_app/hubrise_gateway.rb
67
+ - app/lib/hubrise_app/refresher/account.rb
68
+ - app/lib/hubrise_app/refresher/app_instance.rb
69
+ - app/lib/hubrise_app/refresher/location.rb
70
+ - app/lib/hubrise_app/refresher/user.rb
71
+ - app/lib/hubrise_app/services/assign_app_instance.rb
57
72
  - app/lib/hubrise_app/services/connect_app_instance.rb
58
- - app/lib/hubrise_app/services/disconnect_app_instance.rb
59
- - app/lib/hubrise_app/services/handle_event.rb
60
- - app/lib/hubrise_app/services/override/connect_app_instance.rb
61
- - app/lib/hubrise_app/services/override/disconnect_app_instance.rb
62
- - app/lib/hubrise_app/services/override/handle_event.rb
63
- - app/models/concerns/hubrise_app/hr_api_resource.rb
73
+ - app/lib/hubrise_app/services/resolve_app_instance.rb
64
74
  - app/models/hubrise_app/application_record.rb
65
75
  - app/models/hubrise_app/hr_account.rb
66
76
  - app/models/hubrise_app/hr_app_instance.rb
67
77
  - app/models/hubrise_app/hr_location.rb
68
78
  - app/models/hubrise_app/hr_user.rb
69
79
  - app/models/hubrise_app/hr_user_app_instance.rb
70
- - app/models/hubrise_app/override/hr_user.rb
71
80
  - app/views/hubrise_app/application/root.html.haml
72
81
  - app/views/layouts/hubrise_app/application.html.erb
73
82
  - config/initializers/hubrise_app_config.rb
@@ -75,6 +84,7 @@ files:
75
84
  - db/migrate/20190116155419_create_base_tables.rb
76
85
  - lib/hubrise_app.rb
77
86
  - lib/hubrise_app/engine.rb
87
+ - lib/hubrise_app/spec_support.rb
78
88
  - lib/hubrise_app/version.rb
79
89
  homepage: https://github.com/HubRise/ruby-app
80
90
  licenses:
@@ -96,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
106
  version: '0'
97
107
  requirements: []
98
108
  rubyforge_project:
99
- rubygems_version: 2.7.6
109
+ rubygems_version: 2.7.6.2
100
110
  signing_key:
101
111
  specification_version: 4
102
112
  summary: Rails engine to bootstrap a HubRise-based application
@@ -1,20 +0,0 @@
1
- Copyright 2019 nsave
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,6 +0,0 @@
1
- class HubriseApp::Services::DisconnectAppInstance
2
- prepend HubriseApp::Services::Override::DisconnectAppInstance
3
-
4
- class << self; delegate :run, to: :new; end
5
- def run(hr_app_instance); end
6
- end
@@ -1,22 +0,0 @@
1
- class HubriseApp::Services::HandleEvent
2
- prepend HubriseApp::Services::Override::HandleEvent
3
-
4
- class << self; delegate :run, to: :new; end
5
-
6
- def run(hr_app_instance, params)
7
- case params["resource_type"]
8
- when "customer"
9
- handle_customer_event(hr_app_instance, params)
10
- when "order"
11
- handle_order_event(hr_app_instance, params)
12
- else
13
- raise "Cannot handle hubrise event with type: " + params["resource_type"].to_s
14
- end
15
- end
16
-
17
- protected
18
-
19
- def handle_customer_event(hr_app_instance, params); end
20
-
21
- def handle_order_event(hr_app_instance, params); end
22
- end
@@ -1 +0,0 @@
1
- module HubriseApp::Services::Override::ConnectAppInstance; end
@@ -1 +0,0 @@
1
- module HubriseApp::Services::Override::DisconnectAppInstance; end
@@ -1 +0,0 @@
1
- module HubriseApp::Services::Override::HandleEvent; end
@@ -1,29 +0,0 @@
1
- module HubriseApp::HrApiResource
2
- REFRESH_THRESHOLD = 1.day
3
-
4
- def stale?
5
- refreshed_at.nil? || Time.now - refreshed_at > REFRESH_THRESHOLD
6
- end
7
-
8
- def refresh_via_api_client(api_client)
9
- return unless stale?
10
-
11
- refresh_with(self.class.fetch_hr_attrs(api_client, hr_id))
12
- end
13
-
14
- def refresh_with(attrs)
15
- update!(attrs.merge(refreshed_at: Time.now))
16
- end
17
-
18
- def self.included(base)
19
- base.extend(ClassMethods)
20
- end
21
-
22
- module ClassMethods
23
- def refresh_or_create_via_api_client(api_client, hr_id)
24
- hr_record = find_or_initialize_by(hr_id: hr_id)
25
- hr_record.refresh_via_api_client(api_client)
26
- hr_record
27
- end
28
- end
29
- end
@@ -1 +0,0 @@
1
- module HubriseApp::Override::HrUser; end