legato 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## Legato 0.3.0 ##
2
+
3
+ * Adds Management Goal under Profile
4
+ * Adds Management Segment
5
+ * Adds all `attributes` to WebProperty
6
+ * Adds `legato` CLI
7
+
8
+ Loads OAuth2 configuration and a legato user before dropping you into an
9
+ `irb` session.
10
+
11
+ *Tony Pitale*
12
+
1
13
  ## Legato 0.2.0 ##
2
14
 
3
15
  * Changed Query API for #metrics & #dimensions
data/bin/legato ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'legato'
4
+ require 'legato/cli'
5
+
6
+ cli = Legato::CLI.new
7
+
8
+ @legato_user = cli.build_user
9
+
10
+ def legato_user
11
+ @legato_user
12
+ end
13
+
14
+ include Legato::Management
15
+
16
+ cli.run
data/legato.gemspec CHANGED
@@ -27,7 +27,6 @@ Gem::Specification.new do |s|
27
27
  s.add_development_dependency "bourne"
28
28
  s.add_development_dependency "vcr", "2.0.0.beta2"
29
29
  s.add_development_dependency "fakeweb"
30
- s.add_development_dependency "oauth2"
31
30
  s.add_development_dependency "simplecov"
32
31
 
33
32
  s.add_runtime_dependency "multi_json"
data/lib/legato.rb CHANGED
@@ -41,9 +41,11 @@ require 'legato/user'
41
41
  require 'legato/profile_methods'
42
42
 
43
43
  require 'legato/management/finder'
44
+ require 'legato/management/segment'
44
45
  require 'legato/management/account'
45
46
  require 'legato/management/web_property'
46
47
  require 'legato/management/profile'
48
+ require 'legato/management/goal'
47
49
 
48
50
  require 'legato/list_parameter'
49
51
  require 'legato/response'
data/lib/legato/cli.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'irb'
2
+ require 'yaml'
3
+ require 'oauth2'
4
+
5
+ module Legato
6
+
7
+ module OAuth2Helpers
8
+ def build_client(id, secret)
9
+ opts = {
10
+ :authorize_url => 'https://accounts.google.com/o/oauth2/auth',
11
+ :token_url => 'https://accounts.google.com/o/oauth2/token'
12
+ }
13
+
14
+ OAuth2::Client.new(id, secret, opts)
15
+ end
16
+
17
+ def auth_url(client)
18
+ client.auth_code.authorize_url({
19
+ :scope => 'https://www.googleapis.com/auth/analytics.readonly',
20
+ :redirect_uri => 'http://localhost'
21
+ })
22
+ end
23
+
24
+ extend self
25
+ end
26
+
27
+ class CLI
28
+ def initialize
29
+ path = File.join(ENV['HOME'], '.legato.yml')
30
+
31
+ if File.exists?(path)
32
+ @config = YAML.load_file(path) rescue {}
33
+ else
34
+ puts "##########################################################"
35
+ puts "We can auto-load OAuth2 config from ~/.legato.yml"
36
+ puts "##########################################################"
37
+
38
+ build_config
39
+ end
40
+
41
+ build_access_token
42
+
43
+ print_yaml
44
+ end
45
+
46
+ def run
47
+ setup_irb
48
+ end
49
+
50
+ def setup_irb
51
+ IRB.setup(nil)
52
+ irb = IRB::Irb.new
53
+ IRB.conf[:MAIN_CONTEXT] = irb.context
54
+ irb.context.evaluate("require 'irb/completion'", 0)
55
+
56
+ trap("SIGINT") do
57
+ irb.signal_handle
58
+ end
59
+ catch(:IRB_EXIT) do
60
+ irb.eval_input
61
+ end
62
+ end
63
+
64
+ def client
65
+ @client ||= OAuth2Helpers.build_client(@config['id'], @config['secret'])
66
+ end
67
+
68
+ def token
69
+ @config['token']
70
+ end
71
+
72
+ def build_config
73
+ @config ||= {}
74
+
75
+ puts
76
+ print 'Your OAuth2 id: '
77
+ @config['id'] = $stdin.gets.strip
78
+ print 'Your OAuth2 secret: '
79
+ @config['secret'] = $stdin.gets.strip
80
+ end
81
+
82
+ def build_access_token
83
+ if token
84
+ @access_token = OAuth2::AccessToken.new(client, token)
85
+ else
86
+ puts
87
+ puts "Opening the OAuth2 auth url: #{OAuth2Helpers.auth_url(client)} ..."
88
+ `open "#{OAuth2Helpers.auth_url(client)}"`
89
+
90
+ puts
91
+ print 'OAuth2 Code (in the url): '
92
+ code = $stdin.gets.strip
93
+
94
+ @access_token = client.auth_code.get_token(code, :redirect_uri => 'http://localhost')
95
+ @config['token'] = @access_token.token
96
+ end
97
+ end
98
+
99
+ def build_user
100
+ Legato::User.new(@access_token)
101
+ end
102
+
103
+ def print_yaml
104
+ puts "##########################################################"
105
+ puts "If you haven't already done so, you can make ~/.legato.yml"
106
+ puts YAML.dump(@config)
107
+ puts "##########################################################"
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,42 @@
1
+ module Legato
2
+ module Management
3
+ class Goal
4
+
5
+ extend Finder
6
+
7
+ def self.default_path
8
+ "/accounts/~all/webproperties/~all/profiles/~all/goals"
9
+ end
10
+
11
+ def path
12
+ self.class.default_path + "/" + id.to_s
13
+ end
14
+
15
+ attr_accessor :id, :name, :account_id, :web_property_id, :profile_id, :user, :attributes
16
+
17
+ def initialize(attributes, user)
18
+ self.user = user
19
+ self.id = attributes['id']
20
+ self.name = attributes['name']
21
+ self.account_id = attributes['accountId']
22
+ self.web_property_id = attributes['webPropertyId']
23
+ self.profile_id = attributes['profileId']
24
+
25
+ ['id', 'name', 'accountId', 'webPropertyId', 'profileId'].each { |key| attributes.delete(key) }
26
+ self.attributes = attributes
27
+ end
28
+
29
+ def self.for_account(account)
30
+ all(account.user, account.path+'/webproperties/~all/profiles/~all/goals')
31
+ end
32
+
33
+ def self.for_web_property(web_property)
34
+ all(web_property.user, web_property.path+'/profiles/~all/goals')
35
+ end
36
+
37
+ def self.for_profile(profile)
38
+ all(profile.user, profile.path+'/goals')
39
+ end
40
+ end
41
+ end
42
+ end
@@ -10,18 +10,19 @@ module Legato
10
10
  end
