Skip to content

Commit 6639607

Browse files
Add new Auth module, method to construct host app URL
Use Base64.strict_encode64 (to avoid adding newlines), but Base64.decode64 (using strict_decode64 breaks with what's provided by Shopify)
1 parent bcd5862 commit 6639607

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44

55
## Unreleased
66

7-
N/A
7+
- [#1002](https://github.com/Shopify/shopify-api-ruby/pull/1002) Add new method to construct the host app URL
88

99
## Version 11.0.1
1010

lib/shopify_api/auth.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module ShopifyAPI
5+
module Auth
6+
extend T::Sig
7+
8+
class << self
9+
extend T::Sig
10+
11+
sig { params(host: T.nilable(String)).returns(String) }
12+
def embedded_app_url(host)
13+
unless Context.setup?
14+
raise Errors::ContextNotSetupError, "ShopifyAPI::Context not setup, please call ShopifyAPI::Context.setup"
15+
end
16+
17+
unless host
18+
raise Errors::MissingRequiredArgumentError, "host argument is required"
19+
end
20+
21+
decoded_host = Base64.decode64(host)
22+
"https://#{decoded_host}/apps/#{Context.api_key}"
23+
end
24+
end
25+
end
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
module ShopifyAPI
5+
module Errors
6+
class MissingRequiredArgumentError < StandardError
7+
end
8+
end
9+
end

test/auth_test.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# typed: false
2+
# frozen_string_literal: true
3+
4+
require_relative "./test_helper"
5+
6+
module ShopifyAPITest
7+
class AuthTest < Test::Unit::TestCase
8+
def setup
9+
super
10+
@host = "some-shopify-store.myshopify.com/admin"
11+
@encoded_host = Base64.strict_encode64(@host)
12+
end
13+
14+
def test_valid_host
15+
assert_equal(
16+
ShopifyAPI::Auth.embedded_app_url(@encoded_host),
17+
"https://#{@host}/apps/#{ShopifyAPI::Context.api_key}"
18+
)
19+
end
20+
21+
def test_no_host
22+
assert_raises(ShopifyAPI::Errors::MissingRequiredArgumentError) do
23+
ShopifyAPI::Auth.embedded_app_url(nil)
24+
end
25+
end
26+
27+
def test_context_not_setup
28+
modify_context(api_key: "", api_secret_key: "", host_name: "")
29+
30+
assert_raises(ShopifyAPI::Errors::ContextNotSetupError) do
31+
ShopifyAPI::Auth.embedded_app_url(@encoded_host)
32+
end
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)