class ImageBoss::Path

Constants

OPERATIONS
SERVICE_URL

Public Class Methods

new(client_options, asset_path) click to toggle source
# File lib/imageboss/path.rb, line 19
def initialize(client_options, asset_path)
  @client_options = client_options
  @service_url = client_options[:service_url] || SERVICE_URL
  @source = client_options[:source]
  @secret = client_options[:secret]
  @asset_path = asset_path
end

Public Instance Methods

operation(name, options = {}) click to toggle source
# File lib/imageboss/path.rb, line 27
def operation(name, options = {})
  return @asset_path unless @client_options[:enabled]

  @operation_name = name.to_sym
  @options = options
  @extra_options = parse_options(options[:options])
  @operation = OPERATIONS[@operation_name]
  @required = @operation[:required]

  @required.each do |r|
    @options.fetch(r)
  end

  recipe = [
    SERVICE_URL,
    @operation[:recipe].chomp('/'),
    @asset_path.gsub(/^\/?(.+)/, "\\1")
  ].join

  recipe_url = parse(recipe)
  recipe_path = parse(recipe).gsub(SERVICE_URL, '')
  @secret == false ? recipe_url : add_params(recipe_url, { bossToken: create_token(recipe_path) })
end

Private Instance Methods

add_params(url, params = {}) click to toggle source
# File lib/imageboss/path.rb, line 57
def add_params(url, params = {})
  uri = URI(url)
  params    = Hash[URI.decode_www_form(uri.query || '')].merge(params)
  uri.query =      URI.encode_www_form(params)
  uri.to_s
end
create_token(path) click to toggle source
# File lib/imageboss/path.rb, line 53
def create_token(path)
  OpenSSL::HMAC.hexdigest('sha256', @secret, path)
end
parse(recipe) click to toggle source
# File lib/imageboss/path.rb, line 64
def parse(recipe)
  recipe
    .sub(':source', @source.to_s)
    .sub(':operation', @operation_name.to_s)
    .sub(':width', @options[:width].to_s)
    .sub(':height', @options[:height].to_s)
    .sub(':options', @extra_options.empty? ? '' : "#{@extra_options}/")
    .sub('::mode', @options[:mode] ? ":#{@options[:mode]}" : '').to_s
end
parse_options(options) click to toggle source
# File lib/imageboss/path.rb, line 74
def parse_options(options)
  opts = []
  (options || {}).each_key do |k|
    options[k] = k.to_s == "wmk-path" ? CGI.escape(options[k]) : options[k]
    opts << [k.to_s, options[k] ].join(':')
  end
  opts.join(',')
end