Module: CarrierWave::Blitline

Extended by:
ActiveSupport::Concern
Defined in:
lib/carrierwave/blitline.rb,
lib/carrierwave/blitline/version.rb,
lib/carrierwave/blitline/function.rb,
lib/carrierwave/blitline/image_version.rb,
lib/carrierwave/blitline/image_version_function_presenter.rb

Overview

Extend the behaviour of CarrierWave to support Blitline services

Defined Under Namespace

Modules: ClassMethods Classes: Function, ImageVersion, ImageVersionFunctionPresenter

Constant Summary collapse

RIP_VERSION_NAMES_AT_START =

Does the version name come at the start (carrierwave default) or at the end of the filename

true
BLITLINE_VERSION =

Blitline API version

1.21
UNIQUE_IDENTIFIER_TEMPLATE =
"%{app_name}_%{rails_env}_%{token}".freeze
VERSION =
"0.3.4".freeze

Instance Method Summary collapse

Instance Method Details

#file_name_for_version(version) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/carrierwave/blitline.rb', line 123

def file_name_for_version(version)
  file_name, file_type  = filename.split('.')
  name_components       = [version.name, file_name].compact
  name_components.reverse! unless RIP_VERSION_NAMES_AT_START
  file_namewith_version = name_components.join("_") + ".#{file_type}"
  File.join(store_dir, file_namewith_version).to_s
end

#filenameObject



111
112
113
# File 'lib/carrierwave/blitline.rb', line 111

def filename
  "#{model.class.to_s.underscore}.#{file.extension}" if file
end

#functionsObject

Returns a Hash for each function included in the Blitline API post



89
90
91
92
93
# File 'lib/carrierwave/blitline.rb', line 89

def functions
  blitline_image_versions.map do |version|
    ImageVersionFunctionPresenter.new(version, self).to_hash
  end
end

#job_hashObject

Returns a Hash of params posted off to Blitline API



79
80
81
82
83
84
85
86
# File 'lib/carrierwave/blitline.rb', line 79

def job_hash
  {
    "application_id": CarrierWave::Blitline.blitline_application_id,
    "src": url,
    "v": BLITLINE_VERSION,
    "functions": functions
  }.with_indifferent_access
end

#optimize!Object

sends a request to Blitline to re-process themain image and all versions



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

def optimize!
  rip_process_images(true) if process_via_blitline?
end

#params_for_function(function_name, *args) ⇒ Object



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

def params_for_function(function_name, *args)
  send("params_for_#{function_name}", *args)
end

#params_for_no_op(*_args) ⇒ Object



135
136
137
# File 'lib/carrierwave/blitline.rb', line 135

def params_for_no_op(*_args)
  {}
end

#params_for_resize_to_fill(*args) ⇒ Object



139
140
141
142
# File 'lib/carrierwave/blitline.rb', line 139

def params_for_resize_to_fill(*args)
  args.flatten!
  { width: args.first, height: args.last }
end

#params_for_resize_to_fit(*args) ⇒ Object



144
145
146
147
# File 'lib/carrierwave/blitline.rb', line 144

def params_for_resize_to_fit(*args)
  args.flatten!
  { width: args.first, height: args.last }
end

#rip_can_begin_processing?Boolean

Can we post the images to Blitline for processing?

CarrierWave creates virtual Uploaders for each version of an image. These
versions are processed before the original, so the only way to tell if the
versions are all complete is to check the classname for the current call
and if there is no '::' it is the original class.

Returns a boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/carrierwave/blitline.rb', line 107

def rip_can_begin_processing?
  process_via_blitline? && (!self.class.name.include? "::")
end

#rip_process_images(_file) ⇒ Object

Send a request to Blitline to optimize the original file and create any required versions.

 This is called by an after_store macro and because Carrier creates virtual
 instancies for each version would be called 4 times for an image with three
 versions.

 Because we only want to do this on completion we check all the versions
 have been called by testing it is OK to begin processing

 A hash is created (job_hash) with Blitline's required commands and sent using the
 Blitline gem.

file - not used within the method, but required for the callback to function


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/carrierwave/blitline.rb', line 64

def rip_process_images(_file)
  return unless rip_can_begin_processing?
  Rails.logger.tagged("Blitline") { |l| l.debug(job_hash.to_json) }
  blitline_service.add_job_via_hash(job_hash)
  begin
    blitline_service.post_jobs
  rescue StandardError => e
    Rails.logger.tagged("Blitline") do |logger|
      logger.error format("ERROR: Blitline processing error for %<class>\n%<message>",
                          class: model.class.name, message: e.message)
    end
  end
end

#unique_identifierObject



115
116
117
118
119
120
121
# File 'lib/carrierwave/blitline.rb', line 115

def unique_identifier
  @unique_identifier ||= begin
    UNIQUE_IDENTIFIER_TEMPLATE % { app_name: Rails.application.class.name,
                                   rails_env: Rails.env,
                                   token: SecureRandom.base64(10) }
  end
end