Skip to content

Commit

Permalink
update api version in generators
Browse files Browse the repository at this point in the history
  • Loading branch information
jtgrenz committed Sep 6, 2019
1 parent 5da9124 commit 3cbcb9c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/generators/shopify_app/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class InstallGenerator < Rails::Generators::Base
class_option :application_name, type: :array, default: ['My', 'Shopify', 'App']
class_option :scope, type: :array, default: ['read_products']
class_option :embedded, type: :string, default: 'true'
class_option :api_version, type: :string, default: ShopifyAPI::ApiVersion.latest_stable_version.to_s
class_option :api_version, type: :string, default: nil

def add_dotenv_gem
gem('dotenv-rails', group: [:test, :development])
Expand All @@ -18,7 +18,7 @@ def add_dotenv_gem
def create_shopify_app_initializer
@application_name = format_array_argument(options['application_name'])
@scope = format_array_argument(options['scope'])
@api_version = options['api_version']
@api_version = options['api_version'] || ShopifyAPI::Meta.admin_versions.find(&:latest_supported).handle

template 'shopify_app.rb', 'config/initializers/shopify_app.rb'
end
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/shopify_app/install/templates/shopify_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
config.api_version = "<%= @api_version %>"
config.session_repository = ShopifyApp::InMemorySessionStore
end

# ShopifyApp::Utils.fetch_known_api_versions # Uncomment to fetch known api versions from shopify servers on boot
# ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown # Uncomment to raise an error if attempting to use an api version that was not previously known
7 changes: 7 additions & 0 deletions lib/shopify_app/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ def self.sanitize_shop_domain(shop_domain)
nil
end

def self.fetch_known_api_versions
Rails.logger.info("Fetching known Admin API Versions from Shopify...")
ShopifyAPI.ApiVersion.fetch_known_versions
Rails.logger.info("Known API Versions: #{ShopifyAPI::ApiVersion.versions.keys}")
rescue ActiveResource::ConnectionError
Rails.logger.error("Unable to fetch api_versions from Shopify")
end
end
end
2 changes: 1 addition & 1 deletion lib/shopify_app/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ShopifyApp
VERSION = '11.0.2'.freeze
VERSION = '11.1.0'.freeze
end
4 changes: 3 additions & 1 deletion shopify_app.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ Gem::Specification.new do |s|

s.add_runtime_dependency('browser_sniffer', '~> 1.1.2')
s.add_runtime_dependency('rails', '> 5.2.1')
s.add_runtime_dependency('shopify_api', '~> 7.0')
s.add_runtime_dependency('shopify_api', '~> 8.0')
s.add_runtime_dependency('omniauth-shopify-oauth2', '~> 2.1.0')

s.add_development_dependency('rake')
s.add_development_dependency('byebug')
s.add_development_dependency('pry')
s.add_development_dependency('sqlite3', '~> 1.4')
s.add_development_dependency('minitest')
s.add_development_dependency('mocha')
s.add_development_dependency('webmock')

s.files = `git ls-files`.split("\n").reject { |f| f.match(%r{^(test|example)/}) }
s.test_files = `git ls-files -- {test}/*`.split("\n")
Expand Down
9 changes: 4 additions & 5 deletions test/generators/install_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ class InstallGeneratorTest < Rails::Generators::TestCase
end

teardown do
ShopifyAPI::ApiVersion.clear_defined_versions
ShopifyAPI::ApiVersion.define_known_versions
ShopifyAPI::ApiVersion.clear_known_versions
end

test "creates the ShopifyApp initializer" do
latest_stable_version = ShopifyAPI::ApiVersion.latest_stable_version

run_generator
assert_file "config/initializers/shopify_app.rb" do |shopify_app|
assert_match 'config.application_name = "My Shopify App"', shopify_app
assert_match "config.api_key = ENV['SHOPIFY_API_KEY']", shopify_app
assert_match "config.secret = ENV['SHOPIFY_API_SECRET']", shopify_app
assert_match 'config.scope = "read_products"', shopify_app
assert_match "config.embedded_app = true", shopify_app
assert_match "config.api_version = \"#{latest_stable_version}\"", shopify_app
assert_match 'config.api_version = "2019-10"', shopify_app
assert_match "config.after_authenticate_job = false", shopify_app
assert_match "# ShopifyApp::Utils.fetch_known_api_versions", shopify_app
assert_match "# ShopifyAPI::ApiVersion.version_lookup_mode = :raise_on_unknown ", shopify_app
end
end

Expand Down
24 changes: 24 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
require 'rails/test_help'
require 'mocha/setup'
require 'webmock/minitest'
require 'byebug'
require 'pry'

# Filter out Minitest backtrace while allowing backtrace from other libraries
# to be shown.
Expand All @@ -15,8 +17,30 @@
class ActiveSupport::TestCase
include GeneratorTestHelpers

API_META_TEST_RESPONSE = <<~JSON
{
"apis": [{
"handle": "admin",
"versions": [{
"handle": "2019-07",
"display_name": "2019-07",
"supported": true,
"latest_supported": false
},{
"handle": "2019-10",
"latest_supported": true,
"display_name": "2019-10",
"supported": true
}]
}]
}
JSON


def before_setup
super
WebMock.disable_net_connect!
WebMock.stub_request(:get, "https://app.shopify.com/services/apis.json").to_return(body: API_META_TEST_RESPONSE)
ShopifyAppConfigurer.call
end
end

0 comments on commit 3cbcb9c

Please sign in to comment.