11
11
 
12
12
  def path
13
- self.class.default_path + "/" + id.to_s
13
+ "/accounts/#{account_id}/webproperties/#{web_property_id}/profiles/#{id}"
14
14
  end
15
15
 
16
- attr_accessor :id, :name, :web_property_id, :user, :attributes
16
+ attr_accessor :id, :name, :web_property_id, :account_id, :user, :attributes
17
17
 
18
18
  def initialize(attributes, user)
19
19
  self.user = user
20
20
  self.id = attributes['id']
21
21
  self.name = attributes['name']
22
+ self.account_id = attributes['accountId']
22
23
  self.web_property_id = attributes['webPropertyId']
23
24
 
24
- ['id', 'name', 'webPropertyId'].each { |key| attributes.delete(key) }
25
+ ['id', 'name', 'accountId', 'webPropertyId'].each { |key| attributes.delete(key) }
25
26
  self.attributes = attributes
26
27
  end
27
28
 
@@ -32,6 +33,10 @@ module Legato
32
33
  def self.for_web_property(web_property)
33
34
  all(web_property.user, web_property.path+'/profiles')
34
35
  end
36
+
37
+ def goals
38
+ Goal.for_profile(self)
39
+ end
35
40
  end
36
41
  end
37
42
  end
@@ -0,0 +1,24 @@
1
+ module Legato
2
+ module Management
3
+ class Segment
4
+ extend Finder
5
+
6
+ def self.default_path
7
+ "/segments"
8
+ end
9
+
10
+ def path
11
+ "/segments/#{id}"
12
+ end
13
+
14
+ attr_accessor :id, :name, :definition, :user
15
+
16
+ def initialize(attributes, user)
17
+ self.user = user
18
+ self.id = attributes['id']
19
+ self.name = attributes['name']
20
+ self.definition = attributes['definition']
21
+ end
22
+ end
23
+ end
24
+ end
@@ -8,16 +8,18 @@ module Legato
8
8
  end
9
9
 
10
10
  def path
11
- self.class.default_path + "/" + id.to_s
11
+ "/accounts/#{account_id}/webproperties/#{id}"
12
12
  end
13
13
 
14
- attr_accessor :id, :name, :website_url, :user
14
+ attr_accessor :id, :name, :website_url, :account_id, :user, :attributes
15
15
 
16
16
  def initialize(attributes, user)
