Module: PosthavenTheme

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

Defined Under Namespace

Classes: APIError, Cli

Constant Summary collapse

TIMER_RESET =
10
PERMIT_LOWER_LIMIT =
3
DEFAULT_API_ENDPOINT =
'https://api.posthaven.com/v1'
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.1.1'
@@current_api_call_count =
0
@@total_api_calls =
40

Class Method Summary collapse

Class Method Details

.api_usageObject



84
85
86
# File 'lib/posthaven_theme.rb', line 84

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

.asset_listObject



96
97
98
# File 'lib/posthaven_theme.rb', line 96

def self.asset_list
  handle_response(get(assets_path))
end

.asset_path(path) ⇒ Object



131
132
133
# File 'lib/posthaven_theme.rb', line 131

def self.asset_path(path)
  theme_path + "/asset.json?path=#{path}"
end

.assets_pathObject



128
129
130
# File 'lib/posthaven_theme.rb', line 128

def self.assets_path
  theme_path + "/assets.json"
end

.configObject



112
113
114
# File 'lib/posthaven_theme.rb', line 112

def self.config
  @config
end

.config=(config) ⇒ Object



116
117
118
119
# 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_magicObject



20
21
22
23
24
# 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) ⇒ Object



92
93
94
# File 'lib/posthaven_theme.rb', line 92

def self.create_theme(data)
  handle_response(post(themes_path, body: {theme: data}))
end

.critical_permits?Boolean

Returns:

  • (Boolean)


61
62
63
# 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) ⇒ Object



108
109
110
# File 'lib/posthaven_theme.rb', line 108

def self.delete_asset(asset)
  handle_response(delete(asset_path(asset)))
end

.delta_secondsObject



69
70
71
# File 'lib/posthaven_theme.rb', line 69

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

.get_asset(asset) ⇒ Object



100
101
102
# File 'lib/posthaven_theme.rb', line 100

def self.get_asset(asset)
  handle_response(get(asset_path(asset)))
end

.ignore_filesObject



135
136
137
# 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) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
# 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) ⇒ Object



54
55
56
57
58
59
# 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?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/posthaven_theme.rb', line 73

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

.passed_api_refresh?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/posthaven_theme.rb', line 65

def self.passed_api_refresh?
  delta_seconds > TIMER_RESET
end

.send_asset(data) ⇒ Object



104
105
106
# File 'lib/posthaven_theme.rb', line 104

def self.send_asset(data)
  handle_response(put(asset_path(data[:path]), body: {asset: data}))
end

.sleepObject



77
78
79
80
81
82
# 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?Boolean

Returns:

  • (Boolean)


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

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

.theme_listObject



88
89
90
# File 'lib/posthaven_theme.rb', line 88

def self.theme_list
  handle_response(get(themes_path))
end

.theme_path(theme_id = ) ⇒ Object



124
125
126
# File 'lib/posthaven_theme.rb', line 124

def self.theme_path(theme_id = config[:theme_id])
  "/themes/#{theme_id}"
end

.themes_pathObject



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

def self.themes_path
  '/themes.json'
end

.whitelist_filesObject



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

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