Sha256: 0963b07d008a97fa7d3b96389d2807b6601427b08baed2bfaa360ade6a7d5daf

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module ImageBoss
  class Path
    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

    def initialize(client_options, asset_path)
      @client_options = client_options
      @service_url = client_options[:service_url] || SERVICE_URL
      @source = client_options[:source]
      @asset_path = asset_path
    end

    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
      parse(recipe)
    end
    private

    def parse(recipe)
      recipe
        .sub(':source', @source.to_s)
        .sub(':operation', @operation_name.to_s)
        .sub(':width', @options[:width].to_s)
        .sub(':height', @options[:height].to_s)
        .sub(':options', @extra_options.empty? ? '' : "#{@extra_options}/")
        .sub('::mode', @options[:mode] ? ":#{@options[:mode]}" : '').to_s
    end

    def parse_options(options)
      opts = []
      (options || {}).each_key do |k|
        opts << [k.to_s, options[k] ].join(':')
      end
      opts.join(',')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imageboss-rb-2.0.0 lib/imageboss/path.rb