Permalink
Browse files

Put token in Header and change activation method

  • Loading branch information...
1 parent 4f42b0e commit f46a5ba3e0c7263e1269889e6073d562b0dd7d74 @redronin redronin committed Mar 30, 2012
Showing with 23 additions and 5 deletions.
  1. +16 −0 lib/shopify_api/resources/base.rb
  2. +7 −5 lib/shopify_api/session.rb
@@ -8,6 +8,22 @@ class Base < ActiveResource::Base
"ActiveResource/#{ActiveResource::VERSION::STRING}",
"Ruby/#{RUBY_VERSION}"].join(' ')
+
+
+ def self.activate_session(session)
+ self.site = session.site
+ ActiveResource::Base.site = session.site
+ self.headers.merge!('X-Shopify-Access-Token' => session.token)
+ ActiveResource::Base.headers.merge!('X-Shopify-Access-Token' => session.token)
+ end
+
+ def self.clear_session
+ self.site = nil
+ ActiveResource::Base.site = nil
+ self.headers.delete('X-Shopify-Access-Token')
+ ActiveResource::Base.headers.delete('X-Shopify-Access-Token')
+ end
+
private
def only_id
encode(:only => :id, :include => [], :methods => [])
View
@@ -18,12 +18,15 @@ def setup(params)
def temp(domain, token, &block)
session = new(domain, token)
- original_site = ShopifyAPI::Base.site
+ original_domain = URI.parse(ShopifyAPI::Base.site).host
+ original_token = ShopifyAPI::Base.headers['X-Shopify-Access-Token']
+ original_session = new(original_domain, original_token)
+
begin
- ShopifyAPI::Base.site = session.site
+ activate_session(session)
yield
ensure
- ShopifyAPI::Base.site = original_site
+ activate_session(original_session)
end
end
@@ -32,7 +35,6 @@ def prepare_url(url)
url.gsub!(/https?:\/\//, '') # remove http:// or https://
url.concat(".myshopify.com") unless url.include?('.') # extend url to myshopify.com if no host is given
end
-
end
def initialize(url, token = nil, params = nil)
@@ -45,7 +47,7 @@ def shop
end
def site
- "#{protocol}://:#{token}@#{url}/admin"
+ "#{protocol}://#{url}/admin"
end
def valid?

0 comments on commit f46a5ba

Please sign in to comment.