Sha256: aec4cbd5869d662309a99b5ada46dd101a8721a8598bcb39bc0f287a938a2942

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require 'sinatra/shopify-sinatra-app'

class SinatraApp < Sinatra::Base
  register Sinatra::Shopify

  # set the scope that your app needs, read more here:
  # http://docs.shopify.com/api/tutorials/oauth
  set :scope, 'read_products, read_orders'

  # Your App's Home page
  # this is a simple example that fetches some products
  # from Shopify and displays them inside your app
  get '/' do
    shopify_session do
      @products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
      erb :home
    end
  end

  # this endpoint recieves the uninstall webhook
  # and cleans up data, add to this endpoint as your app
  # stores more data.
  post '/uninstall' do
    webhook_session do |params|
      current_shop.destroy
    end
  end

  private

  # This method gets called when your app is installed.
  # setup any webhooks or services you need on Shopify
  # inside here.
  def install
    shopify_session do
      # create an uninstall webhook, this webhook gets sent
      # when your app is uninstalled from a shop. It is good
      # practice to clean up any data from a shop when they
      # uninstall your app.
      uninstall_webhook = ShopifyAPI::Webhook.new(
        topic: 'app/uninstalled',
        address: "#{base_url}/uninstall",
        format: 'json'
      )
      uninstall_webhook.save
    end

    redirect '/'
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopify-sinatra-app-0.1.5 lib/generator/lib/app.rb
shopify-sinatra-app-0.1.4 lib/generator/lib/app.rb