Class: ImageBoss::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/imageboss/path.rb

Constant Summary collapse

SERVICE_URL =
'https://img.imageboss.me'.freeze
OPERATIONS =
{
  cover: {
    recipe: '/:source/:operation::mode/:widthx:height/:options/', required: [:width, :height]
  },
  width: {
    recipe: '/:source/:operation/:width/:options/', required: [:width]
  },
  height: { recipe: '/:source/:operation/:height/:options/', required: [:height]},
  cdn: { recipe: '/:source/:operation/:options/', required: [] }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(client_options, asset_path) ⇒ Path

Returns a new instance of Path.



19
20
21
22
23
24
25
# 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

Instance Method Details

#operation(name, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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