beyond_canvas 0.16.2.pre → 0.19.2.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/config/beyond_canvas_manifest.js +1 -0
  3. data/app/assets/images/icons/arrow_right.svg +1 -0
  4. data/app/assets/images/icons/close.svg +1 -0
  5. data/app/assets/images/icons/home.svg +1 -0
  6. data/app/assets/javascripts/beyond_canvas/base.js +120 -246
  7. data/app/assets/stylesheets/beyond_canvas/base.scss +10 -1
  8. data/app/assets/stylesheets/beyond_canvas/components/_action_bar.scss +28 -0
  9. data/app/assets/stylesheets/beyond_canvas/components/_breadcrumbs.scss +29 -0
  10. data/app/assets/stylesheets/beyond_canvas/components/_buttons.scss +1 -1
  11. data/app/assets/stylesheets/beyond_canvas/components/_debug.scss +10 -0
  12. data/app/assets/stylesheets/beyond_canvas/components/_forms.scss +11 -14
  13. data/app/assets/stylesheets/beyond_canvas/components/_grids.scss +12 -0
  14. data/app/assets/stylesheets/beyond_canvas/components/_inputs.scss +5 -0
  15. data/app/assets/stylesheets/beyond_canvas/components/_main.scss +12 -13
  16. data/app/assets/stylesheets/beyond_canvas/components/_margins.scss +12 -0
  17. data/app/assets/stylesheets/beyond_canvas/components/_menu.scss +50 -0
  18. data/app/assets/stylesheets/beyond_canvas/components/_modals.scss +35 -0
  19. data/app/assets/stylesheets/beyond_canvas/components/_scrollbox.scss +35 -0
  20. data/app/assets/stylesheets/beyond_canvas/components/_sidebar.scss +53 -0
  21. data/app/assets/stylesheets/beyond_canvas/components/_texts.scss +8 -0
  22. data/app/assets/stylesheets/beyond_canvas/components/_titles.scss +7 -0
  23. data/app/assets/stylesheets/beyond_canvas/settings/_variables.scss +64 -9
  24. data/app/controllers/beyond_canvas/application_controller.rb +3 -0
  25. data/app/controllers/beyond_canvas/authentications_controller.rb +37 -30
  26. data/app/controllers/concerns/beyond_canvas/authentication.rb +1 -12
  27. data/app/controllers/concerns/beyond_canvas/locale_management.rb +5 -4
  28. data/app/form_builders/beyond_canvas/form_builder.rb +8 -6
  29. data/app/helpers/beyond_canvas/authentications_helper.rb +28 -0
  30. data/app/helpers/beyond_canvas/cockpit_app_helper.rb +17 -0
  31. data/app/helpers/beyond_canvas/controller_helper.rb +9 -0
  32. data/app/helpers/beyond_canvas/debug_helper.rb +9 -0
  33. data/app/helpers/beyond_canvas/locale_switch_helper.rb +5 -1
  34. data/app/javascript/beyond_canvas/base.js +3 -0
  35. data/app/javascript/beyond_canvas/initializers/buttons.js +65 -19
  36. data/app/javascript/beyond_canvas/initializers/flash.js +9 -2
  37. data/app/javascript/beyond_canvas/initializers/inputs.js +4 -1
  38. data/app/javascript/beyond_canvas/initializers/modals.js +14 -0
  39. data/app/views/beyond_canvas/authentications/new.html.erb +19 -10
  40. data/app/views/beyond_canvas/shared/_action_bar.html.erb +15 -0
  41. data/app/views/beyond_canvas/shared/_breadcrumbs.html.erb +14 -0
  42. data/app/views/beyond_canvas/shared/_flash.html.erb +22 -12
  43. data/app/views/beyond_canvas/shared/_locales.html.erb +8 -0
  44. data/app/views/beyond_canvas/shared/_menu.html.erb +31 -0
  45. data/app/views/beyond_canvas/shared/_modal.html.erb +6 -0
  46. data/app/views/beyond_canvas/shared/_sidebar.html.erb +16 -0
  47. data/app/views/layouts/beyond_canvas/application.html.erb +31 -0
  48. data/app/views/layouts/beyond_canvas/public.html.erb +11 -4
  49. data/config/locales/en.yml +5 -0
  50. data/config/routes.rb +5 -4
  51. data/lib/beyond_canvas.rb +12 -12
  52. data/lib/beyond_canvas/configuration.rb +11 -6
  53. data/lib/beyond_canvas/engine.rb +5 -6
  54. data/lib/beyond_canvas/menu_item_registration.rb +19 -0
  55. data/lib/beyond_canvas/parameter_sanitizer.rb +1 -1
  56. data/lib/beyond_canvas/rails/routes.rb +8 -7
  57. data/lib/beyond_canvas/version.rb +1 -1
  58. data/lib/generators/beyond_canvas/controller/controller_generator.rb +1 -6
  59. data/lib/generators/beyond_canvas/controller/templates/shops_controller.rb +20 -0
  60. data/lib/generators/beyond_canvas/custom_menu/custom_menu_generator.rb +13 -0
  61. data/lib/generators/beyond_canvas/custom_menu/templates/beyond_canvas_custom_menu.html.erb +32 -0
  62. data/lib/generators/beyond_canvas/custom_styles/custom_styles_generator.rb +1 -1
  63. data/lib/generators/beyond_canvas/custom_styles/templates/beyond_canvas_custom_styles.scss +55 -9
  64. data/lib/generators/beyond_canvas/install/install_generator.rb +3 -5
  65. data/lib/generators/beyond_canvas/install/templates/beyond_canvas.rb.erb +52 -14
  66. data/lib/generators/beyond_canvas/{auth_model/auth_model_generator.rb → model/model_generator.rb} +4 -5
  67. data/lib/generators/beyond_canvas/{auth_model → model}/templates/migration.erb +2 -4
  68. data/lib/generators/beyond_canvas/{auth_model → model}/templates/model.erb +0 -0
  69. data/lib/generators/beyond_canvas/views/views_generator.rb +4 -6
  70. data/lib/models/concerns/authentication.rb +57 -0
  71. data/lib/models/concerns/utils.rb +79 -0
  72. data/lib/models/shop.rb +12 -0
  73. metadata +73 -31
  74. data/app/controllers/concerns/beyond_canvas/resource_management.rb +0 -33
  75. data/app/javascript/beyond_canvas/initializers/functions.js +0 -41
  76. data/app/views/beyond_canvas/locales/_edit.html.erb +0 -8
  77. data/lib/beyond_canvas/models/authentication.rb +0 -66
  78. data/lib/beyond_canvas/models/shop.rb +0 -28
  79. data/lib/beyond_canvas/models/utils.rb +0 -55
  80. data/lib/generators/beyond_canvas/controller/templates/controller.erb +0 -37
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ShopsController < BeyondCanvas::AuthenticationsController
4
+ # before_action :configure_params, only: [:install]
5
+
6
+ # def new
7
+ # super
8
+ # end
9
+
10
+ # def install
11
+ # super
12
+ # end
13
+
14
+ # private
15
+
16
+ # If you have extra params to permit, append them to the sanitizer.
17
+ # def configure_params
18
+ # beyond_canvas_parameter_sanitizer.permit(:attribute1, :attribute2)
19
+ # end
20
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BeyondCanvas
4
+ module Generators
5
+ class CustomMenuGenerator < Rails::Generators::Base # :nodoc:
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def copy_initializer
9
+ copy_file 'beyond_canvas_custom_menu.html.erb', 'app/views/beyond_canvas/shared/_menu.html.erb'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ <div class="menu">
2
+
3
+ <div class="menu--left">
4
+ <%= render 'beyond_canvas/shared/logo' %>
5
+ </div>
6
+
7
+ <div class="menu--center">
8
+
9
+ <%
10
+ =begin %>
11
+ Add here your menu links. You can use `menu__item` as class name and add
12
+ `menu__item--selected` as secondary class for selected menu item. E.g:
13
+
14
+ <%= link_to 'About us', about_us_path, class: "menu__item #{ 'menu__item--selected' if params[:controller] == 'about_us' }" %>
15
+ <%= link_to 'Support', support_path, class: "menu__item #{ 'menu__item--selected' if params[:controller] == 'support' }" %>
16
+ <%
17
+ =end %>
18
+
19
+ </div>
20
+
21
+ <div class="menu--right">
22
+
23
+ <%
24
+ =begin %>
25
+ You can also add here some content that will be right-aligned, like a user
26
+ avatar image or a dropdown account menu.
27
+ <%
28
+ =end %>
29
+
30
+ </div>
31
+
32
+ </div>
@@ -6,7 +6,7 @@ module BeyondCanvas
6
6
  source_root File.expand_path('templates', __dir__)
