Module: CamoImageTag

Extended by:
ActiveSupport::Concern
Defined in:
lib/camo_image_tag.rb,
lib/camo_image_tag/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#camo_configObject



28
29
30
31
# File 'lib/camo_image_tag.rb', line 28

def camo_config
  { 'key'  => ENV['CAMO_KEY']  || "0x24FEEDFACEDEADBEEFCAFE",
    'host' => ENV['CAMO_HOST'] || "http://localhost:8081" }
end

#camo_uri(image_url) ⇒ Object



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

def camo_uri(image_url)
  hexdigest = OpenSSL::HMAC.hexdigest(
    OpenSSL::Digest::Digest.new('sha1'), camo_config['key'], image_url)
  encoded_image_url = image_url.to_enum(:each_byte).map { |byte| "%02x" % byte }.join
  "#{camo_config['host']}/#{hexdigest}/#{encoded_image_url}"
end

#image_tag_with_camo(source, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/camo_image_tag.rb', line 11

def image_tag_with_camo(source, options={})
  if source && source =~ /^http:/
    unless source =~ /^(?:cid|data):/ || source.blank?
      options[:alt] = options.fetch(:alt){ image_alt(source) }
    end
    source = camo_uri(source)
  end
  image_tag_without_camo(source, options)
end