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

#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
38
39
40
41
# File 'lib/legato/model.rb', line 35

def filter(name, &block)
  filters[name] = block

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

#filtersObject



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

def filters
  @filters ||= {}
end

#instance_klassObject



70
71
72
# File 'lib/legato/model.rb', line 70

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



95
96
97
# File 'lib/legato/model.rb', line 95

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

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



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

def results(profile, options = {})
  # TODO: making tracking scope configurable when results are querried.  not sure how to do this.
  Query.new(self).results(profile, options)
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



53
54
55
56
57
58
59
# File 'lib/legato/model.rb', line 53

def segment(name, &block)
  segments[name] = block

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

#segmentsObject



43
44
45
# File 'lib/legato/model.rb', line 43

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



66
67
68
# File 'lib/legato/model.rb', line 66

def set_instance_klass(klass)
  @instance_klass = klass
end