Class: Legato::User

Inherits:
Object
  • Object
show all
Defined in:
lib/legato/user.rb

Constant Summary collapse

VALID_TRACKING_SCOPES =
{
  'ga' => 'ga',
  'mcf' => 'mcf',
  'rt' => 'realtime'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, api_key = nil) ⇒ User

Returns a new instance of User.



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

def initialize(token, api_key = nil)
  self.access_token = token
  self.api_key = api_key
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

Instance Method Details

#accountsObject

All the ‘Account` records available to this user



36
37
38
# File 'lib/legato/user.rb', line 36

def accounts
  Management::Account.all(self)
end

#goalsObject

All the ‘Goal` records available to this user



56
57
58
# File 'lib/legato/user.rb', line 56

def goals
  Management::Goal.all(self)
end

#profilesObject

All the ‘Profile` records available to this user



46
47
48
# File 'lib/legato/user.rb', line 46

def profiles
  Management::Profile.all(self)
end

#request(query) ⇒ Object

TODO: refactor into request object again



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legato/user.rb', line 17

def request(query)
  url = url_for(query)

  raw_response = if api_key
    # oauth 1 + api key
    query_string = URI.escape(query.to_query_string, '<>') # may need to add !~@

    access_token.get(url + query_string + "&key=#{api_key}")
  else
    # oauth 2
    access_token.get(url, :params => query.to_params)
  end

  Response.new(raw_response, query.instance_klass)
end

#segmentsObject

All the ‘Segment` records available to this user



51
52
53
# File 'lib/legato/user.rb', line 51

def segments
  Management::Segment.all(self)
end

#url_for(query) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/legato/user.rb', line 60

def url_for(query)
  raise "invalid tracking_scope" unless tracking_scope_valid?(query.tracking_scope)

  endpoint = VALID_TRACKING_SCOPES[query.tracking_scope]

  "https://www.googleapis.com/analytics/v3/data/#{endpoint}"
end

#web_propertiesObject

All the ‘WebProperty` records available to this user



41
42
43
# File 'lib/legato/user.rb', line 41

def web_properties
  Management::WebProperty.all(self)
end