7
7
 
8
8
  def copy_initializer
9
- template 'beyond_canvas_custom_styles.sass', 'app/assets/stylesheets/_beyond_canvas_custom_styles.sass'
9
+ template 'beyond_canvas_custom_styles.scss', 'app/assets/stylesheets/_beyond_canvas_custom_styles.scss'
10
10
  end
11
11
  end
12
12
  end
@@ -11,7 +11,8 @@
11
11
  // General styles
12
12
  // ************************************************************
13
13
 
14
- // $main-background: rgb(233, 232, 220);
14
+ // $main-background-public: rgb(233, 232, 220);
15
+ // $main-background-application: rgb(233, 232, 220);
15
16
 
16
17
  // ************************************************************
17
18
  // Typography
@@ -81,19 +82,19 @@
81
82
  // Checkboxes
82
83
  // ************************************************************
83
84
 
84
- // $checkbox-checked-color: #97C344;
85
- // $checkbox-checked-background: #ffffff;
86
- // $checkbox-unchecked-color: #C2BF9D;
87
- // $checkbox-unchecked-background: #ffffff;
85
+ // $checkbox-checked-color: #97C344; // This value MUST be HEX
86
+ // $checkbox-checked-background: #ffffff; // This value MUST be HEX
87
+ // $checkbox-unchecked-color: #C2BF9D; // This value MUST be HEX
88
+ // $checkbox-unchecked-background: #ffffff; // This value MUST be HEX
88
89
 
