Browse Source

Move api_version to be set by session.

Alex Aitken 6 years ago
parent
commit
eda83e9690
3 changed files with 22 additions and 6 deletions
  1. 13 1
      lib/shopify_api/resources/base.rb
  2. 7 5
      lib/shopify_api/session.rb
  3. 2 0
      test/base_test.rb

+ 13 - 1
lib/shopify_api/resources/base.rb

@@ -29,6 +29,13 @@ module ShopifyAPI
     end
 
     class << self
+      if ActiveResource::VERSION::MAJOR >= 5 ||
+          (ActiveResource::VERSION::MAJOR == 4 && ActiveResource::VERSION::MINOR >= 1 )
+        threadsafe_attribute :_api_version
+      else
+        cattr_accessor :_api_version
+      end
+
       if ActiveResource::Base.respond_to?(:_headers) && ActiveResource::Base.respond_to?(:_headers_defined?)
         def headers
           if _headers_defined?
@@ -61,11 +68,16 @@ module ShopifyAPI
         self.site = nil
         self.password = nil
         self.user = nil
+        self.api_version = nil
         self.headers.delete('X-Shopify-Access-Token')
       end
 
       def api_version
-        @api_version ||= ApiVersion.no_version
+        self._api_version ||= ApiVersion.no_version
+      end
+
+      def api_version=(api_version)
+        self._api_version = api_version
       end
 
       def prefix(options = {})

+ 7 - 5
lib/shopify_api/session.rb

@@ -10,7 +10,7 @@ module ShopifyAPI
     cattr_accessor :api_key, :secret, :myshopify_domain
     self.myshopify_domain = 'myshopify.com'
 
-    attr_accessor :url, :token, :name, :extra
+    attr_accessor :url, :token, :api_version, :name, :extra
 
     class << self
 
@@ -18,11 +18,12 @@ module ShopifyAPI
         params.each { |k,value| public_send("#{k}=", value) }
       end
 
-      def temp(domain, token, &block)
-        session = new(domain, token)
+      def temp(domain, token, api_version = ApiVersion.no_version, &block)
+        session = new(domain, token, api_version)
         original_site = ShopifyAPI::Base.site.to_s
         original_token = ShopifyAPI::Base.headers['X-Shopify-Access-Token']
-        original_session = new(original_site, original_token)
+        original_version = ShopifyAPI::Base.api_version
+        original_session = new(original_site, original_token, original_version)
 
         begin
           ShopifyAPI::Base.activate_session(session)
@@ -65,8 +66,9 @@ module ShopifyAPI
       end
     end
 
-    def initialize(url, token = nil, extra = {})
+    def initialize(url, token = nil, api_verison = ApiVersion.no_version, extra = {})
       self.url = self.class.prepare_url(url)
+      self.api_version = api_verison
       self.token = token
       self.extra = extra
     end

+ 2 - 0
test/base_test.rb

@@ -86,6 +86,8 @@ class BaseTest < Test::Unit::TestCase
   end
 
   test "prefix= will forward to resource when the value does not start with admin" do
+    ShopifyAPI::Base.activate_session(@session1)
+
     TestResource.prefix = 'a/regular/path/'
 
     assert_equal('/admin/a/regular/path/', TestResource.prefix)