Sha256: 027f28f6b204f68a488cfdff296dae85009bb258c4d70a7daf1a267227816fe0

Contents?: true

Size: 870 Bytes

Versions: 2

Compression:

Stored size: 870 Bytes

Contents

module Garb
  class FilterParameters
    attr_accessor :parameters

    def initialize(parameters)
      self.parameters = (Array.wrap(parameters) || []).compact
    end

    def to_params
      value = array_to_params(self.parameters)
      value.empty? ? {} : {'filters' => value}
    end

    private
    def array_to_params(arr)
      arr.map do |param|
        case param
        when Hash  then hash_to_params(param)
        when Array then array_to_params(param)
        end
      end.join(',') # Array OR
    end

    def hash_to_params(hsh)
      hsh.map do |k, v|
        next unless k.is_a?(SymbolOperatorMethods)

        escaped_v = v.to_s.gsub(/([,;])/) { |c| '\\' + c }
        "#{URI.encode(k.to_google_analytics, /[=<>]/)}#{CGI::escape(escaped_v)}"
      end.join('%3B') # Hash AND (no duplicate keys), escape char for ';' fixes oauth
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
garb-0.9.8 lib/garb/filter_parameters.rb
garb-0.9.7 lib/garb/filter_parameters.rb