shopify-sinatra-app 0.6.0 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42c91774ed4f971d81f0a1d7f8d983e9b65b54de5abc8f27e84e2bc1fca4ac81
4
- data.tar.gz: efadfb3fc31df9453e1a808e644a3045277d6316012104510954f93274d2ab06
3
+ metadata.gz: 824f5c84a183bf093bf35cb931166ed811fafb4dcdb32664a9c3027a56acf38f
4
+ data.tar.gz: 4369c1e33e0032792d4016f772bd041f3eb85aea722e015a9d7e1aed48e29f78
5
5
  SHA512:
6
- metadata.gz: 52b38bf496bc1092461c61f846e6fb3770c9d923977d5e6d5224a953bc06b379078423b4160252d84177263806cef6d1027a9b76a997853613889469f66ac784
7
- data.tar.gz: f1c509bdd3356a3c07d22f7bde37cbf777954d97c4831f3dee15d7b653cc3f8c6c3e77943c083bc302cf7fd91bce37b428dc506b3a778f955bc1a515d7e54caf
6
+ metadata.gz: e64a1b720743bde0a66a79c39d5574d62f9867da1c7bb479a96253051197efb6e21f0a9366c59d1d98d503478b50a69c3707ba4300fefda601e26a78f7b5f042
7
+ data.tar.gz: 7ff7babe886332b170e4ae052669acba61a922ee2b814a77b0d52d0c72ce5920ad7171e80956dcdac81358e4489acf17002448f6f784f2dacf9d57744b2c85a4
data/.travis.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 2.5.0
4
+ - 2.5.3
5
5
 
6
6
  gemfile: example/Gemfile
7
7
 
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 0.7.0
2
+ -----
3
+ * make base_url private (users can add back in their app easily if needed)
4
+ * store request params in the session so they can be retrieved after the omniauth flow. This fixes app actions from the admin if the user's session has expired
5
+ * refactored some smaller methods to make the shopify_session method easier on the eyes.
6
+
1
7
  0.6.0
2
8
  -----
3
9
  * remove current_shop* methods in favor of yielding shop_name to the block methods
data/README.md CHANGED
@@ -112,8 +112,6 @@ It's impossible to control the flow of webhooks to your app from Shopify especia
112
112
 
113
113
  **logout** - This method clears the current session
114
114
 
115
- **base_url** - This returns the url of the app
116
-
117
115
  shopify-sinatra-app includes sinatra/activerecord for creating models that can be persisted in the database. You might want to read more about sinatra/activerecord and the methods it makes available to you: [https://github.com/janko-m/sinatra-activerecord](https://github.com/janko-m/sinatra-activerecord)
118
116
 
119
117
  shopify-sinatra-app also includes `rack-flash3` and the flash messages are forwarded to the Shopify Embedded App SDK (see the code in `views/layouts/application.erb`). Flash messages are useful for signalling to your users that a request was successful without changing the page. The following is an example of how to use a flash message in a route:
data/example/Gemfile CHANGED
@@ -1,5 +1,5 @@
1
1
  source 'https://rubygems.org'
2
- ruby '2.5.0'
2
+ ruby '2.5.3'
3
3
 
4
4
  gem 'shopify-sinatra-app', path: '../'
5
5
  gem 'sinatra-activerecord'
data/example/db/schema.rb CHANGED
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 20140414042317) do
13
+ ActiveRecord::Schema.define(version: 2014_04_14_042317) do
14
14
 
15
15
  create_table "shops", force: :cascade do |t|
16
16
  t.string "name"
data/example/src/app.rb CHANGED
@@ -22,7 +22,7 @@ class SinatraApp < Sinatra::Base
22
22
  # and cleans up data, add to this endpoint as your app
23
23
  # stores more data.
24
24
  post '/uninstall' do
25
- webhook_session do |shop_name, params|
25
+ shopify_webhook do |shop_name, params|
26
26
  Shop.find_by(name: shop_name).destroy
27
27
  end
28
28
  end
@@ -1,6 +1,16 @@
1
1
  require 'test_helper'
2
2
  require './src/app'
3
3
 
4
+ class MockShop
5
+ def initialize(shop_name)
6
+ @shop_name = shop_name
7
+ end
8
+
9
+ def myshopify_domain
10
+ @shop_name
11
+ end
12
+ end
13
+
4
14
  class AppTest < Minitest::Test
5
15
  def app
6
16
  SinatraApp
@@ -8,10 +18,12 @@ class AppTest < Minitest::Test
8
18
 
9
19
  def setup
10
20
  @shop_name = 'testshop.myshopify.com'
21
+ @shopify_shop = MockShop.new(@shop_name)
11
22
  end
12
23
 
13
24
  def test_root_with_session
14
25
  set_session