89
90
  // ************************************************************
90
91
  // Radiobuttons
91
92
  // ************************************************************
92
93
 
93
- // $radio-checked-color: #97C344;
94
- // $radio-checked-background: #ffffff;
95
- // $radio-unchecked-color: #C2BF9D;
96
- // $radio-unchecked-background: #ffffff;
94
+ // $radio-checked-color: #97C344; // This value MUST be HEX
95
+ // $radio-checked-background: #ffffff; // This value MUST be HEX
96
+ // $radio-unchecked-color: #C2BF9D; // This value MUST be HEX
97
+ // $radio-unchecked-background: #ffffff; // This value MUST be HEX
97
98
 
98
99
  // ************************************************************
99
100
  // Hints
@@ -147,7 +148,52 @@
147
148
  // $notice-border-radius: 4px;
148
149
  // $notice-color: rgb(153, 153, 153);
149
150
 
151
+ // ************************************************************
152
+ // Breadcrums
153
+ // ************************************************************
154
+
155
+ // $breadcrum-color: rgb(122, 118, 76);
156
+ // $breadcrum-color-current: rgb(61, 149, 137);
157
+
158
+ // ************************************************************
159
+ // Titles
160
+ // ************************************************************
161
+
162
+ // $title-color: rgb(122, 118, 76);
163
+
164
+ // ************************************************************
165
+ // Menu
166
+ // ************************************************************
167
+
168
+ // $menu-background: #1c3445;
169
+ // $menu-height: 60px;
170
+ // $menu-logo-height: 30px;
171
+ // $menu-item-color: #9ab5c6;
172
+ // $menu-item-hover-color: lighten($menu-item-color, 13%);
173
+ // $menu-item-selected-color: #4eb7a8;
174
+ // $menu-item-selected-hover-color: lighten($menu-item-selected-color, 13%);
175
+
176
+ // ************************************************************
177
+ // Action bar
178
+ // ************************************************************
179
+
180
+ // $action-bar-background: rgb(255, 255, 255);
181
+ // $action-bar-shadow: 1px solid rgba(0, 0, 0, 0.2);
182
+ // $action-bar-height: 73px;
183
+
150
184
  // ************************************************************
151
185
  // Markdown
152
186
  // ************************************************************
153
187
 
188
+
189
+ // ************************************************************
190
+ // Sidebar
191
+ // ************************************************************
192
+
193
+
194
+ // ************************************************************
195
+ // Modals
196
+ // ************************************************************
197
+
198
+ // $modal-close-icon-color: #8b8b8b;
199
+ // $modal-background-color: rgba(0, 0, 0, 0.5);
@@ -6,13 +6,11 @@ module BeyondCanvas
6
6
  desc 'Installs Beyond Canvas and generates the necessary files'
