Sha256: f053be3fb295a7fe2549c79d5e3b899cb573f22c4b616103dd176ff86b530dd2

Contents?: true

Size: 1001 Bytes

Versions: 4

Compression:

Stored size: 1001 Bytes

Contents

require_relative 'fastly'
require 'rack'

module UrlTokenizer
  class FastlyQueryString < Fastly
    def call(input_url, **options)
      options = global_options.merge options
      uri = URI.parse input_url
      path = uri.path
      return if path.empty? || path == '/'

      expiration = expiration_date(options[:expires_in])

      token = digest [string_to_tokenize(uri), expiration].compact.join
      token = [expiration, token].compact.join '_'
      token = "token=#{token}"

      params = parse_query_string(uri).merge(cdntoken: token)

      uri.query = build_query params
      uri.to_s
    end

    def string_to_tokenize(uri)
      query = parse_query_string(uri)
      return uri.path[0..-(File.extname(uri.path).length + 1)] if query.key?('pub')

      File.dirname(uri.path)
    end

    private

    def parse_query_string(uri)
      return {} if uri.query.nil?

      Rack::Utils.parse_query(uri.query).tap do |params|
        params.delete('token')
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
url_tokenizer-1.7.0 lib/url_tokenizer/fastly_query_string.rb
url_tokenizer-1.6.0 lib/url_tokenizer/fastly_query_string.rb
url_tokenizer-1.5.0 lib/url_tokenizer/fastly_query_string.rb
url_tokenizer-1.4.3 lib/url_tokenizer/fastly_query_string.rb