17
17
  self.user = user
18
18
  self.id = attributes['id']
19
- self.name = attributes['name']
20
- self.website_url = attributes['websiteUrl']
19
+ self.name = attributes.delete('name')
20
+ self.website_url = attributes.delete('websiteUrl')
21
+ self.account_id = attributes.delete('accountId')
22
+ self.attributes = attributes
21
23
  end
22
24
 
23
25
  def self.for_account(account)
@@ -1,3 +1,3 @@
1
1
  module Legato
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Legato::Management::Goal do
4
+ context "The Goal class" do
5
+ def self.subject_class_name
6
+ "goals"
7
+ end
8
+
9
+ it_behaves_like "a management finder"
10
+
11
+ it 'creates a new goal instance from a hash of attributes' do
12
+ user = stub
13
+ goal = Legato::Management::Goal.new({"id" => 12345, "name" => "Goal 1", "webPropertyId" => "WebProperty 7", "profileId" => "Profile 111", "value" => "/blah"}, user)
14
+ goal.user.should == user
15
+ goal.id.should == 12345
16
+ goal.name.should == "Goal 1"
17
+ goal.web_property_id.should == "WebProperty 7"
18
+ goal.profile_id.should == "Profile 111"
19
+
20
+ goal.attributes["value"].should == "/blah"
21
+ goal.attributes.has_key?("id").should be_false
22
+ end
23
+
24
+ it 'returns an array of all goals available to a user under an account' do
25
+ account = stub(:user => 'user', :path => 'accounts/12345')
26
+ Legato::Management::Goal.stubs(:all)
27
+
28
+ Legato::Management::Goal.for_account(account)
29
+
30
+ Legato::Management::Goal.should have_received(:all).with('user', 'accounts/12345/webproperties/~all/profiles/~all/goals')
31
+ end
32
+
33
+ it 'returns an array of all goals available to a user under an web property' do
34
+ web_property = stub(:user => 'user', :path => 'accounts/~all/webproperties/12345')
35
+ Legato::Management::Goal.stubs(:all)
36
+
37
+ Legato::Management::Goal.for_web_property(web_property)
38
+
39
+ Legato::Management::Goal.should have_received(:all).with('user', 'accounts/~all/webproperties/12345/profiles/~all/goals')
40
+ end
41
+
42
+ it 'returns an array of all goals available to a user under a profile' do
43
+ profile = stub(:user => 'user', :path => 'accounts/~all/webproperties/~all/profiles/12345')
44
+ Legato::Management::Goal.stubs(:all)
45
+
46
+ Legato::Management::Goal.for_profile(profile)
47
+
48
+ Legato::Management::Goal.should have_received(:all).with('user', 'accounts/~all/webproperties/~all/profiles/12345/goals')
49
+ end
50
+ end
51
+
52
+ context "A Goal instance" do
53
+ it 'builds the path for the goal from the id' do
54
+ goal = Legato::Management::Goal.new({"id" => 12345}, stub)
55
+ goal.path.should == '/accounts/~all/webproperties/~all/profiles/~all/goals/12345'
56
+ end
57
+ end
58
+ end
@@ -10,11 +10,12 @@ describe Legato::Management::Profile do
10
10
 
11
11
  it 'creates a new profile instance from a hash of attributes' do
12
12
  user = stub
13
- profile = Legato::Management::Profile.new({"id" => 12345, "name" => "Profile 1", "webPropertyId" => "WebProperty 7", "timezone" => "America/Edmonton"}, user)
13
+ profile = Legato::Management::Profile.new({"id" => 12345, "name" => "Profile 1", "accountId" => "12345", "webPropertyId" => "UA-12345-2", "timezone" => "America/Edmonton"}, user)
14
14
  profile.user.should == user
15
15
  profile.id.should == 12345
16
16
  profile.name.should == "Profile 1"
17
- profile.web_property_id.should == "WebProperty 7"
17
+ profile.account_id.should == "12345"
18
+ profile.web_property_id.should == "UA-12345-2"
18
19
 
19
20
  profile.attributes["timezone"].should == "America/Edmonton"
20
21
  profile.attributes.has_key?("id").should be_false
@@ -30,19 +31,19 @@ describe Legato::Management::Profile do
30
31
  end
31
32
 
32
33
  it 'returns an array of all profiles available to a user under an web property' do
33
- web_property = stub(:user => 'user', :path => 'accounts/~all/webproperties/12345')
34
+ web_property = stub(:user => 'user', :path => 'accounts/12345/webproperties/UA-12345-3')
34
35
  Legato::Management::Profile.stubs(:all)
