asset.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. module ShopifyAPI
  2. # Assets represent the files that comprise your theme.
  3. # There are different buckets which hold different kinds
  4. # of assets, each corresponding to one of the folders
  5. # within a theme's zip file: "layout", "templates",
  6. # "snippets", "assets", and "config". The full key of an
  7. # asset always starts with the bucket name, and the path
  8. # separator is a forward slash, like layout/theme.liquid
  9. # or assets/bg-body.gif.
  10. #
  11. # Initialize with a key:
  12. # asset = ShopifyAPI::Asset.new(:key => 'assets/special.css', :theme_id => 12345)
  13. #
  14. # Find by key:
  15. # asset = ShopifyAPI::Asset.find('assets/image.png', :params => {:theme_id => 12345})
  16. #
  17. # Get the text or binary value:
  18. # asset.value # decodes from attachment attribute if necessary
  19. #
  20. # You can provide new data for assets in a few different ways:
  21. #
  22. # * assign text data for the value directly:
  23. # asset.value = "div.special {color:red;}"
  24. #
  25. # * provide binary data for the value:
  26. # asset.attach(File.read('image.png'))
  27. #
  28. # * set a URL from which Shopify will fetch the value:
  29. # asset.src = "http://mysite.com/image.png"
  30. #
  31. # * set a source key of another of your assets from which
  32. # the value will be copied:
  33. # asset.source_key = "assets/another_image.png"
  34. class Asset < Base
  35. include DisablePrefixCheck
  36. self.primary_key = 'key'
  37. self.prefix = "/admin/themes/:theme_id/"
  38. def self.prefix(options={})
  39. options[:theme_id].nil? ? "/admin/" : "/admin/themes/#{options[:theme_id]}/"
  40. end
  41. def self.element_path(id, prefix_options = {}, query_options = nil) #:nodoc:
  42. prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  43. "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
  44. end
  45. # find an asset by key:
  46. # ShopifyAPI::Asset.find('layout/theme.liquid', :params => {:theme_id => 99})
  47. def self.find(*args)
  48. if args[0].is_a?(Symbol)
  49. super
  50. else
  51. params = {:asset => {:key => args[0]}}
  52. params = params.merge(args[1][:params]) if args[1] && args[1][:params]
  53. path_prefix = params[:theme_id] ? "/admin/themes/#{params[:theme_id]}" : "/admin"
  54. resource = find(:one, :from => "#{path_prefix}/assets.#{format.extension}", :params => params)
  55. resource.prefix_options[:theme_id] = params[:theme_id] if resource && params[:theme_id]
  56. resource
  57. end
  58. end
  59. # For text assets, Shopify returns the data in the 'value' attribute.
  60. # For binary assets, the data is base-64-encoded and returned in the
  61. # 'attachment' attribute. This accessor returns the data in both cases.
  62. def value
  63. attributes['value'] ||
  64. (attributes['attachment'] ? Base64.decode64(attributes['attachment']) : nil)
  65. end
  66. def attach(data)
  67. self.attachment = Base64.encode64(data)
  68. end
  69. def destroy
  70. connection.delete(element_path(prefix_options.merge(:asset => {:key => key})), self.class.headers)
  71. end
  72. def new?
  73. false
  74. end
  75. def method_missing(method_symbol, *arguments) #:nodoc:
  76. if %w{value= attachment= src= source_key=}.include?(method_symbol)
  77. wipe_value_attributes
  78. end
  79. super
  80. end
  81. private
  82. def wipe_value_attributes
  83. %w{value attachment src source_key}.each do |attr|
  84. attributes.delete(attr)
  85. end
  86. end
  87. end
  88. end