Prechádzať zdrojové kódy

Move /admin/ api_prefix to ShopifyAPI::Base.

This is the first step in moving the start of the resource urls (/admin) to one spot.
Alex Aitken 6 rokov pred
rodič
commit
f8e9f81ff3

+ 1 - 1
lib/shopify_api/disable_prefix_check.rb

@@ -17,7 +17,7 @@ module ShopifyAPI
         define_singleton_method :prefix do |options = {}|
           resource_type =  options[resource] if flexible
 
-          options[resource_id].nil? ? "/admin/" : "/admin/#{resource_type}/#{options[resource_id]}/"
+          options[resource_id].nil? ? api_prefix : "#{api_prefix}#{resource_type}/#{options[resource_id]}/"
         end
 
         define_singleton_method :instantiate_record do |record, prefix_options = {}|

+ 1 - 1
lib/shopify_api/resources/access_scope.rb

@@ -1,5 +1,5 @@
 module ShopifyAPI
   class AccessScope < Base
-    self.prefix = '/admin/oauth/'
+    self.prefix = "#{api_prefix}oauth/"
   end
 end

+ 11 - 11
lib/shopify_api/resources/asset.rb

@@ -10,24 +10,24 @@ module ShopifyAPI
   #
   # Initialize with a key:
   #   asset = ShopifyAPI::Asset.new(:key => 'assets/special.css', :theme_id => 12345)
-  # 
+  #
   # Find by key:
   #   asset = ShopifyAPI::Asset.find('assets/image.png', :params => {:theme_id => 12345})
-  # 
+  #
   # Get the text or binary value:
   #   asset.value # decodes from attachment attribute if necessary
-  # 
+  #
   # You can provide new data for assets in a few different ways:
-  # 
+  #
   #   * assign text data for the value directly:
   #       asset.value = "div.special {color:red;}"
-  #     
+  #
   #   * provide binary data for the value:
   #       asset.attach(File.read('image.png'))
-  #     
+  #
   #   * set a URL from which Shopify will fetch the value:
   #       asset.src = "http://mysite.com/image.png"
-  #     
+  #
   #   * set a source key of another of your assets from which
   #     the value will be copied:
   #       asset.source_key = "assets/another_image.png"
@@ -44,15 +44,15 @@ module ShopifyAPI
     end
 
     # find an asset by key:
-    #   ShopifyAPI::Asset.find('layout/theme.liquid', :params => {:theme_id => 99})
+    #   ShopifyAPI::Asset.find('layout/theme.liquid', :params => { theme_id: 99 })
     def self.find(*args)
       if args[0].is_a?(Symbol)
         super
       else
-        params = {:asset => {:key => args[0]}}
+        params = { asset: { key: args[0] } }
         params = params.merge(args[1][:params]) if args[1] && args[1][:params]
-        path_prefix = params[:theme_id] ? "/admin/themes/#{params[:theme_id]}" : "/admin"
-        resource = find(:one, :from => "#{path_prefix}/assets.#{format.extension}", :params => params)
+        path_prefix = params[:theme_id] ? "themes/#{params[:theme_id]}/" : ""
+        resource = find(:one, from: "#{api_prefix}#{path_prefix}assets.#{format.extension}", params: params)
         resource.prefix_options[:theme_id] = params[:theme_id] if resource && params[:theme_id]
         resource
       end

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

@@ -10,6 +10,8 @@ module ShopifyAPI
                                   "ActiveResource/#{ActiveResource::VERSION::STRING}",
                                   "Ruby/#{RUBY_VERSION}"].join(' ')
 
+    API_PREFIX = '/admin/'.freeze
+
     def encode(options = {})
       same = dup
       same.attributes = {self.class.element_name => same.attributes} if self.class.format.extension == 'json'
@@ -63,12 +65,21 @@ module ShopifyAPI
         self.headers.delete('X-Shopify-Access-Token')
       end
 
+      def api_prefix
+        API_PREFIX
+      end
+
+      def prefix(options = {})
+        self.prefix = api_prefix
+        prefix(options)
+      end
+
       def init_prefix(resource)
         init_prefix_explicit(resource.to_s.pluralize, "#{resource}_id")
       end
 
       def init_prefix_explicit(resource_type, resource_id)
-        self.prefix = "/admin/#{resource_type}/:#{resource_id}/"
+        self.prefix = "#{api_prefix}#{resource_type}/:#{resource_id}/"
 
         define_method resource_id.to_sym do
           @prefix_options[resource_id]

