Module: ShopifyTheme

Includes:
HTTParty
Defined in:
lib/shopify_theme.rb,
lib/shopify_theme/cli.rb,
lib/shopify_theme/version.rb

Defined Under Namespace

Classes: Cli

Constant Summary collapse

NOOPParser =
Proc.new {|data, format| {} }
TIMER_RESET =
10
PERMIT_LOWER_LIMIT =
3
TIMBER_ZIP =
"https://github.com/Shopify/Timber/archive/%s.zip"
LAST_KNOWN_STABLE =
"v1.1.0"
EXTENSIONS =
{mimetype: 'application/x-liquid', extensions: %w(liquid), parents: 'text/plain'},
{mimetype: 'application/json', extensions: %w(json), parents: 'text/plain'},
{mimetype: 'application/js', extensions: %w(map), parents: 'text/plain'},
{mimetype: 'application/vnd.ms-fontobject', extensions: %w(eot)},
{mimetype: 'image/svg+xml', extensions: %w(svg svgz)}
VERSION =
"0.0.21"
@@current_api_call_count =
0
@@total_api_calls =
40

Class Method Summary collapse

Class Method Details

.api_usageObject



46
47
48
# File 'lib/shopify_theme.rb', line 46

def self.api_usage
  "[API Limit: #{@@current_api_call_count || "??"}/#{@@total_api_calls || "??"}]"
end

.asset_listObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/shopify_theme.rb', line 51

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_configObject



133
134
135
# File 'lib/shopify_theme.rb', line 133

def self.check_config
  shopify.get(path).code == 200
end

.configObject



99
100
101
102
103
104
105
106
107
# File 'lib/shopify_theme.rb', line 99

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) ⇒ Object



109
110
111
# File 'lib/shopify_theme.rb', line 109

def self.config=(config)
  @config = config
end

.configureMimeMagicObject



21
22
23
24
25
# File 'lib/shopify_theme/cli.rb', line 21

def self.configureMimeMagic
  ShopifyTheme::EXTENSIONS.each do |extension|
    MimeMagic.add(extension.delete(:mimetype), extension)
  end
end

.critical_permits?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/shopify_theme.rb', line 23

def self.critical_permits?
  @@total_api_calls.to_i - @@current_api_call_count.to_i < PERMIT_LOWER_LIMIT
end

.delete_asset(asset) ⇒ Object



78
79
80
81
82
# File 'lib/shopify_theme.rb', line 78

def self.delete_asset(asset)
  response = shopify.delete(path, :body =>{:asset => {:key => asset}})
  manage_timer(response)
  response
end

.delta_secondsObject



31
32
33
# File 'lib/shopify_theme.rb', line 31

def self.delta_seconds
  Time.now.to_i - @@current_timer.to_i
end

.get_asset(asset) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/shopify_theme.rb', line 62

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

.ignore_filesObject



117
118
119
# File 'lib/shopify_theme.rb', line 117

def self.ignore_files
  (config[:ignore_files] || []).compact.map { |r| Regexp.new(r) }
end

.is_binary_data?(string) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/shopify_theme.rb', line 125

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) ⇒ Object



17
18
19
20
21
# File 'lib/shopify_theme.rb', line 17

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?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/shopify_theme.rb', line 35

def self.needs_sleep?
  critical_permits? && !passed_api_refresh?
end

.passed_api_refresh?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/shopify_theme.rb', line 27

def self.passed_api_refresh?
  delta_seconds > TIMER_RESET
end

.pathObject



113
114
115
# File 'lib/shopify_theme.rb', line 113

def self.path
  @path ||= config[:theme_id] ? "/admin/themes/#{config[:theme_id]}/assets.json" : "/admin/assets.json"
end

.send_asset(data) ⇒ Object



72
73
74
75
76
# File 'lib/shopify_theme.rb', line 72

def self.send_asset(data)
  response = shopify.put(path, :body =>{:asset => data})
  manage_timer(response)
  response
end

.sleepObject



39
40
41
42
43
44
# File 'lib/shopify_theme.rb', line 39

def self.sleep
  if needs_sleep?
    Kernel.sleep(TIMER_RESET - delta_seconds)
    @current_timer = nil
  end
end

.test?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/shopify_theme.rb', line 13

def self.test?
  ENV['test']
end

.upload_timber(name, master) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/shopify_theme.rb', line 84

def self.upload_timber(name, master)
  source = TIMBER_ZIP % (master ? 'master' : LAST_KNOWN_STABLE)
  puts master ? "Using latest build from shopify" : "Using last known stable build -- #{LAST_KNOWN_STABLE}"
  response = shopify.post("/admin/themes.json", :body => {:theme => {:name => name, :src => source, :role => 'unpublished'}})
  manage_timer(response)
  body = JSON.parse(response.body)
  if theme = body['theme']
    watch_until_processing_complete(theme)
  else
    puts "Could not download theme!"
    puts body
    exit 1
  end
end

.whitelist_filesObject



121
122
123
# File 'lib/shopify_theme.rb', line 121

def self.whitelist_files
  (config[:whitelist_files] || []).compact
end