Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update api version handling #790

Merged
merged 3 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ config.api_version = '2019-04'

### Session storage change

You will need to add an `api_version` method to you session storage object. The default implmentation for this is.
You will need to add an `api_version` method to you session storage object. The default implementation for this is.
```ruby
def api_version
ShopifyApp.configuration.api_version
Expand All @@ -480,7 +480,7 @@ end

### Generated file change

`embedded_app.html.erb` the useage of `shop_session.url` needs to be changed to `shop_session.domain`
`embedded_app.html.erb` the usage of `shop_session.url` needs to be changed to `shop_session.domain`
```erb
<script type="text/javascript">
ShopifyApp.init({
Expand Down
1 change: 0 additions & 1 deletion docs/Releasing.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Releasing ShopifyApp
1. Check the Semantic Versioning page for info on how to version the new release: http://semver.org
2. Create a pull request with the following changes:
* Update the version of ShopifyApp in lib/shopify_app/version.rb
* Update the version in package.json, then run `npm install` to make sure the lock file gets updated
* Add a CHANGELOG entry for the new release with the date
* Change the title of the PR to something like: "Packaging for release X.Y.Z"
3. Merge your pull request
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great addition.

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("[ShopifyAPI::ApiVersion] Fetching known Admin API Versions from Shopify...")
ShopifyAPI::ApiVersion.fetch_known_versions
Rails.logger.info("[ShopifyAPI::ApiVersion] Known API Versions: #{ShopifyAPI::ApiVersion.versions.keys}")
rescue ActiveResource::ConnectionError
logger.error( "[ShopifyAPI::ApiVersion] 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