class Picfit::Client

Constants

PICFIT_PARAMS_MAP

Public Class Methods

new(options={}) click to toggle source
# File lib/picfit/client.rb, line 20
def initialize(options={})
  options = Picfit.options.merge(options)
  Configuration::VALID_OPTIONS_KEYS.each do |key|
    send("#{key}=", options[key])
  end
end

Public Instance Methods

config() click to toggle source
# File lib/picfit/client.rb, line 27
def config
  conf = {}
  Configuration::VALID_OPTIONS_KEYS.each do |key|
    conf[key] = send key
  end
  conf
end
image_path(options = {}) click to toggle source

Return the image path (not include the host)

# File lib/picfit/client.rb, line 47
def image_path(options = {})
  path = (options[:method] || self.method).to_s
  secret_key = options[:secret_key] || self.secret_key

  # lets convert the options to picfit params
  params = PICFIT_PARAMS_MAP.inject({}) do |result, (key, param_key)|
    result[param_key] = options[key] if options[key]
    result
  end

  if options[:query_string]
    # Create a query params array from the params
    query_string = params.map{|k,v| [CGI.escape(k.to_s), "=", CGI.escape(v.to_s)]}.map(&:join).join("&")
    if secret_key
      # lets use << instead of + or interpolation
      query_string << "&sig="
      query_string << sign_string(secret_key, query_string)
    end
  else
    path << ["", params[:op], [params[:w].to_s, params[:h].to_s].join('x'), params[:path]].join("/")
  end

  if query_string
    path << "?"
    path << query_string
  end

  path
end
image_url(options = {}) click to toggle source

Return a full image url

# File lib/picfit/client.rb, line 38
def image_url(options = {})
  base_url = options[:base_url] || self.base_url || Configuration.DEFAULT_BASE_URL
  base_url << "/" if base_url[-1,1] != "/"
  base_url << image_path(options)
end
sha_digest() click to toggle source
# File lib/picfit/client.rb, line 83
def sha_digest
  @sha_digest ||= OpenSSL::Digest::SHA1.new
end
sign_string(secret_key, query_string) click to toggle source
# File lib/picfit/client.rb, line 77
def sign_string(secret_key, query_string)
  hmac = OpenSSL::HMAC.new(secret_key, sha_digest)
  hmac << query_string
  hmac.hexdigest
end