35
36
 
36
37
  Legato::Management::Profile.for_web_property(web_property)
37
38
 
38
- Legato::Management::Profile.should have_received(:all).with('user', 'accounts/~all/webproperties/12345/profiles')
39
+ Legato::Management::Profile.should have_received(:all).with('user', 'accounts/12345/webproperties/UA-12345-3/profiles')
39
40
  end
40
41
  end
41
42
 
42
43
  context "A Profile instance" do
43
44
  it 'builds the path for the profile from the id' do
44
- web_property = Legato::Management::Profile.new({"id" => 12345}, stub)
45
- web_property.path.should == '/accounts/~all/webproperties/~all/profiles/12345'
45
+ profile = Legato::Management::Profile.new({"id" => 45678, "accountId" => "12345", "webPropertyId" => "UA-12345-3"}, stub)
46
+ profile.path.should == '/accounts/12345/webproperties/UA-12345-3/profiles/45678'
46
47
  end
47
48
  end
48
49
  end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Legato::Management::Segment do
4
+ context "The Segment class" do
5
+ def self.subject_class_name
6
+ "segments"
7
+ end
8
+
9
+ it_behaves_like "a management finder"
10
+
11
+ it 'creates a new segment instance from a hash of attributes' do
12
+ user = stub(:api_key => nil)
13
+ segment = Legato::Management::Segment.new({"id" => 12345, "name" => "Segment 1", "definition" => "Some Segment"}, user)
14
+ segment.user.should == user
15
+ segment.id.should == 12345
16
+ segment.name.should == "Segment 1"
17
+ segment.definition.should == "Some Segment"
18
+ end
19
+ end
20
+ end
@@ -10,11 +10,22 @@ describe Legato::Management::WebProperty do
10
10
 
11
11
  it 'creates a new web property instance from a hash of attributes' do
12
12
  user = stub
13
- web_property = Legato::Management::WebProperty.new({"id" => 12345, "name" => "WebProperty 1", "websiteUrl" => "http://google.com"}, user)
13
+ web_property = Legato::Management::WebProperty.new({
14
+ "id" => "UA-123456-2",
15
+ "name" => "WebProperty 1",
16
+ "websiteUrl" => "http://google.com",
17
+ "accountId" => "123456",
18
+ "profileCount" => 8,
19
+ "industryVertical" => "ecommerce"
20
+ }, user)
21
+
14
22
  web_property.user.should == user
15
- web_property.id.should == 12345
23
+ web_property.id.should == "UA-123456-2"
16
24
  web_property.name.should == "WebProperty 1"
17
25
  web_property.website_url.should == 'http://google.com'
26
+ web_property.account_id.should == "123456"
27
+ web_property.attributes['profileCount'].should == 8
28
+ web_property.attributes['industryVertical'].should == "ecommerce"
18
29
  end
19
30
 
20
31
  it 'returns an array of all web properties available to a user under an account' do
@@ -27,8 +38,8 @@ describe Legato::Management::WebProperty do
27
38
 
28
39
  context "A WebProperty instance" do
29
40
  it 'builds the path for the web_property from the id' do
30
- web_property = Legato::Management::WebProperty.new({"id" => 123456}, stub)
31
- web_property.path.should == '/accounts/~all/webproperties/123456'
41
+ web_property = Legato::Management::WebProperty.new({"id" => "UA-123456-5", "accountId" => "123456"}, stub)
42
+ web_property.path.should == '/accounts/123456/webproperties/UA-123456-5'
32
43
  end
33
44
  end
34
45
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: legato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-25 00:00:00.000000000 Z
12
+ date: 2014-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -107,22 +107,6 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
- - !ruby/object:Gem::Dependency
111
- name: oauth2
112
- requirement: !ruby/object:Gem::Requirement
113
- none: false
114
- requirements:
115
- - - ! '>='
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
- requirements:
123
- - - ! '>='
124
- - !ruby/object:Gem::Version
125
- version: '0'
126
110
  - !ruby/object:Gem::Dependency
127
111
  name: simplecov
128
112
  requirement: !ruby/object:Gem::Requirement
@@ -160,7 +144,8 @@ description: Access the Google Analytics Core Reporting and Management APIs with
160
144
  need.
161
145
  email:
162
146
  - tpitale@gmail.com
163
- executables: []
147
+ executables:
148
+ - legato
164
149
  extensions: []
165
150
  extra_rdoc_files: []
166
151
  files:
