Skip to content

Instantly share code, notes, and snippets.

@tpitale
Created July 15, 2013 15:43
Show Gist options
  • Save tpitale/6000974 to your computer and use it in GitHub Desktop.
Save tpitale/6000974 to your computer and use it in GitHub Desktop.
Test/demo script for Legato Issue #34
#!/usr/bin/env ruby -Ilib
require 'pp'
require 'oauth2'
require 'legato'
module OAuth2Helpers
def build_client(id, secret)
opts = {
:authorize_url => 'https://accounts.google.com/o/oauth2/auth',
:token_url => 'https://accounts.google.com/o/oauth2/token'
}
OAuth2::Client.new(id, secret, opts)
end
def auth_url(client)
client.auth_code.authorize_url({
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
:redirect_uri => 'http://localhost'
})
end
extend self
end
puts
print 'Your OAuth2 id: '
oauth_id = $stdin.gets.strip
print 'Your OAuth2 secret: '
oauth_secret = $stdin.gets.strip
client = OAuth2Helpers.build_client(oauth_id, oauth_secret)
puts
puts "Opening the OAuth2 auth url: #{OAuth2Helpers.auth_url(client)} ..."
`open "#{OAuth2Helpers.auth_url(client)}"`
puts
print 'OAuth2 Code (in the url): '
code = $stdin.gets.strip
access_token = client.auth_code.get_token(code, :redirect_uri => 'http://localhost')
user = Legato::User.new(access_token)
pp Legato::Management::Profile.all(user).map {|p| [p.id, p.name]}
puts
print 'Enter the profile id you would like to use: '
profile_id = $stdin.gets.strip
profile = Legato::Management::Profile.all(user).select {|p| p.id == profile_id}.first
puts
print 'Enter the vertical you would like to use (e.g.: blog): '
vertical = $stdin.gets.strip
class Visitors
extend Legato::Model
metrics :visitors, :goal_1_completions
dimensions :pagePath
filter :for_vertical, &lambda { |vertical| contains(:pagePath, "/#{vertical}") }
filter :for_medium, &lambda { |medium| matches(:medium, medium) }
end
pp Visitors.for_vertical(vertical).results(profile).to_a # OK
pp Visitors.for_medium("organic").results(profile).to_a # OK
pp Visitors.for_vertical(vertical).for_medium("organic").results(profile).to_a # Works for me :-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment