Module: HaravanTheme

Includes:
HTTParty
Defined in:
lib/haravan_theme.rb,
lib/haravan_theme/cli.rb,
lib/haravan_theme/version.rb,
lib/haravan_theme/releases.rb,
lib/haravan_theme/api_checker.rb,
lib/haravan_theme/file_filters.rb,
lib/haravan_theme/filters/blacklist.rb,
lib/haravan_theme/filters/whitelist.rb,
lib/haravan_theme/filters/command_input.rb

Defined Under Namespace

Modules: Filters Classes: APIChecker, Cli, FileFilters, Releases

Constant Summary collapse

REMOTE_CERT_FILE =
'http://curl.haxx.se/ca/cacert.pem'
CA_CERT_FILE =
File.expand_path(File.dirname(__FILE__) + '/certs/cacert.pem')
NOOPParser =
Proc.new {|data, format| {} }
TIMER_RESET =
10
PERMIT_LOWER_LIMIT =
3
MAX_TIMBER_RETRY =
5
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.25"
FEED_URL =
'https://github.com/Haravan/Timber/releases.atom'
ZIP_URL =
'https://github.com/Haravan/Timber/archive/%s.zip'
@@current_api_call_count =
0
@@total_api_calls =
40

Class Method Summary collapse

Class Method Details

.api_usageObject



50
51
52
# File 'lib/haravan_theme.rb', line 50

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

.asset_listObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/haravan_theme.rb', line 55

def self.asset_list
  # HTTParty parser chokes on assest listing, have it noop
  # and then use a rel JSON parser.
  response = haravan.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



139
140
141
# File 'lib/haravan_theme.rb', line 139

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

.configObject



105
106
107
108
109
110
111
112
113
# File 'lib/haravan_theme.rb', line 105

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



115
116
117
# File 'lib/haravan_theme.rb', line 115

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

.configureMimeMagicObject



22
23
24
25
26
# File 'lib/haravan_theme/cli.rb', line 22

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

.critical_permits?Boolean

Returns:

  • (Boolean)


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

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

.delete_asset(asset) ⇒ Object



82
83
84
85
86
# File 'lib/haravan_theme.rb', line 82

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

.delta_secondsObject



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

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

.get_asset(asset) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/haravan_theme.rb', line 66

def self.get_asset(asset)
  response = haravan.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_indexObject



143
144
145
146
# File 'lib/haravan_theme.rb', line 143

def self.get_index
  puts(config[:theme_id] ? '' : "Hãy nhập theme_id")
  haravan.get(path)
end

.ignore_filesObject



123
124
125
# File 'lib/haravan_theme.rb', line 123

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

.is_binary_data?(string) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
# File 'lib/haravan_theme.rb', line 131

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



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

def self.manage_timer(response)
  return unless response.headers['x-haravan-shop-api-call-limit']
  @@current_api_call_count, @@total_api_calls = response.headers['x-haravan-shop-api-call-limit'].split('/')
  @@current_timer = Time.now if @current_timer.nil?
end

.needs_sleep?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/haravan_theme.rb', line 39

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

.passed_api_refresh?Boolean

Returns:

  • (Boolean)


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

def self.passed_api_refresh?
  delta_seconds > TIMER_RESET
end

.pathObject



119
120
121
# File 'lib/haravan_theme.rb', line 119

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

.send_asset(data) ⇒ Object



76
77
78
79
80
# File 'lib/haravan_theme.rb', line 76

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

.sleepObject



43
44
45
46
47
48
# File 'lib/haravan_theme.rb', line 43

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

.test?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/haravan_theme.rb', line 17

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

.upload_timber(name, version, retries = 0) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/haravan_theme.rb', line 88

def self.upload_timber(name, version, retries=0)
  release = Releases.new.find(version)
  response = haravan.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 Haravan 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_filesObject



127
128
129
# File 'lib/haravan_theme.rb', line 127

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