Sha256: 10da3b9be18d9c141aaa27ffece7cea7e41ce64a1a92412b54ecf7bf0bbede66

Contents?: true

Size: 940 Bytes

Versions: 1

Compression:

Stored size: 940 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 '_'

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

      uri.query = build_query params
      uri.to_s
    end

    def string_to_tokenize(uri)
      query = parse_query_string(uri)
      return query['chname'] if query.include?('chname')

      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

1 entries across 1 versions & 1 rubygems

Version Path
url_tokenizer-1.4.1 lib/url_tokenizer/fastly_query_string.rb