Browse Source

Add with_session and with_version blocks.

This will allow eaiser switching to a specific version for a short
duration.
Alex Aitken 6 years ago
parent
commit
ef58f407b2
2 changed files with 77 additions and 7 deletions
  1. 21 5
      lib/shopify_api/session.rb
  2. 56 2
      test/session_test.rb

+ 21 - 5
lib/shopify_api/session.rb

@@ -20,12 +20,14 @@ module ShopifyAPI
         params.each { |k,value| public_send("#{k}=", value) }
       end
 
-      def temp(domain:, token:, api_version:, &_block)
+      def temp(domain:, token:, api_version:, &block)
         session = new(domain: domain, token: token, api_version: api_version)
-        original_site = ShopifyAPI::Base.site.to_s
-        original_token = ShopifyAPI::Base.headers['X-Shopify-Access-Token']
-        original_version = ShopifyAPI::Base.api_version
-        original_session = new(domain: original_site, token: original_token, api_version: original_version)
+
+        with_session(session, &block)
+      end
+
+      def with_session(session, &_block)
+        original_session = extract_current_session
 
         begin
           ShopifyAPI::Base.activate_session(session)
@@ -35,6 +37,13 @@ module ShopifyAPI
         end
       end
 
+      def with_version(api_version, &block)
+        original_session = extract_current_session
+        session = new(domain: original_session.site, token: original_session.token, api_version: api_version)
+
+        with_session(session, &block)
+      end
+
       def prepare_domain(domain)
         return nil if domain.blank?
         # remove http:// or https://
@@ -66,6 +75,13 @@ module ShopifyAPI
         params = params.except(:signature, :hmac, :action, :controller)
         params.map{|k,v| "#{URI.escape(k.to_s, '&=%')}=#{URI.escape(v.to_s, '&%')}"}.sort.join('&')
       end
+
+      def extract_current_session
+        site = ShopifyAPI::Base.site.to_s
+        token = ShopifyAPI::Base.headers['X-Shopify-Access-Token']
+        version = ShopifyAPI::Base.api_version
+        new(domain: site, token: token, api_version: version)
+      end
     end
 
     def initialize(domain:, token:, api_version:, extra: {})

+ 56 - 2
test/session_test.rb

@@ -76,8 +76,6 @@ class SessionTest < Test::Unit::TestCase
   end
 
   test "#temp reset ShopifyAPI::Base.site to original value" do
-
-    ShopifyAPI::Session.setup(:api_key => "key", :secret => "secret")
     session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: :no_version)
     ShopifyAPI::Base.activate_session(session1)
 
@@ -93,6 +91,62 @@ class SessionTest < Test::Unit::TestCase
     assert_equal(ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::Base.api_version)
   end
 
+  test "#with_session activates the session for the duration of the block" do
+    session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: :no_version)
+    ShopifyAPI::Base.activate_session(session1)
+
+    other_session = ShopifyAPI::Session.new(
+      domain: "testshop.myshopify.com",
+      token: "any-token",
+      api_version: :unstable
+    )
+
+    ShopifyAPI::Session.with_session(other_session) do
+      @assigned_site = ShopifyAPI::Base.site
+      @assigned_version = ShopifyAPI::Base.api_version
+    end
+
+    assert_equal('https://testshop.myshopify.com', @assigned_site.to_s)
+    assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
+
+    assert_equal(ShopifyAPI::ApiVersion::Unstable.new, @assigned_version)
+    assert_equal(ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::Base.api_version)
+  end
+
+  test "#with_session resets the activated session even if there an exception during the block" do
+    session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: :no_version)
+    ShopifyAPI::Base.activate_session(session1)
+
+    other_session = ShopifyAPI::Session.new(
+      domain: "testshop.myshopify.com",
+      token: "any-token",
+      api_version: :unstable
+    )
+
+    assert_raises StandardError do
+      ShopifyAPI::Session.with_session(other_session) { raise StandardError, "" }
+    end
+
+    assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
+    assert_equal(ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::Base.api_version)
+  end
+
+  test "#with_version will adjust the actvated api version for the duration of the block" do
+    session1 = ShopifyAPI::Session.new(domain: 'fakeshop.myshopify.com', token: 'token1', api_version: :no_version)
+    ShopifyAPI::Base.activate_session(session1)
+
+    ShopifyAPI::Session.with_version(:unstable) do
+      @assigned_site = ShopifyAPI::Base.site
+      @assigned_version = ShopifyAPI::Base.api_version
+    end
+
+    assert_equal('https://fakeshop.myshopify.com', @assigned_site.to_s)
+    assert_equal('https://fakeshop.myshopify.com', ShopifyAPI::Base.site.to_s)
+
+    assert_equal(ShopifyAPI::ApiVersion::Unstable.new, @assigned_version)
+    assert_equal(ShopifyAPI::ApiVersion::NoVersion.new, ShopifyAPI::Base.api_version)
+  end
+
   test "create_permission_url returns correct url with single scope no redirect uri" do
     ShopifyAPI::Session.setup(:api_key => "My_test_key", :secret => "My test secret")
     session = ShopifyAPI::Session.new(