Skip to content
Browse files

initial commit, completed management api for accounts, web_properties…

…, profiles
  • Loading branch information...
1 parent d3bbeea commit 66744413e6e62b9c844836177c167a0bec0a8c77 @tpitale committed Nov 23, 2011
View
2 .rspec
@@ -0,0 +1,2 @@
+--colour
+--format nested
View
2 Gemfile
@@ -1,4 +1,4 @@
source "http://rubygems.org"
-# Specify your gem's dependencies in cepstrum.gemspec
+# Specify your gem's dependencies in legato.gemspec
gemspec
View
22 OAUTH.md
@@ -0,0 +1,22 @@
+# Authorizing requests with OAuth 2.0 #
+
+## Creating the client ##
+
+* Go to the [API Console](https://code.google.com/apis/console#access)
+* Create a new Project
+* Click API Access in the left column
+* Click Create an OAuth 2.0 client ID
+* Enter a product name, and add an optional logo
+* Click next
+* Select Installed Application
+* Click create client id
+* Hang onto the client id and secret
+
+client = OAuth2::Client.new('779170787975.apps.googleusercontent.com', 'mbCISoZiSwyVQIDEbLj4EeEc', :authorize_url => 'https://accounts.google.com/o/oauth2/auth', :token_url => 'https://accounts.google.com/o/oauth2/token')
+
+client.auth_code.authorize_url(:scope => 'https://www.googleapis.com/auth/analytics.readonly', :redirect_uri => 'http://localhost')
+
+access_token = client.auth_code.get_token('4/Wdfs_g6f8LU3uIjuPHMd3VgSsiDa', :redirect_uri => 'http://localhost')
+
+json = access_token.get('https://www.googleapis.com/analytics/v3/management/accounts').body
+JSON.parse(json)
View
22 README.md
@@ -1,10 +1,10 @@
-http://en.wikipedia.org/wiki/Cepstrum
+http://en.wikipedia.org/wiki/Legato
Google Analytics Mapper
access\_token = OAuth2 # from Google, provide instructions on getting the token!
-user = Cepstrum::User.new(access\_token)
+user = Legato::User.new(access\_token)
user.accounts
user.accounts.first.profiles
@@ -16,11 +16,11 @@ profile = user.profiles.first
profile.user == user #=> true
class Exit
- #? extend Cepstrum::Mapping
- #? extend Cepstrum::Filtering
- #? extend Cepstrum::Attribute
+ #? extend Legato::Mapping
+ #? extend Legato::Filtering
+ #? extend Legato::Attribute
- extend Cepstrum # ::Mapper/::Model
+ extend Legato # ::Mapper/::Model
metrics :exits, :pageviews
dimensions :page\_path, :operating\_system, :browser
@@ -81,15 +81,15 @@ Filtering
Accounts, WebProperties, Profiles, and Goals
--------------------------------------------
- > Cepstrum::Management::Account.all
- > Cepstrum::Management::WebProperty.all
- > Cepstrum::Management::Profile.all
- > Cepstrum::Management::Goal.all
+ > Legato::Management::Account.all
+ > Legato::Management::WebProperty.all
+ > Legato::Management::Profile.all
+ > Legato::Management::Goal.all
Profiles for a UA- Number (a WebProperty)
-----------------------------------------
- > profile = Cepstrum::Management::Profile.all.detect {|p| p.web_property_id == 'UA-XXXXXXX-X'}
+ > profile = Legato::Management::Profile.all.detect {|p| p.web_property_id == 'UA-XXXXXXX-X'}
Other Parameters
----------------
View
12 cepstrum.gemspec → legato.gemspec
@@ -1,17 +1,17 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
-require "cepstrum/version"
+require "legato/version"
Gem::Specification.new do |s|
- s.name = "cepstrum"
- s.version = Cepstrum::VERSION
+ s.name = "legato"
+ s.version = Legato::VERSION
s.authors = ["Tony Pitale"]
s.email = ["tpitale@gmail.com"]
- s.homepage = "" # "http://cepstrum.github.com"
+ s.homepage = "" # "http://legato.github.com"
s.summary = %q{TODO: Write a gem summary}
s.description = %q{TODO: Write a gem description}
- s.rubyforge_project = "cepstrum" # ?
+ s.rubyforge_project = "legato" # ?
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -24,5 +24,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "bourne"
# s.add_development_dependency "vcr"
- # s.add_runtime_dependency "" # NOTHING!
+ s.add_runtime_dependency "oauth2"
end
View
5 lib/cepstrum.rb
@@ -1,5 +0,0 @@
-require "cepstrum/version"
-
-module Cepstrum
- # Your code goes here...
-end
View
14 lib/legato.rb
@@ -0,0 +1,14 @@
+require "legato/version"
+
+require 'json'
+
+module Legato
+ autoload :User, 'legato/user'
+
+ module Management
+ autoload :Finder, 'legato/management/finder'
+ autoload :Account, 'legato/management/account'
+ autoload :WebProperty, 'legato/management/web_property'
+ autoload :Profile, 'legato/management/profile'
+ end
+end
View
23 lib/legato/management/account.rb
@@ -0,0 +1,23 @@
+module Legato
+ module Management
+ class Account
+ extend Finder
+
+ def self.default_path
+ "/accounts"
+ end
+
+ def path
+ "/accounts/#{id}"
+ end
+
+ attr_accessor :id, :name, :user
+
+ def initialize(attributes, user)
+ self.user = user
+ self.id = attributes['id']
+ self.name = attributes['name']
+ end
+ end
+ end
+end
View
14 lib/legato/management/finder.rb
@@ -0,0 +1,14 @@
+module Legato
+ module Management
+ module Finder
+ def base_uri
+ "https://www.googleapis.com/analytics/v3/management"
+ end
+
+ def all(user, path=default_path)
+ json = user.access_token.get(base_uri + path).body
+ JSON.parse(json)['items'].map {|item| new(item, user)}
+ end
+ end
+ end
+end
View
31 lib/legato/management/profile.rb
@@ -0,0 +1,31 @@
+module Legato
+ module Management
+ class Profile
+ extend Finder
+
+ def self.default_path
+ "/accounts/~all/web_properties/~all/profiles"
+ end
+
+ def path
+ self.class.default_path + "/" + id.to_s
+ end
+
+ attr_accessor :id, :name, :user
+
+ def initialize(attributes, user)
+ self.user = user
+ self.id = attributes['id']
+ self.name = attributes['name']
+ end
+
+ def self.for_account(account)
+ all(account.user, account.path+'/web_properties/~all/profiles')
+ end
+
+ def self.for_web_property(web_property)
+ all(web_property.user, web_property.path+'/profiles')
+ end
+ end
+ end
+end
View
28 lib/legato/management/web_property.rb
@@ -0,0 +1,28 @@
+module Legato
+ module Management
+ class WebProperty
+ extend Finder
+
+ def self.default_path
+ "/accounts/~all/web_properties"
+ end
+
+ def path
+ self.class.default_path + "/" + id.to_s
+ end
+
+ attr_accessor :id, :name, :website_url, :user
+
+ def initialize(attributes, user)
+ self.user = user
+ self.id = attributes['id']
+ self.name = attributes['name']
+ self.website_url = attributes['websiteUrl']
+ end
+
+ def self.for_account(account)
+ all(account.user, account.path+'/web_properties')
+ end
+ end
+ end
+end
View
5 lib/legato/user.rb
@@ -0,0 +1,5 @@
+module Legato
+ class User
+ attr_accessor :access_token
+ end
+end
View
2 lib/cepstrum/version.rb → lib/legato/version.rb
@@ -1,3 +1,3 @@
-module Cepstrum
+module Legato
VERSION = "0.0.1"
end
View
26 spec/lib/legato/management/account_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+describe Legato::Management::Account do
+ context "The Account class" do
+ def self.subject_class_name
+ "accounts"
+ end
+
+ it_behaves_like "a management finder"
+
+ it 'creates a new account instance from a hash of attributes' do
+ user = stub
+ account = Legato::Management::Account.new({"id" => 12345, "name" => "Account 1"}, user)
+ account.user.should == user
+ account.id.should == 12345
+ account.name.should == "Account 1"
+ end
+ end
+
+ context "An Account instance" do
+ it 'builds the path for the account from the id' do
+ account = Legato::Management::Account.new({"id" => 123456}, stub)
+ account.path.should == '/accounts/123456'
+ end
+ end
+end
View
45 spec/lib/legato/management/profile_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+
+describe Legato::Management::Profile do
+ context "The Profile class" do
+ def self.subject_class_name
+ "profiles"
+ end
+
+ it_behaves_like "a management finder"
+
+ it 'creates a new profile instance from a hash of attributes' do
+ user = stub
+ profile = Legato::Management::Profile.new({"id" => 12345, "name" => "Profile 1"}, user)
+ profile.user.should == user
+ profile.id.should == 12345
+ profile.name.should == "Profile 1"
+ end
+
+ it 'returns an array of all profiles available to a user under an account' do
+ account = stub(:user => 'user', :path => 'accounts/12345')
+ Legato::Management::Profile.stubs(:all)
+
+ Legato::Management::Profile.for_account(account)
+
+ Legato::Management::Profile.should have_received(:all).with('user', 'accounts/12345/web_properties/~all/profiles')
+ end
+
+ it 'returns an array of all profiles available to a user under an web property' do
+ web_property = stub(:user => 'user', :path => 'accounts/~all/web_properties/12345')
+ Legato::Management::Profile.stubs(:all)
+
+ Legato::Management::Profile.for_web_property(web_property)
+
+ Legato::Management::Profile.should have_received(:all).with('user', 'accounts/~all/web_properties/12345/profiles')
+ end
+ end
+
+ context "A Profile instance" do
+ it 'builds the path for the profile from the id' do
+ web_property = Legato::Management::Profile.new({"id" => 12345}, stub)
+ web_property.path.should == '/accounts/~all/web_properties/~all/profiles/12345'
+ end
+ end
+end
+
View
36 spec/lib/legato/management/web_property_spec.rb
@@ -0,0 +1,36 @@
+require 'spec_helper'
+
+describe Legato::Management::WebProperty do
+ context "The WebProperty class" do
+ def self.subject_class_name
+ "web properties"
+ end
+
+ it_behaves_like "a management finder"
+
+ it 'creates a new web property instance from a hash of attributes' do
+ user = stub
+ web_property = Legato::Management::WebProperty.new({"id" => 12345, "name" => "WebProperty 1", "websiteUrl" => "http://google.com"}, user)
+ web_property.user.should == user
+ web_property.id.should == 12345
+ web_property.name.should == "WebProperty 1"
+ web_property.website_url.should == 'http://google.com'
+ end
+
+ it 'returns an array of all web properties available to a user under an account' do
+ account = stub(:user => 'user', :path => 'accounts/12345')
+ Legato::Management::WebProperty.stubs(:all)
+
+ Legato::Management::WebProperty.for_account(account)
+
+ Legato::Management::WebProperty.should have_received(:all).with('user', 'accounts/12345/web_properties')
+ end
+ end
+
+ context "A WebProperty instance" do
+ it 'builds the path for the web_property from the id' do
+ web_property = Legato::Management::WebProperty.new({"id" => 123456}, stub)
+ web_property.path.should == '/accounts/~all/web_properties/123456'
+ end
+ end
+end
View
5 spec/lib/legato/user_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Legato::User do
+
+end
View
14 spec/spec_helper.rb
@@ -0,0 +1,14 @@
+require 'bundler'
+Bundler.setup(:test)
+
+require 'rspec'
+require 'mocha'
+require 'bourne'
+
+require File.expand_path('../../lib/legato', __FILE__)
+
+RSpec.configure do |config|
+ config.mock_with :mocha
+end
+
+Dir["./spec/support/**/*.rb"].each {|f| require f}
View
18 spec/support/examples/management_finder.rb
@@ -0,0 +1,18 @@
+shared_examples_for "a management finder" do
+ it "returns an array of all #{subject_class_name} available to a user" do
+ JSON.stubs(:parse).returns({'items' => ['item1', 'item2']})
+ response = stub(:body => 'some json')
+ access_token = stub(:get => response)
+ user = stub(:access_token => access_token)
+ described_class.stubs(:new).returns('thing1', 'thing2')
+
+ described_class.all(user).should == ['thing1', 'thing2']
+
+ user.should have_received(:access_token)
+ access_token.should have_received(:get).with('https://www.googleapis.com/analytics/v3/management'+described_class.default_path)
+ response.should have_received(:body)
+ JSON.should have_received(:parse).with('some json')
+ described_class.should have_received(:new).with('item1', user)
+ described_class.should have_received(:new).with('item2', user)
+ end
+end

0 comments on commit 6674441

Please sign in to comment.
Something went wrong with that request. Please try again.