@@ -172,9 +157,10 @@ files:
172
157
  - Gemfile
173
158
  - README.md
174
159
  - Rakefile
160
+ - bin/legato
175
161
  - legato.gemspec
176
162
  - lib/legato.rb
177
- - lib/legato/collection.rb
163
+ - lib/legato/cli.rb
178
164
  - lib/legato/core_ext/array.rb
179
165
  - lib/legato/core_ext/string.rb
180
166
  - lib/legato/filter.rb
@@ -182,15 +168,14 @@ files:
182
168
  - lib/legato/list_parameter.rb
183
169
  - lib/legato/management/account.rb
184
170
  - lib/legato/management/finder.rb
171
+ - lib/legato/management/goal.rb
185
172
  - lib/legato/management/profile.rb
173
+ - lib/legato/management/segment.rb
186
174
  - lib/legato/management/web_property.rb
187
175
  - lib/legato/model.rb
188
176
  - lib/legato/profile_methods.rb
189
177
  - lib/legato/query.rb
190
- - lib/legato/reports.rb
191
- - lib/legato/request.rb
192
178
  - lib/legato/response.rb
193
- - lib/legato/result_set.rb
194
179
  - lib/legato/user.rb
195
180
  - lib/legato/version.rb
196
181
  - spec/cassettes/management/accounts.json
@@ -204,7 +189,9 @@ files:
204
189
  - spec/lib/legato/filter_spec.rb
205
190
  - spec/lib/legato/list_parameter_spec.rb
206
191
  - spec/lib/legato/management/account_spec.rb
192
+ - spec/lib/legato/management/goal_spec.rb
207
193
  - spec/lib/legato/management/profile_spec.rb
194
+ - spec/lib/legato/management/segment_spec.rb
208
195
  - spec/lib/legato/management/web_property_spec.rb
209
196
  - spec/lib/legato/model_spec.rb
210
197
  - spec/lib/legato/query_spec.rb
@@ -249,7 +236,9 @@ test_files:
249
236
  - spec/lib/legato/filter_spec.rb
250
237
  - spec/lib/legato/list_parameter_spec.rb
251
238
  - spec/lib/legato/management/account_spec.rb
239
+ - spec/lib/legato/management/goal_spec.rb
252
240
  - spec/lib/legato/management/profile_spec.rb
241
+ - spec/lib/legato/management/segment_spec.rb
253
242
  - spec/lib/legato/management/web_property_spec.rb
254
243
  - spec/lib/legato/model_spec.rb
255
244
  - spec/lib/legato/query_spec.rb
@@ -258,3 +247,4 @@ test_files:
258
247
  - spec/spec_helper.rb
259
248
  - spec/support/examples/management_finder.rb
260
249
  - spec/support/macros/oauth.rb
250
+ has_rdoc:
@@ -1,5 +0,0 @@
1
- module Legato
2
- class Collection
3
-
4
- end
5
- end
@@ -1,16 +0,0 @@
1
- # module Legato
2
- # module Reports
3
- # def self.add_report_method(klass)
4
- # # demodulize leaves potential to redefine
5
- # # these methods given different namespaces
6
- # method_name = klass.name.to_s.demodulize.underscore
7
- # return unless method_name.length > 0
8
- #
9
- # class_eval <<-CODE
10
- # def #{method_name}(opts = {}, &block)
11
- # #{klass}.results(self, opts, &block)
12
- # end
13
- # CODE
14
- # end
15
- # end
16
- # end
@@ -1,21 +0,0 @@
1
- # module Legato
2
- # class Request
3
- # URL = "https://www.googleapis.com/analytics/v3/data/ga"
4
- #
5
- # def initialize(query)
6
- # @query = query
7
- # end
8
- #
9
- # def response
10
- # @response ||= Response.new(parsed_response)
11
- # end
12
- #
13
- # def parsed_response
14
- # MultiJson.decode(raw_response)
15
- # end
16
- #
17
- # def raw_response
18
- # @query.profile.user.get(URL, :params => @query.to_params)
19
- # end
20
- # end
21
- # end
@@ -1,21 +0,0 @@
1
- # module Legato
2
- # class ResultSet
3
- # include Enumerable
4
- #
5
- # attr_accessor :total_results, :sampled
6
- #
7
- # alias :sampled? :sampled
8
- #
9
- # def initialize(results)
10
- # @results = results
11
- # end
12
- #
13
- # def each(&block)
14
- # @results.each(&block)
15
- # end
16
- #
17
- # def to_a
18
- # @results
19
- # end
20
- # end
21
- # end