Class: ShopifyAPI::Asset

Inherits:
Base
  • Object
show all
Defined in:
lib/shopify_api.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, and assets. 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')

Find by key:

asset = ShopifyAPI::Asset.find('assets/image.png')

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

Instance Method Summary collapse

Methods included from Countable

#count

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object

:nodoc:



455
456
457
458
459
460
# File 'lib/shopify_api.rb', line 455

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:



450
451
452
453
# File 'lib/shopify_api.rb', line 450

def self.element_path(id, prefix_options = {}, query_options = nil) #:nodoc:
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  "#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
end

.find(*args) ⇒ Object

find an asset by key:

ShopifyAPI::Asset.find('layout/theme.liquid')


422
423
424
425
426
427
428
# File 'lib/shopify_api.rb', line 422

def self.find(*args)
  if args[0].is_a?(Symbol)
    super
  else
    find(:one, :from => "/admin/assets.#{format.extension}", :params => {:asset => {:key => args[0]}})
  end
end

Instance Method Details

#attach(data) ⇒ Object



438
439
440
# File 'lib/shopify_api.rb', line 438

def attach(data)
  self.attachment = Base64.encode64(data)
end

#destroyObject

:nodoc:



442
443
444
# File 'lib/shopify_api.rb', line 442

def destroy #:nodoc:
  connection.delete(element_path(:asset => {:key => key}), self.class.headers)
end

#new?Boolean

:nodoc:

Returns:

  • (Boolean)


446
447
448
# File 'lib/shopify_api.rb', line 446

def new? #:nodoc:
  false
end

#valueObject

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.



433
434
435
436
# File 'lib/shopify_api.rb', line 433

def value
  attributes['value'] ||
  (attributes['attachment'] ? Base64.decode64(attributes['attachment']) : nil)
end