module ShopifyTheme

Constants

CA_CERT_FILE
EXTENSIONS
FEED_URL
MAX_TIMBER_RETRY
NOOPParser
PERMIT_LOWER_LIMIT
REMOTE_CERT_FILE
TIMER_RESET
VERSION
ZIP_URL

Public Class Methods

api_usage() click to toggle source
# File lib/shopify_theme.rb, line 49
def self.api_usage
  "[API Limit: #{@@current_api_call_count || "??"}/#{@@total_api_calls || "??"}]"
end
asset_list() click to toggle source
# File lib/shopify_theme.rb, line 54
def self.asset_list
  # HTTParty parser chokes on assest listing, have it noop
  # and then use a rel JSON parser.
  response = shopify.get(path, :parser => NOOPParser)
  manage_timer(response)

  assets = JSON.parse(response.body)["assets"].collect {|a| a['key'] }
  # Remove any .css files if a .css.liquid file exists
  assets.reject{|a| assets.include?("#{a}.liquid") }
end
check_config() click to toggle source
# File lib/shopify_theme.rb, line 138
def self.check_config
  shopify.get(path).code == 200
end
config() click to toggle source
# File lib/shopify_theme.rb, line 104
def self.config
  @config ||= if File.exist? 'config.yml'
    config = YAML.load(File.read('config.yml'))
    config
  else
    puts "config.yml does not exist!" unless test?
    {}
  end
end
config=(config) click to toggle source
# File lib/shopify_theme.rb, line 114
def self.config=(config)
  @config = config
end
configureMimeMagic() click to toggle source
# File lib/shopify_theme/cli.rb, line 22
def self.configureMimeMagic
  ShopifyTheme::EXTENSIONS.each do |extension|
    MimeMagic.add(extension.delete(:mimetype), extension)
  end
end
critical_permits?() click to toggle source
# File lib/shopify_theme.rb, line 26
def self.critical_permits?
  @@total_api_calls.to_i - @@current_api_call_count.to_i < PERMIT_LOWER_LIMIT
end
delete_asset(asset) click to toggle source
# File lib/shopify_theme.rb, line 81
def self.delete_asset(asset)
  response = shopify.delete(path, :body =>{:asset => {:key => asset}})
  manage_timer(response)
  response
end
delta_seconds() click to toggle source
# File lib/shopify_theme.rb, line 34
def self.delta_seconds
  Time.now.to_i - @@current_timer.to_i
end
get_asset(asset) click to toggle source
# File lib/shopify_theme.rb, line 65
def self.get_asset(asset)
  response = shopify.get(path, :query =>{:asset => {:key => asset}}, :parser => NOOPParser)
  manage_timer(response)

  # HTTParty json parsing is broken?
  asset = response.code == 200 ? JSON.parse(response.body)["asset"] : {}
  asset['response'] = response
  asset
end
get_index() click to toggle source
# File lib/shopify_theme.rb, line 142
def self.get_index
  shopify.get(path)
end
ignore_files() click to toggle source
# File lib/shopify_theme.rb, line 122
def self.ignore_files
  (config[:ignore_files] || []).compact
end
is_binary_data?(string) click to toggle source
# File lib/shopify_theme.rb, line 130
def self.is_binary_data?(string)
  if string.respond_to?(:encoding)
    string.encoding == "US-ASCII"
  else
    ( string.count( "^ -~", "^\r\n" ).fdiv(string.size) > 0.3 || string.index( "\x00" ) ) unless string.empty?
  end
end
manage_timer(response) click to toggle source
# File lib/shopify_theme.rb, line 20
def self.manage_timer(response)
  return unless response.headers['x-shopify-shop-api-call-limit']
  @@current_api_call_count, @@total_api_calls = response.headers['x-shopify-shop-api-call-limit'].split('/')
  @@current_timer = Time.now if @current_timer.nil?
end
needs_sleep?() click to toggle source
# File lib/shopify_theme.rb, line 38
def self.needs_sleep?
  critical_permits? && !passed_api_refresh?
end
passed_api_refresh?() click to toggle source
# File lib/shopify_theme.rb, line 30
def self.passed_api_refresh?
  delta_seconds > TIMER_RESET
end
path() click to toggle source
# File lib/shopify_theme.rb, line 118
def self.path
  @path ||= config[:theme_id] ? "/admin/themes/#{config[:theme_id]}/assets.json" : "/admin/assets.json"
end
send_asset(data) click to toggle source
# File lib/shopify_theme.rb, line 75
def self.send_asset(data)
  response = shopify.put(path, :body =>{:asset => data})
  manage_timer(response)
  response
end
sleep() click to toggle source
# File lib/shopify_theme.rb, line 42
def self.sleep
  if needs_sleep?
    Kernel.sleep(TIMER_RESET - delta_seconds)
    @current_timer = nil
  end
end
test?() click to toggle source
# File lib/shopify_theme.rb, line 16
def self.test?
  ENV['test']
end
upload_timber(name, version, retries=0) click to toggle source
# File lib/shopify_theme.rb, line 87
def self.upload_timber(name, version, retries=0)
  release = Releases.new.find(version)
  response = shopify.post("/admin/themes.json", :body => {:theme => {:name => name, :src => release.zip_url, :role => 'unpublished'}})
  manage_timer(response)
  body = JSON.parse(response.body)
  if theme = body['theme']
    puts "Successfully created #{name} using Shopify Timber #{version}"
    watch_until_processing_complete(theme)
  elsif retries < MAX_TIMBER_RETRY
    upload_timber(name, version, retries + 1)
  else
    puts "Could not download theme!"
    puts body
    exit 1
  end
end
whitelist_files() click to toggle source
# File lib/shopify_theme.rb, line 126
def self.whitelist_files
  (config[:whitelist_files] || []).compact
end

Private Class Methods

shopify() click to toggle source
# File lib/shopify_theme.rb, line 147
def self.shopify
  headers 'X-Shopify-Access-Token' => config[:password] || config[:access_token]
  base_uri "https://#{config[:store]}"
  ShopifyTheme
end
watch_until_processing_complete(theme) click to toggle source
# File lib/shopify_theme.rb, line 153
def self.watch_until_processing_complete(theme)
  count = 0
  while true do
    Kernel.sleep(count)
    response = shopify.get("/admin/themes/#{theme['id']}.json")
    theme = JSON.parse(response.body)['theme']
    return theme if theme['previewable']
    count += 5
  end
end