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

Add new method to construct host app URL #1002

Merged
merged 2 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api

## Unreleased

N/A
- [#1002](https://github.com/Shopify/shopify-api-ruby/pull/1002) Add new method to construct the host app URL
mkevinosullivan marked this conversation as resolved.
Show resolved Hide resolved

## Version 11.0.1

Expand Down
26 changes: 26 additions & 0 deletions lib/shopify_api/auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# typed: strict
# frozen_string_literal: true

module ShopifyAPI
module Auth
extend T::Sig

class << self
extend T::Sig

sig { params(host: T.nilable(String)).returns(String) }
def embedded_app_url(host)
mkevinosullivan marked this conversation as resolved.
Show resolved Hide resolved
unless Context.setup?
raise Errors::ContextNotSetupError, "ShopifyAPI::Context not setup, please call ShopifyAPI::Context.setup"
end

unless host
raise Errors::MissingRequiredArgumentError, "host argument is required"
end

decoded_host = Base64.decode64(host)
"https://#{decoded_host}/apps/#{Context.api_key}"
end
end
end
end
9 changes: 9 additions & 0 deletions lib/shopify_api/errors/missing_required_argument_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# typed: strict
# frozen_string_literal: true

module ShopifyAPI
module Errors
class MissingRequiredArgumentError < StandardError
end
end
end
35 changes: 35 additions & 0 deletions test/auth_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# typed: false
# frozen_string_literal: true

require_relative "./test_helper"

module ShopifyAPITest
class AuthTest < Test::Unit::TestCase
def setup
super
@host = "some-shopify-store.myshopify.com/admin"
@encoded_host = Base64.strict_encode64(@host)
end

def test_valid_host
assert_equal(
ShopifyAPI::Auth.embedded_app_url(@encoded_host),
"https://#{@host}/apps/#{ShopifyAPI::Context.api_key}"
)
end

def test_no_host
assert_raises(ShopifyAPI::Errors::MissingRequiredArgumentError) do
ShopifyAPI::Auth.embedded_app_url(nil)
end
end

def test_context_not_setup
modify_context(api_key: "", api_secret_key: "", host_name: "")

assert_raises(ShopifyAPI::Errors::ContextNotSetupError) do
ShopifyAPI::Auth.embedded_app_url(@encoded_host)
end
end
end
end