th_shopify_api 1.2.6.pre
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/CHANGELOG +57 -0
- data/LICENSE +20 -0
- data/README.rdoc +60 -0
- data/RELEASING +16 -0
- data/Rakefile +41 -0
- data/bin/shopify +4 -0
- data/lib/active_resource/connection_ext.rb +16 -0
- data/lib/shopify_api.rb +18 -0
- data/lib/shopify_api/countable.rb +7 -0
- data/lib/shopify_api/events.rb +7 -0
- data/lib/shopify_api/limits.rb +76 -0
- data/lib/shopify_api/metafields.rb +18 -0
- data/lib/shopify_api/resources.rb +40 -0
- data/lib/shopify_api/resources/address.rb +4 -0
- data/lib/shopify_api/resources/application_charge.rb +9 -0
- data/lib/shopify_api/resources/article.rb +12 -0
- data/lib/shopify_api/resources/asset.rb +95 -0
- data/lib/shopify_api/resources/base.rb +5 -0
- data/lib/shopify_api/resources/billing_address.rb +4 -0
- data/lib/shopify_api/resources/blog.rb +10 -0
- data/lib/shopify_api/resources/cli.rb +161 -0
- data/lib/shopify_api/resources/collect.rb +5 -0
- data/lib/shopify_api/resources/comment.rb +13 -0
- data/lib/shopify_api/resources/countable.rb +7 -0
- data/lib/shopify_api/resources/country.rb +4 -0
- data/lib/shopify_api/resources/custom_collection.rb +19 -0
- data/lib/shopify_api/resources/customer.rb +4 -0
- data/lib/shopify_api/resources/customer_group.rb +4 -0
- data/lib/shopify_api/resources/event.rb +10 -0
- data/lib/shopify_api/resources/fulfillment.rb +5 -0
- data/lib/shopify_api/resources/image.rb +16 -0
- data/lib/shopify_api/resources/line_item.rb +4 -0
- data/lib/shopify_api/resources/metafield.rb +15 -0
- data/lib/shopify_api/resources/note_attribute.rb +4 -0
- data/lib/shopify_api/resources/option.rb +4 -0
- data/lib/shopify_api/resources/order.rb +25 -0
- data/lib/shopify_api/resources/page.rb +6 -0
- data/lib/shopify_api/resources/payment_details.rb +4 -0
- data/lib/shopify_api/resources/product.rb +33 -0
- data/lib/shopify_api/resources/product_search_engine.rb +4 -0
- data/lib/shopify_api/resources/province.rb +5 -0
- data/lib/shopify_api/resources/receipt.rb +4 -0
- data/lib/shopify_api/resources/recurring_application_charge.rb +23 -0
- data/lib/shopify_api/resources/redirect.rb +4 -0
- data/lib/shopify_api/resources/rule.rb +4 -0
- data/lib/shopify_api/resources/script_tag.rb +4 -0
- data/lib/shopify_api/resources/shipping_address.rb +4 -0
- data/lib/shopify_api/resources/shipping_line.rb +4 -0
- data/lib/shopify_api/resources/shop.rb +23 -0
- data/lib/shopify_api/resources/smart_collection.rb +10 -0
- data/lib/shopify_api/resources/tax_line.rb +4 -0
- data/lib/shopify_api/resources/theme.rb +4 -0
- data/lib/shopify_api/resources/transaction.rb +5 -0
- data/lib/shopify_api/resources/variant.rb +11 -0
- data/lib/shopify_api/resources/webhook.rb +4 -0
- data/lib/shopify_api/session.rb +166 -0
- data/shopify_api.gemspec +35 -0
- data/test/cli_test.rb +109 -0
- data/test/limits_test.rb +37 -0
- data/test/order_test.rb +48 -0
- data/test/shopify_api_test.rb +55 -0
- data/test/test_helper.rb +29 -0
- metadata +153 -0
data/.document
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
== Version 1.2.5
|
2
|
+
|
3
|
+
* Fix for Article#comments
|
4
|
+
|
5
|
+
== Version 1.2.4
|
6
|
+
|
7
|
+
* Added Article#comments
|
8
|
+
* Added Order#cancel
|
9
|
+
* Added Comment#restore, #not_spam
|
10
|
+
|
11
|
+
== Version 1.2.3
|
12
|
+
|
13
|
+
* Added Customer, CustomerGroup support
|
14
|
+
|
15
|
+
== Version 1.2.2
|
16
|
+
|
17
|
+
* Added ScriptTag support
|
18
|
+
|
19
|
+
== Version 1.2.1
|
20
|
+
|
21
|
+
* Allow abbreviated names for all commands like rails does, e.g. 'shopify c' instead of 'shopify console'
|
22
|
+
* Fix Variant to support accessing both nested variants with a product prefix as well as top level variants directly
|
23
|
+
* Add 'grande' to supported product image size variants
|
24
|
+
|
25
|
+
== Version 1.2.0
|
26
|
+
|
27
|
+
* Command-line interface
|
28
|
+
* Allow custom params when fetching a single Asset
|
29
|
+
|
30
|
+
== Version 1.1.3 (November 4, 2010)
|
31
|
+
|
32
|
+
* Add ProductSearchEngines resource
|
33
|
+
|
34
|
+
== Version 1.1.2 (October 20, 2010)
|
35
|
+
|
36
|
+
* Fix for users of ActiveResource 3.x
|
37
|
+
|
38
|
+
== Version 1.1.1 (October 5, 2010)
|
39
|
+
|
40
|
+
* Remove hard coded xml formatting in API calls
|
41
|
+
* Remove jeweler stuff
|
42
|
+
* Ruby 1.9 encoding fix
|
43
|
+
|
44
|
+
== Version 1.1.0 (September 24, 2010)
|
45
|
+
|
46
|
+
* Add new Events API for Shop, Order, Product, CustomCollection, SmartCollection, Page, Blog and Article
|
47
|
+
* Add new 'compact' product image size variant
|
48
|
+
* Rails 3 fix: attribute_accessors has to be explicitly included since activesupport 3.0.0
|
49
|
+
|
50
|
+
== Version 1.0.6
|
51
|
+
|
52
|
+
* Add metafields
|
53
|
+
* Add latest changes from Shopify including asset support, token validation and a common base class
|
54
|
+
|
55
|
+
== Version 1.0.0
|
56
|
+
|
57
|
+
* extracting ShopifyAPI from Shopify into Gem
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 "JadedPixel inc."
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
= Shopify API
|
2
|
+
|
3
|
+
The Shopify API gem allows Ruby developers to programmatically access the admin section of Shopify stores.
|
4
|
+
|
5
|
+
The API is implemented as XML over HTTP using all four verbs (GET/POST/PUT/DELETE). Each resource, like Order, Product, or Collection, has its own URL and is manipulated in isolation. In other words, we’ve tried to make the API follow the REST principles as much as possible.
|
6
|
+
|
7
|
+
|
8
|
+
== Usage
|
9
|
+
|
10
|
+
=== Requirements
|
11
|
+
|
12
|
+
All API usage happens through Shopify applications, created by either shop owners for their own shops, or by Shopify Partners for use by other shop owners:
|
13
|
+
|
14
|
+
* Shop owners can create applications for themselves through their own admin (under the Preferences > Applications tab).
|
15
|
+
* Shopify Partners create applications through their admin: http://app.shopify.com/services/partners
|
16
|
+
|
17
|
+
For more information and detailed documentation about the API visit http://api.shopify.com
|
18
|
+
|
19
|
+
=== Getting Started
|
20
|
+
|
21
|
+
ShopifyAPI uses ActiveResource to communicate with the REST web service. ActiveResource has to be configured with a fully authorized URL of a particular store first. To obtain that URL you can follow these steps:
|
22
|
+
|
23
|
+
1. First create a new application in either the partners admin or your store admin and write down your API_KEY and SHARED_SECRET.
|
24
|
+
|
25
|
+
2. You will need to supply two parameters to the Session class before you instantiate it:
|
26
|
+
|
27
|
+
ShopifyAPI::Session.setup({:api_key => API_KEY, :secret => SHARED_SECRET})
|
28
|
+
|
29
|
+
3. Create a new Session for a specific shop. That session is not fully valid yet, but it can be used to create a URL that you will redirect your users to:
|
30
|
+
|
31
|
+
session = ShopifyAPI::Session.new("yourshopname.myshopify.com")
|
32
|
+
session.valid? # returns false
|
33
|
+
|
34
|
+
4. To access the API shop owners need a "token" from that specific shop. In order to get this token they need to authorize with that shop first. The URL to redirect your user to can be generated via:
|
35
|
+
|
36
|
+
url = session.create_permission_url
|
37
|
+
|
38
|
+
5. After visiting this URL, the shop redirects the owner to a custom URL of your application where the "token" gets sent to (it's param name is just "t"). Use that token to instantiate a "valid" session, that is ready to make calls to that particular shop.
|
39
|
+
|
40
|
+
token = params[:t]
|
41
|
+
session = ShopifyAPI::Session.new("yourshopname.myshopify.com", token)
|
42
|
+
session.valid? # returns true
|
43
|
+
|
44
|
+
6. Now you can finally get the fully authorized URL for that shop. Use that URL to configure ActiveResource and you're set:
|
45
|
+
|
46
|
+
ActiveResource::Base.site = session.site
|
47
|
+
|
48
|
+
7. Get data from that shop (returns ActiveResource instances):
|
49
|
+
|
50
|
+
shop = ShopifyAPI::Shop.current
|
51
|
+
latest_orders = ShopifyAPI::Order.find(:all)
|
52
|
+
|
53
|
+
Alternatively, you can use #temp to initialize a Session and execute a command which also handles temporarily setting ActiveResource::Base.site:
|
54
|
+
|
55
|
+
latest_orders = ShopifyAPI::Session.temp("yourshopname.myshopify.com", token) { ShopifyAPI::Order.find(:all) }
|
56
|
+
|
57
|
+
|
58
|
+
== Copyright
|
59
|
+
|
60
|
+
Copyright (c) 2011 "JadedPixel inc.". See LICENSE for details.
|
data/RELEASING
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Releasing ShopifyAPI
|
2
|
+
|
3
|
+
1. Check the Semantic Versioning page for info on how to version the new release: http://semver.org
|
4
|
+
2. Update the version of ShopifyAPI in shopify_api.gemspec
|
5
|
+
3. Add a CHANGELOG entry for the new release with the date (also include the updated date in shopify_api.gemspec)
|
6
|
+
4. Commit the changes with a commit message like "Packaging for release X.Y.Z"
|
7
|
+
5. Tag the release with the version (Leave REV blank for HEAD or provide a SHA)
|
8
|
+
$ git tag vX.Y.Z REV
|
9
|
+
6. Push out the changes
|
10
|
+
$ git push
|
11
|
+
7. Push out the tags
|
12
|
+
$ git push --tags
|
13
|
+
8. Build the new .gem from the updated .gemspec
|
14
|
+
$ gem build shopify_api.gemspec
|
15
|
+
9. Publish the Gem to gemcutter
|
16
|
+
$ gem push shopify_api-X.Y.Z.gem
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
require 'rake/testtask'
|
6
|
+
Rake::TestTask.new(:test) do |test|
|
7
|
+
test.libs << 'lib' << 'test'
|
8
|
+
test.pattern = 'test/**/*_test.rb'
|
9
|
+
test.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
begin
|
13
|
+
require 'rcov/rcovtask'
|
14
|
+
Rcov::RcovTask.new do |test|
|
15
|
+
test.libs << 'test'
|
16
|
+
test.pattern = 'test/**/*_test.rb'
|
17
|
+
test.verbose = true
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
task :rcov do
|
21
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
task :default => :test
|
27
|
+
|
28
|
+
require 'rake/rdoctask'
|
29
|
+
Rake::RDocTask.new do |rdoc|
|
30
|
+
if File.exist?('VERSION.yml')
|
31
|
+
config = YAML.load(File.read('VERSION.yml'))
|
32
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
33
|
+
else
|
34
|
+
version = ""
|
35
|
+
end
|
36
|
+
|
37
|
+
rdoc.rdoc_dir = 'rdoc'
|
38
|
+
rdoc.title = "shopify_api #{version}"
|
39
|
+
rdoc.rdoc_files.include('README*')
|
40
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
41
|
+
end
|
data/bin/shopify
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'active_support/core_ext/module/aliasing'
|
2
|
+
|
3
|
+
module ActiveResource
|
4
|
+
class Connection
|
5
|
+
|
6
|
+
attr_reader :response
|
7
|
+
|
8
|
+
def handle_response_with_response_capture(response)
|
9
|
+
@response = handle_response_without_response_capture(response)
|
10
|
+
end
|
11
|
+
|
12
|
+
alias_method_chain :handle_response, :response_capture
|
13
|
+
# alias_method :handle_response_without_instance, :handle_response
|
14
|
+
# alias_method :handle_response, :handle_response_with_instance
|
15
|
+
end
|
16
|
+
end
|
data/lib/shopify_api.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
require 'active_resource'
|
4
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
5
|
+
require 'digest/md5'
|
6
|
+
require 'base64'
|
7
|
+
require 'active_resource/connection_ext'
|
8
|
+
require 'shopify_api/limits'
|
9
|
+
|
10
|
+
module ShopifyAPI
|
11
|
+
include Limits
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'shopify_api/events'
|
15
|
+
require 'shopify_api/metafields'
|
16
|
+
require 'shopify_api/countable'
|
17
|
+
require 'shopify_api/resources'
|
18
|
+
require 'shopify_api/session'
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module ShopifyAPI
|
2
|
+
module Limits
|
3
|
+
def self.included(klass)
|
4
|
+
klass.send(:extend, ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# Takes form num_requests_executed/max_requests
|
10
|
+
# Eg: 101/3000
|
11
|
+
CREDIT_LIMIT_HEADER_PARAM = {
|
12
|
+
:global => 'http_x_shopify_api_call_limit',
|
13
|
+
:shop => 'http_x_shopify_shop_api_call_limit'
|
14
|
+
}
|
15
|
+
|
16
|
+
##
|
17
|
+
# How many more API calls can I make?
|
18
|
+
# @return {Integer}
|
19
|
+
#
|
20
|
+
def credit_left
|
21
|
+
shop = credit_limit(:shop) - credit_used(:shop)
|
22
|
+
global = credit_limit(:global) - credit_used(:global)
|
23
|
+
shop < global ? shop : global
|
24
|
+
end
|
25
|
+
alias_method :available_calls, :credit_left
|
26
|
+
|
27
|
+
##
|
28
|
+
# Have I reached my API call limit?
|
29
|
+
# @return {Boolean}
|
30
|
+
#
|
31
|
+
def credit_maxed?
|
32
|
+
credit_left <= 0
|
33
|
+
end
|
34
|
+
alias_method :maxed?, :credit_maxed?
|
35
|
+
|
36
|
+
##
|
37
|
+
# How many total API calls can I make?
|
38
|
+
# NOTE: subtracting 1 from credit_limit because I think ShopifyAPI cuts off at 299/2999 or shop/global limits.
|
39
|
+
# @param {Symbol} scope [:shop|:global]
|
40
|
+
# @return {Integer}
|
41
|
+
#
|
42
|
+
def credit_limit(scope=:shop)
|
43
|
+
@api_credit_limit ||= {}
|
44
|
+
@api_credit_limit[scope] ||= api_credit_limit_param(scope).pop.to_i - 1
|
45
|
+
end
|
46
|
+
alias_method :call_limit, :credit_limit
|
47
|
+
|
48
|
+
##
|
49
|
+
# How many API calls have I made?
|
50
|
+
# @param {Symbol} scope [:shop|:global]
|
51
|
+
# @return {Integer}
|
52
|
+
#
|
53
|
+
def credit_used(scope=:shop)
|
54
|
+
api_credit_limit_param(scope).shift.to_i
|
55
|
+
end
|
56
|
+
alias_method :call_count, :credit_used
|
57
|
+
|
58
|
+
##
|
59
|
+
# @return {HTTPResonse}
|
60
|
+
#
|
61
|
+
def response
|
62
|
+
Shop.current unless ShopifyAPI::Base.connection.response
|
63
|
+
ShopifyAPI::Base.connection.response
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
##
|
69
|
+
# @return {Array}
|
70
|
+
#
|
71
|
+
def api_credit_limit_param(scope)
|
72
|
+
response[CREDIT_LIMIT_HEADER_PARAM[scope]].split('/')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ShopifyAPI
|
2
|
+
module Metafields
|
3
|
+
def metafields
|
4
|
+
Metafield.find(:all, :params => {:resource => self.class.collection_name, :resource_id => id})
|
5
|
+
end
|
6
|
+
|
7
|
+
def add_metafield(metafield)
|
8
|
+
raise ArgumentError, "You can only add metafields to resource that has been saved" if new?
|
9
|
+
|
10
|
+
metafield.prefix_options = {
|
11
|
+
:resource => self.class.collection_name,
|
12
|
+
:resource_id => id
|
13
|
+
}
|
14
|
+
metafield.save
|
15
|
+
metafield
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'shopify_api/resources/base'
|
2
|
+
require 'shopify_api/resources/address'
|
3
|
+
require 'shopify_api/resources/application_charge'
|
4
|
+
require 'shopify_api/resources/article'
|
5
|
+
require 'shopify_api/resources/asset'
|
6
|
+
require 'shopify_api/resources/billing_address'
|
7
|
+
require 'shopify_api/resources/blog'
|
8
|
+
require 'shopify_api/resources/collect'
|
9
|
+
require 'shopify_api/resources/comment'
|
10
|
+
require 'shopify_api/resources/country'
|
11
|
+
require 'shopify_api/resources/custom_collection'
|
12
|
+
require 'shopify_api/resources/customer_group'
|
13
|
+
require 'shopify_api/resources/customer'
|
14
|
+
require 'shopify_api/resources/event'
|
15
|
+
require 'shopify_api/resources/fulfillment'
|
16
|
+
require 'shopify_api/resources/image'
|
17
|
+
require 'shopify_api/resources/line_item'
|
18
|
+
require 'shopify_api/resources/metafield'
|
19
|
+
require 'shopify_api/resources/note_attribute'
|
20
|
+
require 'shopify_api/resources/option'
|
21
|
+
require 'shopify_api/resources/order'
|
22
|
+
require 'shopify_api/resources/page'
|
23
|
+
require 'shopify_api/resources/payment_details'
|
24
|
+
require 'shopify_api/resources/product'
|
25
|
+
require 'shopify_api/resources/product_search_engine'
|
26
|
+
require 'shopify_api/resources/province'
|
27
|
+
require 'shopify_api/resources/receipt'
|
28
|
+
require 'shopify_api/resources/recurring_application_charge'
|
29
|
+
require 'shopify_api/resources/redirect'
|
30
|
+
require 'shopify_api/resources/rule'
|
31
|
+
require 'shopify_api/resources/script_tag'
|
32
|
+
require 'shopify_api/resources/shipping_address'
|
33
|
+
require 'shopify_api/resources/shipping_line'
|
34
|
+
require 'shopify_api/resources/shop'
|
35
|
+
require 'shopify_api/resources/smart_collection'
|
36
|
+
require 'shopify_api/resources/tax_line'
|
37
|
+
require 'shopify_api/resources/theme'
|
38
|
+
require 'shopify_api/resources/transaction'
|
39
|
+
require 'shopify_api/resources/variant'
|
40
|
+
require 'shopify_api/resources/webhook'
|
@@ -0,0 +1,95 @@
|
|
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
|
+
self.primary_key = 'key'
|
36
|
+
self.prefix = "/admin/themes/:theme_id/"
|
37
|
+
|
38
|
+
def self.prefix(options={})
|
39
|
+
options[:theme_id].nil? ? "/admin/" : "/admin/themes/#{options[:theme_id]}/"
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.element_path(id, prefix_options = {}, query_options = nil) #:nodoc:
|
43
|
+
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
|
44
|
+
"#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
|
45
|
+
end
|
46
|
+
|
47
|
+
# find an asset by key:
|
48
|
+
# ShopifyAPI::Asset.find('layout/theme.liquid', :params => {:theme_id => 99})
|
49
|
+
def self.find(*args)
|
50
|
+
if args[0].is_a?(Symbol)
|
51
|
+
super
|
52
|
+
else
|
53
|
+
params = {:asset => {:key => args[0]}}
|
54
|
+
params = params.merge(args[1][:params]) if args[1] && args[1][:params]
|
55
|
+
path_prefix = params[:theme_id] ? "/admin/themes/#{params[:theme_id]}" : "/admin"
|
56
|
+
find(:one, :from => "#{path_prefix}/assets.#{format.extension}", :params => params)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# For text assets, Shopify returns the data in the 'value' attribute.
|
61
|
+
# For binary assets, the data is base-64-encoded and returned in the
|
62
|
+
# 'attachment' attribute. This accessor returns the data in both cases.
|
63
|
+
def value
|
64
|
+
attributes['value'] ||
|
65
|
+
(attributes['attachment'] ? Base64.decode64(attributes['attachment']) : nil)
|
66
|
+
end
|
67
|
+
|
68
|
+
def attach(data)
|
69
|
+
self.attachment = Base64.encode64(data)
|
70
|
+
end
|
71
|
+
|
72
|
+
def destroy
|
73
|
+
connection.delete(element_path(prefix_options.merge(:asset => {:key => key})), self.class.headers)
|
74
|
+
end
|
75
|
+
|
76
|
+
def new?
|
77
|
+
false
|
78
|
+
end
|
79
|
+
|
80
|
+
def method_missing(method_symbol, *arguments) #:nodoc:
|
81
|
+
if %w{value= attachment= src= source_key=}.include?(method_symbol)
|
82
|
+
wipe_value_attributes
|
83
|
+
end
|
84
|
+
super
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def wipe_value_attributes
|
90
|
+
%w{value attachment src source_key}.each do |attr|
|
91
|
+
attributes.delete(attr)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|