shopify-sinatra-app 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e20ce69bc42bc9bc307d4a57867d6249fadd78d8a45cc78085271af499efbc3
4
- data.tar.gz: dc0743abc76b5d92f6d14eeed9a1096611b6200f506d8f942e090ebaa77f78bf
3
+ metadata.gz: 42c91774ed4f971d81f0a1d7f8d983e9b65b54de5abc8f27e84e2bc1fca4ac81
4
+ data.tar.gz: efadfb3fc31df9453e1a808e644a3045277d6316012104510954f93274d2ab06
5
5
  SHA512:
6
- metadata.gz: bc5d771d614036bd2d966e33c1e98a98d1f539fd8922cccf5f948fd536128374a6b1aac3591e02109fb50f5ea987d88b2ebd92cd33dab5615843127eeb4904c5
7
- data.tar.gz: 3d83a80b3d6df53d4441b25ecba9966bc16535c8dd71572dfdfd40823349eab9f1094bfaa00380fa65d05e31cfd31bf01dd77384f269441a65e5dc245ca1b8cf
6
+ metadata.gz: 52b38bf496bc1092461c61f846e6fb3770c9d923977d5e6d5224a953bc06b379078423b4160252d84177263806cef6d1027a9b76a997853613889469f66ac784
7
+ data.tar.gz: f1c509bdd3356a3c07d22f7bde37cbf777954d97c4831f3dee15d7b653cc3f8c6c3e77943c083bc302cf7fd91bce37b428dc506b3a778f955bc1a515d7e54caf
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 0.6.0
2
+ -----
3
+ * remove current_shop* methods in favor of yielding shop_name to the block methods
4
+
1
5
  0.5.0
2
6
  -----
3
7
  * replace webhook_session and webhook_job with shopify_webhook
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 signaling 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:
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
@@ -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: current_shop_name).destroy
25
+ webhook_session do |shop_name, params|
26
+ Shop.find_by(name: shop_name).destroy
26
27
  end
27
28
  end
28
29
 
@@ -1,5 +1,4 @@
1
1
  ENV['RACK_ENV'] = 'test'
2
- ENV['SHOPIFY_REDIRECT_URI'] = 'http://localhost:4567'
3
2
  ENV['SECRET'] = 'secret'
4
3
 
5
4
  require 'minitest/autorun'
@@ -4,6 +4,6 @@
4
4
  <h3>Products</h3>
5
5
  <ul>
6
6
  <% @products.each do |product| %>
7
- <li><a href=<%="#{current_shop_url}/admin/products/#{product.id}"%> target="_blank"> <%= product.id %> </a></li>
7
+ <li><a href=<%="https://#{@shop.myshopify_domain}/admin/products/#{product.id}"%> target="_blank"> <%= product.id %> </a></li>
8
8
  <% end %>
9
9
  </ul>
@@ -5,7 +5,7 @@
5
5
  <script type="text/javascript">
6
6
  ShopifyApp.init({
7
7
  apiKey: "<%= SinatraApp.settings.api_key %>",
8
- shopOrigin: "<%= current_shop_url %>",
8
+ shopOrigin: "<%= shop_origin %>",
9
9
  debug: true
10
10
  });
11
11
  </script>
@@ -21,20 +21,12 @@ module Sinatra
21
21
  end
22
22
 
23
23
  def base_url
24
- @base_url ||= "#{request_protocol}://#{request.env['HTTP_HOST']}"
24
+ "#{request_protocol}://#{request.env['HTTP_HOST']}"
25
25
  end
26
26
 
27
- def current_shop
28
- Shop.find_by(name: current_shop_name)
29
- end
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 current_shop
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
- @shop_name = request.env['HTTP_X_SHOPIFY_SHOP_DOMAIN']
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(shop)
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'shopify-sinatra-app'
3
- s.version = '0.5.0'
3
+ s.version = '0.6.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.5.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-25 00:00:00.000000000 Z
11
+ date: 2018-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra