Sha256: 663ee486dc63778609f84ae96d5508a0fe9b081432f30977bea1c39361e9b1e3

Contents?: true

Size: 1.59 KB

Versions: 14

Compression:

Stored size: 1.59 KB

Contents

require 'uri'
require 'cgi'
require 'digest'
require_relative 'provider'
require 'facets/hash/symbolize_keys'

module UrlTokenizer
  class Limelight < Provider
    PARAMS = %i[e h p cf cd]

    def call(input_url, **options)
      options = global_options.merge options
      uri = URI.parse input_url
      folder = File.dirname input_url

      provider_options = build_options options, url: folder, uri: uri

      query_string = URI.encode_www_form provider_options
      url_to_encode = [folder, query_string].join '?'

      provider_options[:h] = digest url_to_encode

      uri.query = URI.encode_www_form provider_options
      uri.to_s
    end

    private
    def build_options(token_options, url:, uri:)
      server_params = {
        p: url.length,
        e: expiration_date(token_options[:expires_in]),
      }.delete_if { |k, v| v.nil? }

      cookie_params = {
        cd: token_options[:cd],
        cf: expiration_date(token_options[:cf]),
      }.delete_if { |k, v| v.nil? }

      token_options.delete :ip if global_options[:ip] == false
      filtering_params = {
        ip: token_options[:ip]
      }.delete_if { |k, v| v.nil? }

      server_params.delete :e if cookie_params.any?

      url_params(uri)
        .merge(server_params)
        .merge(cookie_params)
        .merge(filtering_params)
    end

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

      CGI.parse(uri.query).symbolize_keys.tap do |params|
        PARAMS.each do |param|
          params.delete param
        end
      end
    end

    def digest(url)
      Digest::MD5.hexdigest "#{ key }#{ url }"
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
url_tokenizer-1.7.0 lib/url_tokenizer/limelight.rb
url_tokenizer-1.6.0 lib/url_tokenizer/limelight.rb
url_tokenizer-1.5.0 lib/url_tokenizer/limelight.rb
url_tokenizer-1.4.3 lib/url_tokenizer/limelight.rb
url_tokenizer-1.4.2 lib/url_tokenizer/limelight.rb
url_tokenizer-1.4.1 lib/url_tokenizer/limelight.rb
url_tokenizer-1.4.0 lib/url_tokenizer/limelight.rb
url_tokenizer-1.3.1 lib/url_tokenizer/limelight.rb
url_tokenizer-1.3.0 lib/url_tokenizer/limelight.rb
url_tokenizer-1.2.1 lib/url_tokenizer/limelight.rb
url_tokenizer-1.2.0 lib/url_tokenizer/limelight.rb
url_tokenizer-1.1.7 lib/url_tokenizer/limelight.rb
url_tokenizer-1.1.6 lib/url_tokenizer/limelight.rb
url_tokenizer-1.1.5 lib/url_tokenizer/limelight.rb