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 =
5 * 60 + 5
- PERMIT_LOWER_LIMIT =
10- VERSION =
"0.0.15"- @@current_api_call_count =
0- @@total_api_calls =
40
Class Method Summary collapse
- .asset_list ⇒ Object
- .check_config ⇒ Object
- .config ⇒ Object
- .critical_permits? ⇒ Boolean
- .delete_asset(asset) ⇒ Object
- .delta_seconds ⇒ Object
- .get_asset(asset) ⇒ Object
- .ignore_files ⇒ Object
- .is_binary_data?(string) ⇒ Boolean
- .manage_timer(response) ⇒ Object
- .needs_sleep? ⇒ Boolean
- .passed_api_refresh? ⇒ Boolean
- .path ⇒ Object
- .send_asset(data) ⇒ Object
- .sleep ⇒ Object
- .test? ⇒ Boolean
- .whitelist_files ⇒ Object
Class Method Details
.asset_list ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/shopify_theme.rb', line 44 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 ⇒ Object
108 109 110 |
# File 'lib/shopify_theme.rb', line 108 def self.check_config shopify.get(path).code == 200 end |
.config ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/shopify_theme.rb', line 77 def self.config @config ||= if File.exist? 'config.yml' config = YAML.load(File.read('config.yml')) puts ":ignore_files: is deprecated for a white list, use :whitelist_files: instead" if config[:ignore_files] && !test? config else puts "config.yml does not exist!" unless test? {} end end |
.critical_permits? ⇒ Boolean
20 21 22 |
# File 'lib/shopify_theme.rb', line 20 def self.critical_permits? @@total_api_calls.to_i - @@current_api_call_count.to_i < PERMIT_LOWER_LIMIT end |
.delete_asset(asset) ⇒ Object
71 72 73 74 75 |
# File 'lib/shopify_theme.rb', line 71 def self.delete_asset(asset) response = shopify.delete(path, :body =>{:asset => {:key => asset}}) manage_timer(response) response end |
.delta_seconds ⇒ Object
28 29 30 |
# File 'lib/shopify_theme.rb', line 28 def self.delta_seconds Time.now.to_i - @@current_timer.to_i end |
.get_asset(asset) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/shopify_theme.rb', line 55 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_files ⇒ Object
92 93 94 |
# File 'lib/shopify_theme.rb', line 92 def self.ignore_files @ignore_files ||= (config[:ignore_files] || []).compact.map { |r| Regexp.new(r) } end |
.is_binary_data?(string) ⇒ Boolean
100 101 102 103 104 105 106 |
# File 'lib/shopify_theme.rb', line 100 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
15 16 17 18 |
# File 'lib/shopify_theme.rb', line 15 def self.manage_timer(response) @@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
32 33 34 |
# File 'lib/shopify_theme.rb', line 32 def self.needs_sleep? critical_permits? && !passed_api_refresh? end |
.passed_api_refresh? ⇒ Boolean
24 25 26 |
# File 'lib/shopify_theme.rb', line 24 def self.passed_api_refresh? delta_seconds > TIMER_RESET end |
.path ⇒ Object
88 89 90 |
# File 'lib/shopify_theme.rb', line 88 def self.path @path ||= config[:theme_id] ? "/admin/themes/#{config[:theme_id]}/assets.json" : "/admin/assets.json" end |
.send_asset(data) ⇒ Object
65 66 67 68 69 |
# File 'lib/shopify_theme.rb', line 65 def self.send_asset(data) response = shopify.put(path, :body =>{:asset => data}) manage_timer(response) response end |
.sleep ⇒ Object
36 37 38 39 40 41 |
# File 'lib/shopify_theme.rb', line 36 def self.sleep if needs_sleep? Kernel.sleep(TIMER_RESET - delta_seconds) @current_timer = nil end end |
.test? ⇒ Boolean
11 12 13 |
# File 'lib/shopify_theme.rb', line 11 def self.test? ENV['test'] end |
.whitelist_files ⇒ Object
96 97 98 |
# File 'lib/shopify_theme.rb', line 96 def self.whitelist_files @whitelist_files ||= (config[:whitelist_files] || []).compact end |