Procházet zdrojové kódy

changed create_permission_url to instance method and updated readme

Kevin Hughes před 11 roky
rodič
revize
07ede115a3
2 změnil soubory, kde provedl 27 přidání a 20 odebrání
  1. 17 9
      README.rdoc
  2. 10 11
      lib/shopify_api/session.rb

+ 17 - 9
README.rdoc

@@ -49,10 +49,20 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
 
    * client_id – Required – The API key for your app
    * scope – Required – The list of required scopes (explained here: http://docs.shopify.com/api/tutorials/oauth)
-   * redirect_uri – Optional – The URL that the merchant will be sent to once authentication is complete. Must be the same host as the Return URL specified in the application settings
+   * redirect_uri – Optional – The URL that the merchant will be sent to once authentication is complete. Defaults to the URL specified in the application settings and must be the same host as that URL.
 
-   We've added the create_permision_url method to make this easier:
-    permission_url = ShopifyAPI::Session.create_permission_url("SHOP_NAME.myshopify.com", scope=["write_products"], redirect_uri=nil)  
+   We've added the create_permision_url method to make this easier, first instantiate your session object:
+    
+    session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com")
+
+  Then call:
+
+    scope = ["write_products"]
+    permission_url = session.create_permission_url(scope)
+
+  or if you want a custom redirect_uri:  
+
+    permission_url = session.create_permission_url(scope, "https://my_redirect_uri.com")
 
 4. Once authorized, the shop redirects the owner to the return URL of your application with a parameter named 'code'. This is a temporary token that the app can exchange for a permanent access token. Make the following call:
 
@@ -66,15 +76,13 @@ ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveR
 
    and you'll get your permanent access token back in the response.
 
-   There is a method to make the request and get the token for you, first instantiate a new session with the shop url:
-
-    session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com")
-   
-   Then call:
+   There is a method to make the request and get the token for you:
 
     token = session.request_token(code)
 
-   Which will request the token, save it to the session object and return it.
+   Which will request the token, save it to the session object and return it. For future sessions simply pass the token in when creating the session object:
+
+    session = ShopifyAPI::Session.new("SHOP_NAME.myshopify.com", token)
 
 5. The session must be activated before use:
 

+ 10 - 11
lib/shopify_api/session.rb

@@ -38,17 +38,6 @@ module ShopifyAPI
         url.concat(".myshopify.com") unless url.include?('.')   # extend url to myshopify.com if no host is given
       end
 
-      def create_permission_url(shop_url, scope, redirect_uri=nil)
-        self.prepare_url(shop_url)
-        params = {:client_id => self.api_key, :scope => scope.join(',')}
-        params[:redirect_uri => redirect_uri] if redirect_uri
-        "#{self.protocol}://#{shop_url}/admin/oauth/authorize?#{parameterize(params)}"
-      end
-
-      def parameterize(params)
-        URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))
-      end
-
       def validate_signature(params)
         return false unless signature = params[:signature]
 
@@ -80,6 +69,16 @@ module ShopifyAPI
       end
     end
 
+    def create_permission_url(scope, redirect_uri = nil)
+      params = {:client_id => self.api_key, :scope => scope.join(',')}
+      params[:redirect_uri] = redirect_uri if redirect_uri
+      "#{self.protocol}://#{url}/admin/oauth/authorize?#{parameterize(params)}"
+    end
+
+    def parameterize(params)
+      URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))
+    end
+
     def request_token(code)
       return self.token if self.token