7
7
 
8
8
  class_option :skip_webpacker, type: :boolean, default: false, desc: 'Use Sprockets assets instead of Webpacker'
9
- class_option :auth_model, type: :string, default: 'shop', desc: 'Authentication model'
10
9
 
11
10
  source_root File.expand_path('templates', __dir__)
12
11
 
13
12
  def copy_initializer
14
13
  @skip_webpacker = options[:skip_webpacker]
15
- @auth_model = options[:auth_model]
16
14
 
17
15
  template 'beyond_canvas.rb.erb', 'config/initializers/beyond_canvas.rb'
18
16
  end
@@ -29,12 +27,12 @@ module BeyondCanvas
29
27
  generate 'beyond_canvas:beyond_api'
30
28
  end
31
29
 
32
- def generate_auth_model
33
- generate "beyond_canvas:auth_model #{@auth_model}"
30
+ def generate_model
31
+ generate 'beyond_canvas:model shop'
34
32
  end
35
33
 
36
34
  def setup_routes
37
- route "beyond_canvas_for :#{@auth_model.pluralize}"
35
+ route 'beyond_canvas_routes'
38
36
  end
39
37
 
40
38
  def copy_locale
@@ -1,9 +1,41 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  BeyondCanvas.setup do |config|
4
+ # Carefully read and fill this configuration file.
5
+ #
6
+ # All uncommented configuration parameters are necessary for proper operation
7
+ # of Beyond Canvas.
8
+
9
+ # ==> Engine configuration
10
+
11
+ # Set the namespace where the Engine is going to be mounted.
12
+ #
13
+ # config.namespace = '/'
14
+
15
+ # Set the url or path where a shop is going to be redirected after a
16
+ # succesful authentication.
17
+ #
18
+ config.open_app_url = nil
19
+
20
+ # Set if your app ig going to run as a cockpit app. If you are not sure about
21
+ # this, please contact apps@epages.com.
22
+ #
23
+ # config.cockpit_app = false
24
+
25
+ # Set if your app is going to be preinstalled on packages. If you are not sure
26
+ # about this, please contact apps@epages.com.
27
+ #
28
+ # config.preinstalled = false
29
+
30
+ # Set if you want to enable the available debug mode Beyond Canvas offers.
31
+ # This options displays a parameter debug as well as some custom Beyond Canvas
32
+ # logs.
33
+ #
34
+ # config.debug_mode = false
35
+
4
36
  # ==> Site configuration
5
37
 
6
- # Set the title that is displayed on the main layout..
38
+ # Set the title that is displayed on the main layout.
7
39
  #
8
40
  config.site_title = '<%= Rails.application.class.name.split("::").first.humanize %>'
9
41
 
@@ -17,35 +49,41 @@ BeyondCanvas.setup do |config|
17
49
  #
18
50
  # config.favicon = 'favicon.ico'
19
51
 
52
+ # Set the menu items that will be displayed on the top of your app.
53
+ # It accepts 3 parameters:
54
+ # 1. name: the text displayed on the link tag. Supports any string or an I18n compatible path.
55
+ # 2. url: the href of the link tag. Supports any string or a symbolized route.
56
+ # 3. options: any html attributes that will be added to the link tag.
57
+ # You can also run `rails g beyond_canvas:custom_menu` to generate a menu template you can override.
58
+ # See examples:
59
+ #
60
+ # config.register_menu_item 'Contact', '/contact/, class: 'contact'
61
+ # config.register_menu_item 'menu_item.about_us', :root_path, class: 'about-us', id: 'about-us'
62
+ # config.register_menu_item 'Our website', 'https://epages.com', target: :_blank
63
+
20
64
  # ==> Stylesheets & Javascripts
21
65
 
22
66
  # We recommend using the built in Beyond Canvas layout and loading up your own
23
67
  # stylesheets / javascripts to customize the look and feel.
24
68
  #
25
69
  # To load a stylesheet:
26
- # config.register_stylesheet 'my_stylesheet.css'
70
+ # config.register_stylesheet 'my_stylesheet.css'
27
71
  #
28
- # You can provide an options hash for more control, which is passed along to stylesheet_link_tag():
29
- # config.register_stylesheet 'my_print_stylesheet.css', media: :print
72
+ # You can provide an options hash for more control, which is passed along
73
+ # to stylesheet_link_tag():
74
+ # config.register_stylesheet 'my_print_stylesheet.css', media: :print
30
75
  #
