Module: Glib::DynamicImagesHelper

Defined in:
app/helpers/glib/dynamic_images_helper.rb

Defined Under Namespace

Classes: EncryptionService

Instance Method Summary collapse

Instance Method Details

#dynamic_image_url(blob_key, w: 100, h: 100, fit: 'clip', bucket: Rails.application.config.try(:aws_s3_bucket)) ⇒ Object

uri.to_s end



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/glib/dynamic_images_helper.rb', line 16

def dynamic_image_url(blob_key, w: 100, h: 100, fit: 'clip', bucket: Rails.application.config.try(:aws_s3_bucket))
  return unless blob_key.present?

  full_params_hash = {
    bucket_name: bucket,
    blob_key: blob_key,
    w: w,
    h: h,
    fit: fit,
    expires: 1.hours.from_now.to_i
  }
  request_params_hash = full_params_hash.except(:bucket_name, :blob_key)

  if (private_key = ENV['PRIVATE_API_KEY'])
    encryption_service = EncryptionService.new(bucket, private_key)
    signature = encryption_service.encrypt(full_params_hash)
    request_params_hash = request_params_hash.merge(signature: signature)
  end

  # TODO: This should probably be set as a parameter for json_libs
  uri = URI::HTTPS.build(
    host: 'imageserver-demo.herokuapp.com',
    path: "/image/#{bucket}/#{blob_key}",
    query: request_params_hash.to_param
  )

  uri.to_s
end