Module: Legato::Model

Defined in:
lib/legato/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



3
4
5
# File 'lib/legato/model.rb', line 3

def self.extended(base)
  ProfileMethods.add_profile_method(base)
end

Instance Method Details

#add_method_to_set(name, type, &block) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/legato/model.rb', line 93

def add_method_to_set(name, type, &block)
  send(type)[name] = block

  (class << self; self; end).instance_eval do
    define_method(name) {|*args| Query.new(self).apply_filter_expression(type, *args, &block)}
  end
end

#dimensions(*fields) ⇒ ListParameter

Adds dimensions to the class for retrieval from GA

Parameters:

  • *fields (Symbol)

    the names of the fields to retrieve

Returns:



20
21
22
23
# File 'lib/legato/model.rb', line 20

def dimensions(*fields)
  @dimensions ||= ListParameter.new(:dimensions, [])
  @dimensions << fields
end

#filter(name, &block) ⇒ Proc

Define a filter

Parameters:

  • name (Symbol)

    the class method name for the resulting filter

  • block

    the block which contains filter methods to define the parameters used for filtering the request to GA

Returns:

  • (Proc)

    the body of newly defined method



35
36
37
# File 'lib/legato/model.rb', line 35

def filter(name, &block)
  add_method_to_set(name, :filters, &block)
end

#filtersObject



25
26
27
# File 'lib/legato/model.rb', line 25

def filters
  @filters ||= {}
end

#instance_klassObject



63
64
65
# File 'lib/legato/model.rb', line 63

def instance_klass
  @instance_klass || OpenStruct
end

#metrics(*fields) ⇒ ListParameter

Adds metrics to the class for retrieval from GA

Parameters:

  • *fields (Symbol)

    the names of the fields to retrieve

Returns:



11
12
13
14
# File 'lib/legato/model.rb', line 11

def metrics(*fields)
  @metrics ||= ListParameter.new(:metrics, [])
  @metrics << fields
end

#realtimeQuery

Builds a ‘query` and sets the `realtime` property

Returns:

  • (Query)

    a new query with ‘realtime` property set



89
90
91
# File 'lib/legato/model.rb', line 89

def realtime
  Query.new(self).realtime
end

#results(profile, options = {}) ⇒ Query

Builds a ‘query` to get results from GA

Parameters:

  • profile (Legato::Management::Profile)

    the profile to query GA against

  • options (Hash) (defaults to: {})

    options:

    • start_date

    • end_date

    • limit

    • offset

    • sort

    • quota_user

    • segment_id

Returns:

  • (Query)

    a new query with all the filters/segments defined on the model, allowing for chainable behavior before kicking of the request to Google Analytics which returns the result data



81
82
83
84
# File 'lib/legato/model.rb', line 81

def results(profile, options = {})
  # TODO: making tracking scope configurable when results are querried.  not sure how to do this.
  Query.new(self).apply_options(options.merge(:profile => profile))
end

#segment(name, &block) ⇒ Proc

Define a segment

Parameters:

  • name (Symbol)

    the class method name for the resulting segment

  • block

    the block which contains filter methods to define the parameters used for segmenting the request to GA

Returns:

  • (Proc)

    the body of newly defined method



50
51
52
# File 'lib/legato/model.rb', line 50

def segment(name, &block)
  add_method_to_set(name, :segment_filters, &block)
end

#segmentsObject Also known as: segment_filters



39
40
41
# File 'lib/legato/model.rb', line 39

def segments
  @segments ||= {}
end

#set_instance_klass(klass) ⇒ Object

Set the class used to make new instances of returned results from GA

Parameters:

  • klass (Class)

    any class that accepts a hash of attributes to initialize the values of the class

Returns:

  • the original class given



59
60
61
# File 'lib/legato/model.rb', line 59

def set_instance_klass(klass)
  @instance_klass = klass
end