31
76
  # To load a javascript file:
32
- # config.register_javascript 'my_javascript.js'
77
+ # config.register_javascript 'my_javascript.js'
33
78
 
34
79
  # ==> Webpacker
35
80
 
36
- # By default, Active Admin uses Webpacker.
81
+ # By default, Beyond Canvas uses Webpacker.
37
82
  # You can switch to using Sprocket's asset pipeline here.
38
83
  #
39
84
  <% unless @skip_webpacker %># <% end %>config.skip_webpacker = <%= @skip_webpacker %>
40
85
 
41
- # ==> Authentication with ePages Beyond API
86
+ # ==> Database encryption
42
87
 
43
- # Default model is shop
44
- #
45
88
  config.encryption_key = '<%= SecureRandom.hex(32) %>'
46
- config.blind_index_key = '<%= SecureRandom.hex(32) %>'
47
-
48
- # ==> Mountable engine configuration
49
- #
50
- # config.namespace = '/'
51
89
  end
@@ -4,7 +4,7 @@ require 'rails/generators/active_record'
4
4
 
5
5
  module BeyondCanvas
6
6
  module Generators
7
- class AuthModelGenerator < ActiveRecord::Generators::Base # :nodoc:
7
+ class ModelGenerator < ActiveRecord::Generators::Base # :nodoc:
8
8
  desc 'Generates a model with the given name and provides a method to authenticate in Beyond Backend'
9
9
 
10
10
  argument :attributes, type: :array, default: [], banner: 'field:type field:type'
@@ -19,7 +19,7 @@ module BeyondCanvas
19
19
  end
20
20
 
21
21
  def generate_model
22
- template 'model.erb', File.join('app', 'models', "#{file_path}.rb")
22
+ template 'model.erb', File.join("app/models/#{file_path}.rb")
23
23
  end
24
24
 
25
25
  private
@@ -34,15 +34,14 @@ module BeyondCanvas
34
34
 
35
35
  def migration_data
36
36
  <<RUBY
37
- t.string :encrypted_beyond_api_url, null: false
38
- t.string :encrypted_beyond_api_url_iv, null: false
39
- t.string :beyond_api_url_bidx, null: false
37
+ t.string :beyond_api_url, null: false
40
38
 
41
39
  t.text :encrypted_beyond_access_token, null: true
42
40
  t.text :encrypted_beyond_access_token_iv, null: true
43
41
 
44
42
  t.text :encrypted_beyond_refresh_token, null: true
45
43
  t.text :encrypted_beyond_refresh_token_iv, null: true
44
+
46
45
  RUBY
47
46
  end
48
47
  end
@@ -4,16 +4,14 @@ class BeyondCanvasCreate<%= table_name.camelize %> < ActiveRecord::Migration<%=
4
4
  def change
5
5
  create_table :<%= table_name %><%= primary_key_type %> do |t|
6
6
  <%= migration_data -%>
7
-
8
7
  <% attributes.each do |attribute| -%>
9
8
  t.<%= attribute.type %> :<%= attribute.name %>
10
- <% end -%>
11
9
 
10
+ <% end -%>
12
11
  t.timestamps null: false
13
12
  end
14
13
 
15
- add_index :<%= table_name %>, :encrypted_beyond_api_url_iv, unique: true
16
- add_index :<%= table_name %>, :beyond_api_url_bidx, unique: true
14
+ add_index :<%= table_name %>, :beyond_api_url, unique: true
17
15
  add_index :<%= table_name %>, :encrypted_beyond_access_token_iv, unique: true
18
16
  add_index :<%= table_name %>, :encrypted_beyond_refresh_token_iv, unique: true
19
17
  end
@@ -5,14 +5,12 @@ require 'rails/generators/active_record'
5
5
  module BeyondCanvas
6
6
  module Generators
7
7
  class ViewsGenerator < Rails::Generators::Base # :nodoc:
8
- desc 'Creates a view in the app/view folder'
8
+ desc 'Creates all Beyond Canvas views to overwrite them'
9
9
 
10
- argument :scope, required: true, desc: 'The scope to copy views to'
10
+ source_root File.expand_path('../../../../app/views/beyond_canvas', __dir__)
11
11
 