26
+ fake 'https://testshop.myshopify.com/admin/shop.json', body: {myshopify_domain: @shop_name}.to_json
15
27
  fake 'https://testshop.myshopify.com/admin/products.json?limit=10', body: '{}'
16
28
  get '/'
17
29
  assert last_response.ok?
@@ -20,6 +32,7 @@ class AppTest < Minitest::Test
20
32
  def test_root_with_session_activates_api
21
33
  set_session
22
34
  SinatraApp.any_instance.expects(:activate_shopify_api).with(@shop_name, 'token')
35
+ ShopifyAPI::Shop.expects(:current).returns(@shopify_shop)
23
36
  ShopifyAPI::Product.expects(:find).returns([])
24
37
  get '/'
25
38
  assert last_response.ok?
@@ -46,7 +59,7 @@ class AppTest < Minitest::Test
46
59
  set_session
47
60
  SinatraApp.any_instance.expects(:activate_shopify_api).with(@shop_name, 'token')
48
61
  SinatraApp.any_instance.expects(:clear_session)
49
- ShopifyAPI::Product.expects(:find).raises(ActiveResource::UnauthorizedAccess.new('UnauthorizedAccess'))
62
+ ShopifyAPI::Shop.expects(:current).raises(ActiveResource::UnauthorizedAccess.new('UnauthorizedAccess'))
50
63
  get '/'
51
64
  assert_equal 302, last_response.status
52
65
  assert_equal 'http://example.org/', last_response.location
@@ -1,3 +1,5 @@
1
+ $VERBOSE = nil
2
+
1
3
  ENV['RACK_ENV'] = 'test'
2
4
  ENV['SECRET'] = 'secret'
3
5
 
@@ -18,10 +18,7 @@ module Sinatra
18
18
 
19
19
  def logout
20
20
  session.delete(:shopify)
21
- end
22
-
23
- def base_url
24
- "#{request_protocol}://#{request.env['HTTP_HOST']}"
21
+ session.clear
25
22
  end
26
23
 
27
24
  # for the esdk initializer
@@ -30,13 +27,14 @@ module Sinatra
30
27
  end
31
28
 
32
29
  def shopify_session(&blk)
33
- return_to = request.env['sinatra.route'].split(' ').last
30
+ return_to = request.path
31
+ return_params = request.params
34
32
 
35
- if !session.key?(:shopify)
36
- authenticate(return_to)
37
- elsif params[:shop].present? && session[:shopify][:shop] != sanitize_shop_param(params)
33
+ if no_session?
34
+ authenticate(return_to, return_params)
35
+ elsif different_shop?
38
36
  logout
39
- authenticate(return_to)
37
+ authenticate(return_to, return_params)
40
38
  else
41
39
  shop_name = session[:shopify][:shop]
42
40
  token = session[:shopify][:token]
@@ -45,7 +43,7 @@ module Sinatra
45
43
  end
46
44
  rescue ActiveResource::UnauthorizedAccess
47
45
  clear_session shop_name
48
- redirect request.env['sinatra.route'].split(' ').last
46
+ redirect request.path
49
47
  end
50
48
 
51
49
  def shopify_webhook(&blk)
@@ -62,8 +60,21 @@ module Sinatra
62
60
  request.secure? ? 'https' : 'http'
63
61
  end
64
62
 
65
- def authenticate(return_to = '/')
63
+ def base_url
64
+ "#{request_protocol}://#{request.env['HTTP_HOST']}"
65
+ end
66
+
67
+ def no_session?
68
+ !session.key?(:shopify)
69
+ end
70
+
71
+ def different_shop?
72
+ params[:shop].present? && session[:shopify][:shop] != sanitize_shop_param(params)
73
+ end
74
+
75
+ def authenticate(return_to = '/', return_params = nil)
66
76
  if shop_name = sanitized_shop_name
77
+ session[:return_params] = return_params if return_params
67
78
  redirect_url = "/auth/shopify?shop=#{shop_name}&return_to=#{base_url}#{return_to}"
68
79
  redirect_javascript redirect_url
69
80
  else
@@ -221,6 +232,11 @@ module Sinatra
221
232
  after_shopify_auth()
222
233
 
223
234
  return_to = env['omniauth.params']['return_to']
235
+ return_params = session[:return_params]
236
+ session.delete(:return_params)
237
+
238
+ return_to += "?#{return_params.to_query}" if return_params.present?
239
+
224
240
  redirect return_to
225
241
  end
226
242
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'shopify-sinatra-app'
3
- s.version = '0.6.0'
3
+ s.version = '0.7.0'
4
4
 
5
5
  s.summary = 'A classy shopify app'
6
6
  s.description = 'A Sinatra extension for building Shopify Apps. Akin to the shopify_app gem but for Sinatra'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-sinatra-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Hughes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-26 00:00:00.000000000 Z
11
+ date: 2019-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra