Class: ShopifyAPI::Asset
- Inherits:
-
Base
- Object
- ActiveResource::Base
- Base
- ShopifyAPI::Asset
- Includes:
- DisablePrefixCheck
- Defined in:
- lib/shopify_api/resources/asset.rb
Overview
Assets represent the files that comprise your theme. There are different buckets which hold different kinds of assets, each corresponding to one of the folders within a theme’s zip file: “layout”, “templates”, “snippets”, “assets”, and “config”. The full key of an asset always starts with the bucket name, and the path separator is a forward slash, like layout/theme.liquid or assets/bg-body.gif.
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"
Class Method Summary collapse
-
.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object
:nodoc:.
-
.find(*args) ⇒ Object
find an asset by key: ShopifyAPI::Asset.find(‘layout/theme.liquid’, :params => => 99).
Instance Method Summary collapse
- #attach(data) ⇒ Object
- #destroy ⇒ Object
-
#method_missing(method_symbol, *arguments) ⇒ Object
:nodoc:.
- #new? ⇒ Boolean
-
#value ⇒ Object
For text assets, Shopify returns the data in the ‘value’ attribute.
Methods inherited from Base
activate_session, #as_json, clear_session, #encode, #headers, init_prefix, init_prefix_explicit, #persisted?
Methods included from Countable
Methods inherited from ActiveResource::Base
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object
:nodoc:
81 82 83 84 85 86 |
# File 'lib/shopify_api/resources/asset.rb', line 81 def method_missing(method_symbol, *arguments) #:nodoc: if %w{value= attachment= src= source_key=}.include?(method_symbol) wipe_value_attributes end super end |
Class Method Details
.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object
:nodoc:
41 42 43 44 |
# File 'lib/shopify_api/resources/asset.rb', line 41 def self.element_path(id, = {}, = nil) #:nodoc: , = () if .nil? "#{prefix()}#{collection_name}.#{format.extension}#{query_string()}" end |
.find(*args) ⇒ Object
find an asset by key:
ShopifyAPI::Asset.find('layout/theme.liquid', :params => {:theme_id => 99})
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/shopify_api/resources/asset.rb', line 48 def self.find(*args) if args[0].is_a?(Symbol) super else 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) resource.[:theme_id] = params[:theme_id] if resource && params[:theme_id] resource end end |
Instance Method Details
#attach(data) ⇒ Object
69 70 71 |
# File 'lib/shopify_api/resources/asset.rb', line 69 def attach(data) self. = Base64.encode64(data) end |
#destroy ⇒ Object
73 74 75 |
# File 'lib/shopify_api/resources/asset.rb', line 73 def destroy connection.delete(element_path(.merge(:asset => {:key => key})), self.class.headers) end |
#new? ⇒ Boolean
77 78 79 |
# File 'lib/shopify_api/resources/asset.rb', line 77 def new? false end |
#value ⇒ Object
For text assets, Shopify returns the data in the ‘value’ attribute. For binary assets, the data is base-64-encoded and returned in the ‘attachment’ attribute. This accessor returns the data in both cases.
64 65 66 67 |
# File 'lib/shopify_api/resources/asset.rb', line 64 def value attributes['value'] || (attributes['attachment'] ? Base64.decode64(attributes['attachment']) : nil) end |