12
- source_root File.expand_path('../../../../app/views/beyond_canvas/authentications', __dir__)
13
-
14
- def create_view
15
- copy_file 'new.html.erb', "app/views/#{scope}/new.html.erb"
12
+ def copy_views
13
+ directory 'authentications', 'app/views/beyond_canvas/authentications'
16
14
  end
17
15
  end
18
16
  end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BeyondCanvas
4
+ module Models
5
+ module Concerns
6
+ module Authentication # :nodoc:
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ attr_accessor :code, :signature, :access_token_url, :api_url, :return_url, :terms
11
+
12
+ ##############################################################################
13
+ # Encrypted attribute configuration
14
+ ##############################################################################
15
+
16
+ attr_encrypted :beyond_access_token, key: [BeyondCanvas.configuration.encryption_key].pack('H*')
17
+ attr_encrypted :beyond_refresh_token, key: [BeyondCanvas.configuration.encryption_key].pack('H*')
18
+
19
+ ##############################################################################
20
+ # Validations
21
+ ##############################################################################
22
+
23
+ # Database fields
24
+
25
+ validates :beyond_api_url,
26
+ presence: true
27
+ validates :beyond_access_token,
28
+ presence: true,
29
+ unless: -> { encrypted_beyond_access_token_was.blank? }
30
+ validates :beyond_refresh_token,
31
+ presence: true,
32
+ unless: -> { encrypted_beyond_refresh_token_was.blank? }
33
+
34
+ ##############################################################################
35
+ # Instance methods
36
+ ##############################################################################
37
+
38
+ #
39
+ # Get and save access_token and refresh_token using the authentication code
40
+ # NOTE: This method is used during the shop creation, as it is the only point
41
+ # we know about the authentication code
42
+ #
43
+ def authenticate(params_code)
44
+ session = BeyondApi::Session.new(api_url: beyond_api_url)
45
+ session.token.create(params_code)
46
+ update(beyond_access_token: session.access_token,
47
+ beyond_refresh_token: session.refresh_token)
48
+ end
49
+
50
+ def authenticated?
51
+ beyond_access_token.present? && beyond_refresh_token.present?
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jwt'
4
+
5
+ module BeyondCanvas
6
+ module Models
7
+ module Concerns
8
+ module Utils # :nodoc:
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ ##############################################################################
13
+ # Instance methods
14
+ ##############################################################################
15
+
16
+ #
17
+ # Generates a new access_token and refresh_token
18
+ #
19
+ def refresh_token
20
+ beyond_session = BeyondApi::Session.new(api_url: beyond_api_url, refresh_token: beyond_refresh_token)
21
+ beyond_session.token.refresh
22
+
23
+ update(beyond_access_token: beyond_session.access_token,
24
+ beyond_refresh_token: beyond_session.refresh_token)
25
+ end
26
+
27
+ #
28
+ # Generates a new access_token and refresh_token if they have expired
29
+ #
30
+ def refresh_token_if_needed
31
+ token_timestamp = decoded_jwt['exp']
32
+ current_timestamp = DateTime.now.to_i
33
+ return unless token_timestamp - current_timestamp <= 3600
34
+
35
+ refresh_token
36
+ end
37
+
38
+ #
39
+ # Returns a BeyondApi::Session object with api_url, access_token and refresh_token attributes
40
+ #
41
+ def to_session
42
+ BeyondApi::Session.new(api_url: beyond_api_url,
43
+ access_token: beyond_access_token,
44
+ refresh_token: beyond_refresh_token)
45
+ end
46
+
47
+ #
48
+ # Returns the shop url
49
+ #
50
+ def url
51
+ "https://#{URI.parse(beyond_api_url).host}"
52
+ end
53
+
54
+ def has_scope?(scope)
55
+ return unless scope.include?(':')
56
+
57
+ base, scope = scope.split(':')
58
+ !!(decoded_jwt['scope'].find { |s| s.split(':').first == base } =~ /#{base}:.*#{scope}.*/)
59
+ end
60
+
61
+ ##############################################################################
62
+ # Class methods
63
+ ##############################################################################
64
+
65
+ def self.find_session(id)
66
+ shop = find(id)
67
+ shop.to_session
68
+ end
69
+
70
+ private
71
+
72
+ def decoded_jwt
73
+ JWT.decode(beyond_access_token, nil, false).first
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end