module PosthavenTheme

Constants

DEFAULT_API_ENDPOINT
EXTENSIONS
PERMIT_LOWER_LIMIT
TIMER_RESET
VERSION

Public Class Methods

api_usage() click to toggle source
# File lib/posthaven_theme.rb, line 84
def self.api_usage
  "[API Limit: #{@@current_api_call_count || "??"}/#{@@total_api_calls || "??"}]"
end
asset_list() click to toggle source
# File lib/posthaven_theme.rb, line 96
def self.asset_list
  handle_response(get(assets_path))
end
asset_path(path) click to toggle source
# File lib/posthaven_theme.rb, line 131
def self.asset_path(path)
  theme_path + "/asset.json?path=#{path}"
end
assets_path() click to toggle source
# File lib/posthaven_theme.rb, line 128
def self.assets_path
  theme_path + "/assets.json"
end
config() click to toggle source
# File lib/posthaven_theme.rb, line 112
def self.config
  @config
end
config=(config) click to toggle source
# File lib/posthaven_theme.rb, line 116
def self.config=(config)
  @config = config && Hash[config.map { |k, v| [k.to_sym, v] }]
  setup
end
configure_mime_magic() click to toggle source
# File lib/posthaven_theme/cli.rb, line 20
def self.configure_mime_magic
  PosthavenTheme::EXTENSIONS.each do |extension|
    MimeMagic.add(extension.delete(:mimetype), extension)
  end
end
create_theme(data) click to toggle source
# File lib/posthaven_theme.rb, line 92
def self.create_theme(data)
  handle_response(post(themes_path, body: {theme: data}))
end
critical_permits?() click to toggle source
# File lib/posthaven_theme.rb, line 61
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/posthaven_theme.rb, line 108
def self.delete_asset(asset)
  handle_response(delete(asset_path(asset)))
end
delta_seconds() click to toggle source
# File lib/posthaven_theme.rb, line 69
def self.delta_seconds
  Time.now.to_i - @@current_timer.to_i
end
get_asset(asset) click to toggle source
# File lib/posthaven_theme.rb, line 100
def self.get_asset(asset)
  handle_response(get(asset_path(asset)))
end
ignore_files() click to toggle source
# File lib/posthaven_theme.rb, line 135
def self.ignore_files
  (config[:ignore_files] || []).compact.map { |r| Regexp.new(r) }
end
is_binary_data?(string) click to toggle source
# File lib/posthaven_theme.rb, line 143
def self.is_binary_data?(string)
  if string.respond_to?(:encoding)
    string.encoding == "US-ASCII"
  else
    unless string.empty?
      (string.count("^ -~", "^\r\n").fdiv(string.size) > 0.3 || string.index("\x00"))
    end
  end
end
manage_timer(response) click to toggle source
# File lib/posthaven_theme.rb, line 54
def self.manage_timer(response)
  return unless response.headers['x-posthaven-api-call-limit']
  @@current_api_call_count, @@total_api_calls = response.headers['x-posthaven-api-call-limit']
                                                        .split('/')
  @@current_timer = Time.now if @current_timer.nil?
end
needs_sleep?() click to toggle source
# File lib/posthaven_theme.rb, line 73
def self.needs_sleep?
  critical_permits? && !passed_api_refresh?
end
passed_api_refresh?() click to toggle source
# File lib/posthaven_theme.rb, line 65
def self.passed_api_refresh?
  delta_seconds > TIMER_RESET
end
send_asset(data) click to toggle source
# File lib/posthaven_theme.rb, line 104
def self.send_asset(data)
  handle_response(put(asset_path(data[:path]), body: {asset: data}))
end
sleep() click to toggle source
# File lib/posthaven_theme.rb, line 77
def self.sleep
  if needs_sleep?
    Kernel.sleep(TIMER_RESET - delta_seconds)
    @current_timer = nil
  end
end
test?() click to toggle source
# File lib/posthaven_theme.rb, line 50
def self.test?
  ENV['test']
end
theme_list() click to toggle source
# File lib/posthaven_theme.rb, line 88
def self.theme_list
  handle_response(get(themes_path))
end
theme_path(theme_id = config[:theme_id]) click to toggle source
# File lib/posthaven_theme.rb, line 124
def self.theme_path(theme_id = config[:theme_id])
  "/themes/#{theme_id}"
end
themes_path() click to toggle source
# File lib/posthaven_theme.rb, line 121
def self.themes_path
  '/themes.json'
end
whitelist_files() click to toggle source
# File lib/posthaven_theme.rb, line 139
def self.whitelist_files
  (config[:whitelist_files] || []).compact
end

Private Class Methods

handle_response(response) click to toggle source
# File lib/posthaven_theme.rb, line 155
def self.handle_response(response)
  manage_timer(response)

  if response.success?
    if response.parsed_response
      response.parsed_response["data"]
    else
      response
    end
  else
    raise APIError.new(response)
  end
end
setup() click to toggle source
# File lib/posthaven_theme.rb, line 169
def self.setup
  # Basics
  headers 'Authorization' => "Token #{config[:api_key]}"
  base_uri (config[:api_endpoint] || ENV['PHTHEME_ENDPOINT'] || DEFAULT_API_ENDPOINT)

  # Dev
  require 'resolv-replace'  if base_uri.include?(':')

  debug_output $stdout  if config[:debug] || ENV['PHTHEME_DEBUG']
  default_options.update(verify: false)  if ENV['PHTHEME_IGNORE_SSL_VERIFY']
end