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:



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

def dimensions(*fields)
  fields, options = options_from_fields(fields)
  @dimensions ||= ListParameter.new(:dimensions, [], options.fetch(:tracking_scope, "ga"))
  @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



37
38
39
40
41
42
43
# File 'lib/legato/model.rb', line 37

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



27
28
29
# File 'lib/legato/model.rb', line 27

def filters
  @filters ||= {}
end

#instance_klassObject



72
73
74
# File 'lib/legato/model.rb', line 72

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
15
# File 'lib/legato/model.rb', line 11

def metrics(*fields)
  fields, options = options_from_fields(fields)
  @metrics ||= ListParameter.new(:metrics, [], options.fetch(:tracking_scope, "ga"))
  @metrics << fields
end

#options_from_fields(fields) ⇒ Object



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

def options_from_fields(fields)
  options = fields.pop if fields.last.is_a?(Hash)
  [fields, (options || {})]
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



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

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



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

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



45
46
47
# File 'lib/legato/model.rb', line 45

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



68
69
70
# File 'lib/legato/model.rb', line 68

def set_instance_klass(klass)
  @instance_klass = klass
end