shopify-sinatra-app 0.5.0 → 0.6.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 +4 -4
- data/CHANGELOG +4 -0
- data/README.md +4 -10
- data/example/src/app.rb +4 -3
- data/example/test/test_helper.rb +0 -1
- data/example/views/home.erb +1 -1
- data/example/views/layouts/application.erb +1 -1
- data/lib/sinatra/shopify-sinatra-app.rb +10 -17
- data/shopify-sinatra-app.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42c91774ed4f971d81f0a1d7f8d983e9b65b54de5abc8f27e84e2bc1fca4ac81
|
4
|
+
data.tar.gz: efadfb3fc31df9453e1a808e644a3045277d6316012104510954f93274d2ab06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52b38bf496bc1092461c61f846e6fb3770c9d923977d5e6d5224a953bc06b379078423b4160252d84177263806cef6d1027a9b76a997853613889469f66ac784
|
7
|
+
data.tar.gz: f1c509bdd3356a3c07d22f7bde37cbf777954d97c4831f3dee15d7b653cc3f8c6c3e77943c083bc302cf7fd91bce37b428dc506b3a778f955bc1a515d7e54caf
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -80,18 +80,18 @@ Shopify::Methods
|
|
80
80
|
|
81
81
|
```ruby
|
82
82
|
get '/products.json' do
|
83
|
-
shopify_session do
|
83
|
+
shopify_session do |shop_name|
|
84
84
|
products = ShopifyAPI::Product.all(limit: 5)
|
85
85
|
products.to_json
|
86
86
|
end
|
87
87
|
end
|
88
88
|
```
|
89
89
|
|
90
|
-
**shopify_webhook** - This method is for an endpoint that receives a webhook from Shopify. Webhooks are a great way to keep your app in sync with a shop's data without polling. You can read more about webhooks [here](http://docs.shopify.com/api/tutorials/using-webhooks). This method also takes a block and yields the `webhook_body` as a hash (note only works for json webhooks, don't use xml). Here is an example that listens to an order creation webhook:
|
90
|
+
**shopify_webhook** - This method is for an endpoint that receives a webhook from Shopify. Webhooks are a great way to keep your app in sync with a shop's data without polling. You can read more about webhooks [here](http://docs.shopify.com/api/tutorials/using-webhooks). This method also takes a block and yields the `shop_name` and `webhook_body` as a hash (note only works for json webhooks, don't use xml). Here is an example that listens to an order creation webhook:
|
91
91
|
|
92
92
|
```ruby
|
93
93
|
post '/order.json' do
|
94
|
-
shopify_webhook do |webhook_data|
|
94
|
+
shopify_webhook do |shop_name, webhook_data|
|
95
95
|
# do something with the data
|
96
96
|
end
|
97
97
|
end
|
@@ -112,17 +112,11 @@ 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
|
-
**current_shop_name** - Returns the name of the current shop (format: example.myshopify.com)
|
116
|
-
|
117
|
-
**current_shop_url** - Returns the url of the current shop (format: https://example.myshopify.com)
|
118
|
-
|
119
|
-
**current_shop** - Returns the activerecord model of the current shop. Use carefully!
|
120
|
-
|
121
115
|
**base_url** - This returns the url of the app
|
122
116
|
|
123
117
|
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)
|
124
118
|
|
125
|
-
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
|
119
|
+
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:
|
126
120
|
|
127
121
|
```ruby
|
128
122
|
post '/flash_message' do
|
data/example/src/app.rb
CHANGED
@@ -11,7 +11,8 @@ class SinatraApp < Sinatra::Base
|
|
11
11
|
# this is a simple example that fetches some products
|
12
12
|
# from Shopify and displays them inside your app
|
13
13
|
get '/' do
|
14
|
-
shopify_session do
|
14
|
+
shopify_session do |shop_name|
|
15
|
+
@shop = ShopifyAPI::Shop.current
|
15
16
|
@products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
|
16
17
|
erb :home
|
17
18
|
end
|
@@ -21,8 +22,8 @@ class SinatraApp < Sinatra::Base
|
|
21
22
|
# and cleans up data, add to this endpoint as your app
|
22
23
|
# stores more data.
|
23
24
|
post '/uninstall' do
|
24
|
-
webhook_session do |params|
|
25
|
-
Shop.find_by(name:
|
25
|
+
webhook_session do |shop_name, params|
|
26
|
+
Shop.find_by(name: shop_name).destroy
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
data/example/test/test_helper.rb
CHANGED
data/example/views/home.erb
CHANGED
@@ -4,6 +4,6 @@
|
|
4
4
|
<h3>Products</h3>
|
5
5
|
<ul>
|
6
6
|
<% @products.each do |product| %>
|
7
|
-
<li><a href=<%="
|
7
|
+
<li><a href=<%="https://#{@shop.myshopify_domain}/admin/products/#{product.id}"%> target="_blank"> <%= product.id %> </a></li>
|
8
8
|
<% end %>
|
9
9
|
</ul>
|
@@ -21,20 +21,12 @@ module Sinatra
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def base_url
|
24
|
-
|
24
|
+
"#{request_protocol}://#{request.env['HTTP_HOST']}"
|
25
25
|
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
def current_shop_name
|
32
|
-
return session[:shopify][:shop] if session.key?(:shopify)
|
33
|
-
return @shop_name if @shop_name
|
34
|
-
end
|
35
|
-
|
36
|
-
def current_shop_url
|
37
|
-
"https://#{current_shop_name}" if current_shop_name
|
27
|
+
# for the esdk initializer
|
28
|
+
def shop_origin
|
29
|
+
"https://#{session[:shopify][:shop]}"
|
38
30
|
end
|
39
31
|
|
40
32
|
def shopify_session(&blk)
|
@@ -49,18 +41,18 @@ module Sinatra
|
|
49
41
|
shop_name = session[:shopify][:shop]
|
50
42
|
token = session[:shopify][:token]
|
51
43
|
activate_shopify_api(shop_name, token)
|
52
|
-
yield
|
44
|
+
yield shop_name
|
53
45
|
end
|
54
46
|
rescue ActiveResource::UnauthorizedAccess
|
55
|
-
clear_session
|
47
|
+
clear_session shop_name
|
56
48
|
redirect request.env['sinatra.route'].split(' ').last
|
57
49
|
end
|
58
50
|
|
59
51
|
def shopify_webhook(&blk)
|
60
52
|
return unless verify_shopify_webhook
|
61
|
-
|
53
|
+
shop_name = request.env['HTTP_X_SHOPIFY_SHOP_DOMAIN']
|
62
54
|
webhook_body = ActiveSupport::JSON.decode(request.body.read.to_s)
|
63
|
-
yield webhook_body
|
55
|
+
yield shop_name, webhook_body
|
64
56
|
status 200
|
65
57
|
end
|
66
58
|
|
@@ -84,8 +76,9 @@ module Sinatra
|
|
84
76
|
ShopifyAPI::Base.activate_session(api_session)
|
85
77
|
end
|
86
78
|
|
87
|
-
def clear_session(
|
79
|
+
def clear_session(shop_name)
|
88
80
|
logout
|
81
|
+
shop = Shop.find_by(name: shop_name)
|
89
82
|
shop.token = nil
|
90
83
|
shop.save
|
91
84
|
end
|
data/shopify-sinatra-app.gemspec
CHANGED
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.
|
4
|
+
version: 0.6.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-
|
11
|
+
date: 2018-12-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|