Sha256: 7ebad8b50f283ff69c4d901d3bd5abb472489ea0bcd4d2e52b8b3d49cc36d453

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require 'base64'
require 'openssl'
require 'kanoko/errors'

module Kanoko
  class Configure
    attr_accessor :digest_func, :secret_key, :hash_proc

    # kanoko_host expect String
    # digest_func expect String
    # secret_key expect String
    # hash_proc expect Proc
    #
    # example:
    #   Kanoko.configure.tap do |c|
    #     c.kanoko_host = "http://example.com"
    #     c.digest_func = "sha1"
    #     c.secret_key = "secret"
    #   end
    #   Kanoko.url_for(:resize, "100x100") #=> "http://example.com/.../.../..."
    def initialize
      @kanoko_host = nil
      @digest_func = nil
      @secret_key = nil
      @hash_proc = ->(*args){
        if @digest_func.nil? || @secret_key.nil?
          fail ConfigureError, "`digest_func' and `secret_key' must be set"
        end
        Base64.urlsafe_encode64(
          OpenSSL::HMAC.digest @digest_func,
          @secret_key,
          args.map(&:to_s).join(',')
        )
      }
    end

    def kanoko_host=(host)
      @kanoko_host = normalize_url(host)
    end

    def kanoko_host
      @kanoko_host
    end

    private

    def normalize_url(host)
      case host
      when %r{\Ahttps?://}
        host
      when %r{\A[^/]}
        "http://#{host}"
      when %r{\A//}
        "http:#{host}"
      else
        fail ConfigureError, "invalid kanoko_host `#{host}'"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kanoko-0.0.2 lib/kanoko/configure.rb