+ 1 - 1
lib/shopify_api/resources/fulfillment_event.rb

@@ -1,6 +1,6 @@
 module ShopifyAPI
   class FulfillmentEvent < Base
-    self.prefix = '/admin/orders/:order_id/fulfillments/:fulfillment_id/'
+    self.prefix = "#{api_prefix}orders/:order_id/fulfillments/:fulfillment_id/"
     self.collection_name = 'events'
     self.element_name = 'event'
 

+ 1 - 1
lib/shopify_api/resources/location.rb

@@ -2,7 +2,7 @@ module ShopifyAPI
   class Location < Base
 
     def inventory_levels
-      ShopifyAPI::InventoryLevel.find(:all, from: "/admin/locations/#{id}/inventory_levels.json")
+      ShopifyAPI::InventoryLevel.find(:all, from: "#{self.class.prefix}locations/#{id}/inventory_levels.json")
     end
   end
 end

+ 1 - 1
lib/shopify_api/resources/payment.rb

@@ -2,6 +2,6 @@
 
 module ShopifyAPI
   class Payment < Base
-    self.prefix = '/admin/checkouts/:checkout_id/'
+    self.prefix = "#{api_prefix}checkouts/:checkout_id/"
   end
 end

+ 1 - 1
lib/shopify_api/resources/ping/conversation.rb

@@ -3,7 +3,7 @@
 module ShopifyAPI
   module Ping
     class Conversation < Base
-      self.prefix = "/admin/api/ping-api/v1/"
+      self.prefix = "#{api_prefix}api/ping-api/v1/"
 
       def send_message(message_attrs)
         message = ShopifyAPI::Ping::Message.new(

+ 1 - 1
lib/shopify_api/resources/ping/delivery_confirmation_details.rb

@@ -3,7 +3,7 @@
 module ShopifyAPI
   module Ping
     class DeliveryConfirmationDetails < Base
-      self.prefix = "/admin/api/ping-api/v1/conversations/:conversation_id/messages/:message_id/"
+      self.prefix = "#{api_prefix}api/ping-api/v1/conversations/:conversation_id/messages/:message_id/"
       self.collection_name = "delivery_confirmation"
     end
   end

+ 1 - 1
lib/shopify_api/resources/ping/message.rb

@@ -2,7 +2,7 @@
 module ShopifyAPI
   module Ping
     class Message < Base
-      self.prefix = "/admin/api/ping-api/v1/conversations/:conversation_id/"
+      self.prefix = "#{api_prefix}api/ping-api/v1/conversations/:conversation_id/"
     end
   end
 end

+ 1 - 1
lib/shopify_api/resources/shipping_rate.rb

@@ -2,6 +2,6 @@
 
 module ShopifyAPI
   class ShippingRate < Base
-    self.prefix = '/admin/checkouts/:checkout_id/'
+    self.prefix = "#{api_prefix}checkouts/:checkout_id/"
   end
 end

+ 3 - 3
lib/shopify_api/resources/shop.rb

@@ -5,12 +5,12 @@ module ShopifyAPI
     if ActiveResource::VERSION::MAJOR >= 4
       include ActiveResource::Singleton
 
-      def self.current(options={})
+      def self.current(options = {})
         find(options)
       end
     else
-      def self.current(options={})
-        find(:one, options.merge({from: "/admin/shop.#{format.extension}"}))
+      def self.current(options = {})
+        find(:one, options.merge(from: "#{api_prefix}shop.#{format.extension}"))
       end
     end
 

+ 1 - 1
lib/shopify_api/resources/smart_collection.rb

@@ -5,7 +5,7 @@ module ShopifyAPI
 
     def products(options = {})
       if options.present?
-        Product.find(:all, from: "/admin/smart_collections/#{id}/products.json", params: options)
+        Product.find(:all, from: "#{self.class.prefix}smart_collections/#{id}/products.json", params: options)
       else
         Product.find(:all, params: { collection_id: id })
       end

+ 1 - 1
lib/shopify_api/session.rb

@@ -132,7 +132,7 @@ module ShopifyAPI
     end
 
     def access_token_request(code)
-      uri = URI.parse("https://#{url}/admin/oauth/access_token")
+      uri = URI.parse("https://#{url}#{Base.api_prefix}oauth/access_token")
       https = Net::HTTP.new(uri.host, uri.port)
       https.use_ssl = true
       request = Net::HTTP::Post.new(uri.request_uri)