Class: Garb::FilterParameters

Inherits:
Object
  • Object
show all
Defined in:
lib/garb/filter_parameters.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilterParameters

Returns a new instance of FilterParameters.



18
19
20
# File 'lib/garb/filter_parameters.rb', line 18

def initialize
  self.parameters = []
end

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters.



16
17
18
# File 'lib/garb/filter_parameters.rb', line 16

def parameters
  @parameters
end

Class Method Details

.define_operators(*methods) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/garb/filter_parameters.rb', line 3

def self.define_operators(*methods)
  methods.each do |method|
    class_eval <<-CODE
      def #{method}(field, value)
        @filter_hash.merge!({SymbolOperator.new(field, :#{method}) => value})
      end
    CODE
  end
end

Instance Method Details

#filters(&block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/garb/filter_parameters.rb', line 22

def filters(&block)
  @filter_hash = {}

  instance_eval &block

  self.parameters << @filter_hash
end

#to_paramsObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/garb/filter_parameters.rb', line 30

def to_params
  value = self.parameters.map do |param|
    param.map do |k,v|
      next unless k.is_a?(SymbolOperator)
      "#{URI.encode(k.to_google_analytics, /[=<>]/)}#{CGI::escape(v.to_s)}"
    end.join(';') # Hash AND (no duplicate keys)
  end.join(',') # Array OR

  value.empty? ? {} : {'filters' => value}
end