ariadna 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -29,7 +29,7 @@ Create a new connexion with your Oauth2 access token
29
29
  Get a list of all accounts available
30
30
 
31
31
  ```ruby
32
- accounts = analytics.accounts
32
+ accounts = analytics.accounts.all
33
33
  ```
34
34
 
35
35
  Get a list of all web properties available for an account
@@ -44,7 +44,7 @@ Get a list of all profiles available for a web property
44
44
  profiles = properties.first.profiles.all
45
45
  ```
46
46
 
47
- Create a query with your metrics and dimensions
47
+ Create a query with metrics and dimensions
48
48
 
49
49
  ```ruby
50
50
  results = profile.results.select(
@@ -65,10 +65,10 @@ Create a query with your metrics and dimensions
65
65
  All the metrics and dimensions returned by the query are mapped into attributes.
66
66
 
67
67
  ```ruby
68
- @results.each do |result|
68
+ results.each do |result|
69
69
  puts result.visits
70
70
  puts result.bounces
71
- puts result.timeonsite
71
+ puts result.timeOnSite
72
72
  puts result.country
73
73
  end
74
74
  ```
@@ -114,7 +114,7 @@ gem 'omniauth'
114
114
  gem 'omniauth-google-oauth2'
115
115
  ```
116
116
 
117
- Google Oauth2 tokens have a very short life. To make things easy if the connexion gives a 401 error and there is a refresh token passed as a param Ariadna will try to get a new access token from Google and store it in the curren user info calling update_access_token_from_google. If you want to use this feature you must create a method in your user model that saves this token.
117
+ Google Oauth2 tokens have a very short life. To make things easy if the connexion gives a 401 error and there is a refresh token passed as a param Ariadna will try to get a new access token from Google and store it in the current user info calling update_access_token_from_google. If you want to use this feature you must create a method in your user model that saves this token.
118
118
 
119
119
  ```ruby
120
120
  def update_access_token_from_google(new_token)
@@ -10,6 +10,7 @@ require "ariadna/analytics"
10
10
  require "ariadna/connexion"
11
11
  require "ariadna/delegator"
12
12
  require "ariadna/profile"
13
+ require "ariadna/goal"
13
14
  require "ariadna/result"
14
15
  require "ariadna/error"
15
16
  require "ariadna/error_code"
@@ -2,13 +2,10 @@ module Ariadna
2
2
  class Account
3
3
 
4
4
  class << self;
5
- attr_reader :url
6
5
  attr_accessor :owner
7
6
  end
8
7
 
9
- #attr_reader :id, :link, :name, :properties_url
10
-
11
- @url = "https://www.googleapis.com/analytics/v3/management/accounts"
8
+ URL = "https://www.googleapis.com/analytics/v3/management/accounts"
12
9
 
13
10
  def initialize(item)
14
11
  item.each do |k,v|
@@ -20,6 +17,14 @@ module Ariadna
20
17
  @accounts ||= create_accounts
21
18
  end
22
19
 
20
+ def self.find(params)
21
+ return [] if params.empty?
22
+ all.each do |account|
23
+ return account if get_a_match(params, account)
24
+ end
25
+ []
26
+ end
27
+
23
28
  def properties
24
29
  Delegator.new(WebProperty, self)
25
30
  end
@@ -27,7 +32,7 @@ module Ariadna
27
32
  private
28
33
 
29
34
  def self.create_accounts
30
- accounts = Ariadna.connexion.get_url(self.url)
35
+ accounts = Ariadna.connexion.get_url(get_url)
31
36
  if (accounts["totalResults"].to_i > 0)
32
37
  create_attributes(accounts["items"])
33
38
  accounts["items"].map do |account|
@@ -41,5 +46,23 @@ module Ariadna
41
46
  attr_reader k.to_sym
42
47
  end
43
48
  end
49
+
50
+ def self.get_a_match(params, account)
51
+ if params[:id]
52
+ return true if (account.id.to_i == params[:id].to_i)
53
+ end
54
+ if params[:name]
55
+ return true if account.name.downcase.include? params[:name].downcase
56
+ end
57
+ return false
58
+ end
59
+
60
+ def self.get_url
61
+ if @account_id
62
+ "#{URL}/#{@account_id}"
63
+ else
64
+ URL
65
+ end
66
+ end
44
67
  end
45
68
  end
@@ -35,7 +35,7 @@ module Ariadna
35
35
  private
36
36
 
37
37
  def extract_proxy_options(proxy_options)
38
- return unless proxy_options.present?
38
+ return if proxy_options.empty?
39
39
  @use_proxy = true
40
40
  @proxy_host = proxy_options[:proxy_host]
41
41
  @proxy_port = proxy_options[:proxy_port]
@@ -44,7 +44,7 @@ module Ariadna
44
44
  end
45
45
 
46
46
  def extract_refresh_info(refresh_info)
47
- return unless refresh_info.present?
47
+ return if refresh_info.empty?
48
48
  @refresh_token = refresh_info[:refresh_token]
49
49
  @client_id = refresh_info[:client_id]
50
50
  @client_secret = refresh_info[:client_secret]
@@ -4,8 +4,6 @@ module Ariadna
4
4
  class << self;
5
5
  attr_accessor :owner
6
6
  end
7
-
8
- attr_reader :id, :link, :name, :goals, :parent
9
7
 
10
8
  def initialize(item)
11
9
  item.each do |k,v|
@@ -17,6 +15,11 @@ module Ariadna
17
15
  @profiles ||= create_profiles
18
16
  end
19
17
 
18
+ def self.find(id)
19
+ @profile_id = id
20
+ all.first
21
+ end
22
+
20
23
  def results
21
24
  Delegator.new(Result, self)
22
25
  end
@@ -24,7 +27,7 @@ module Ariadna
24
27
  private
25
28
 
26
29
  def self.create_profiles
27
- profiles = Ariadna.connexion.get_url(@owner.childLink["href"])
30
+ profiles = Ariadna.connexion.get_url(get_url)
28
31
  if (profiles["totalResults"].to_i > 0)
29
32
  create_attributes(profiles["items"])
30
33
  profiles["items"].map do |item|
@@ -38,5 +41,14 @@ module Ariadna
38
41
  attr_reader k.to_sym
39
42
  end
40
43
  end
44
+
45
+ def self.get_url
46
+ url = @owner.childLink["href"]
47
+ if @profile_id
48
+ "#{url}/#{@profile_id}"
49
+ else
50
+ url
51
+ end
52
+ end
41
53
  end
42
54
  end
@@ -14,7 +14,6 @@ module Ariadna
14
14
  end
15
15
 
16
16
  # metrics and dimensions
17
-
18
17
  def self.select(params)
19
18
  get_metrics_and_dimensions(params)
20
19
  self
@@ -33,6 +32,13 @@ module Ariadna
33
32
  self
34
33
  end
35
34
 
35
+ #uses profile id for query
36
+ def self.profile(id)
37
+ @profile_id = id
38
+ self
39
+ end
40
+
41
+
36
42
  # number of results returned
37
43
  def self.limit(results)
38
44
  if ((results.to_i > 0) and (results.to_i < 1001))
@@ -64,7 +70,7 @@ module Ariadna
64
70
  header["name"].sub('ga:', '')
65
71
  end
66
72
 
67
- # create attributes for each metric and dimension
73
+ # create attributes
68
74
  def self.create_attributes(results)
69
75
  summary_rows = Hash.new
70
76
  summary_rows.merge!(results)
@@ -128,7 +134,7 @@ module Ariadna
128
134
  end
129
135
 
130
136
  def self.generate_url
131
- params = conditions.merge({"ids" => "ga:#{@owner.id}"})
137
+ params = conditions.merge(get_profile_id)
132
138
  "#{URL}?" + params.map{ |k,v| "#{k}=#{v}"}.join("&")
133
139
  end
134
140
 
@@ -145,6 +151,14 @@ module Ariadna
145
151
  end
146
152
  end
147
153
 
154
+ def self.get_profile_id
155
+ if @profile_id
156
+ {"ids" => "ga:#{@profile_id}"}
157
+ else
158
+ {"ids" => "ga:#{@owner.id}"}
159
+ end
160
+ end
161
+
148
162
  def self.api_compatible_names(values)
149
163
  values.collect {|e| "ga:#{e}"}.join(",")
150
164
  end
@@ -1,3 +1,3 @@
1
1
  module Ariadna
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -9,6 +9,14 @@ module Ariadna
9
9
  @properties ||= create_properties
10
10
  end
11
11
 
12
+ def self.find(params)
13
+ return [] if params.empty?
14
+ all.each do |property|
15
+ return property if get_a_match(params, property)
16
+ end
17
+ []
18
+ end
19
+
12
20
  def initialize(item)
13
21
  item.each do |k,v|
14
22
  instance_variable_set("@#{k}", v)
@@ -22,7 +30,7 @@ module Ariadna
22
30
  private
23
31
 
24
32
  def self.create_properties
25
- properties = Ariadna.connexion.get_url("https://www.googleapis.com/analytics/v3/management/accounts/#{@owner.id}/webproperties")
33
+ properties = Ariadna.connexion.get_url(get_url)
26
34
  if (properties["totalResults"].to_i > 0)
27
35
  create_attributes(properties["items"])
28
36
  properties["items"].map do |property|
@@ -36,5 +44,24 @@ module Ariadna
36
44
  attr_reader k.to_sym
37
45
  end
38
46
  end
47
+
48
+ def self.get_a_match(params, property)
49
+ if params[:id]
50
+ return true if property.id.downcase == params[:id].downcase
51
+ end
52
+ if params[:name]
53
+ return true if property.name.downcase.include? params[:name].downcase
54
+ end
55
+ return false
56
+ end
57
+
58
+ def self.get_url
59
+ url = "https://www.googleapis.com/analytics/v3/management/accounts/#{@owner.id}/webproperties"
60
+ if @web_property_id
61
+ "#{url}/#{@web_property_id}"
62
+ else
63
+ url
64
+ end
65
+ end
39
66
  end
40
67
  end
@@ -1,6 +1,6 @@
1
1
  account_111:
2
2
  id: 111
3
- name: 'name'
3
+ name: 'my company account'
4
4
  selfLink: 'www.example.com/self/111'
5
5
  childLink:
6
6
  href: 'www.example.com/child/111'
@@ -1,5 +1,5 @@
1
1
  property:
2
- id: 909090
2
+ id: "UA-909090-1"
3
3
  kind: 'analytics#webProperty'
4
4
  selfLink: 'selfLink'
5
5
  accountId: 111
@@ -7,7 +7,7 @@ describe "Accounts" do
7
7
  end
8
8
 
9
9
  it "uses version 3 of the api" do
10
- Ariadna::Account.url.should == "https://www.googleapis.com/analytics/v3/management/accounts"
10
+ Ariadna::Account::URL.should == "https://www.googleapis.com/analytics/v3/management/accounts"
11
11
  end
12
12
 
13
13
  context "A new account" do
@@ -43,4 +43,21 @@ describe "Accounts" do
43
43
  @properties.first.class == Ariadna::WebProperty
44
44
  end
45
45
  end
46
+
47
+ context "can find an account by id or name" do
48
+ it "should return an empty array if can not find anything" do
49
+ account = Ariadna::Account.find({})
50
+ account.size.should == 0
51
+ end
52
+
53
+ it "should return the requested account filtered by name" do
54
+ account = Ariadna::Account.find(:name => "my COMpany account")
55
+ account.id.should == 111
56
+ end
57
+
58
+ it "should return the requested account filtered by name" do
59
+ account = Ariadna::Account.find(:id => 222)
60
+ account.name.should == 'name 222'
61
+ end
62
+ end
46
63
  end
@@ -1,3 +1,5 @@
1
+ require 'spec_helper'
2
+
1
3
  describe "Profile" do
2
4
  before :each do
3
5
  conn = Ariadna::Analytics.new("token")
@@ -61,7 +63,14 @@ describe "Profile" do
61
63
  end
62
64
 
63
65
  it "returns a list of profiles objects" do
64
- @profile.class == Ariadna::Profile
66
+ @profile.class.should == Ariadna::Profile
67
+ end
68
+ end
69
+
70
+ context "can find a profile by id" do
71
+ it "should return the requested profile" do
72
+ profile = @property.profiles.find(423423)
73
+ profile.class.should == Ariadna::Profile
65
74
  end
66
75
  end
67
76
  end
@@ -81,4 +81,25 @@ describe "Result" do
81
81
  Ariadna::Result.url.include?("filters=ga:browser%3D%3DFirefox").should be
82
82
  end
83
83
  end
84
+
85
+ context "Generate results with a profile id" do
86
+ it "should include a profile id" do
87
+ Ariadna::Result.profile(998877)
88
+ .select(
89
+ :metrics => [:visits, :bounces, :timeOnSite],
90
+ :dimensions => [:country]
91
+ )
92
+ .where(
93
+ :start_date => Date.today,
94
+ :end_date => Date.today,
95
+ :browser => "==Firefox"
96
+ )
97
+ .limit(100)
98
+ .offset(40)
99
+ .order([:visits, :bounces])
100
+ .all
101
+
102
+ Ariadna::Result.url.include?("ga:998877").should be
103
+ end
104
+ end
84
105
  end
@@ -10,7 +10,7 @@ describe "WebProperty" do
10
10
  context "A new web_property" do
11
11
  before :each do
12
12
  new_property = {
13
- "id" => 888888,
13
+ "id" => "UA-888888-1",
14
14
  "kind" => "analytics#webProperty",
15
15
  "selfLink" => "selfLink",
16
16
  "accountId" => @account.id,
@@ -53,4 +53,21 @@ describe "WebProperty" do
53
53
  @profiles.first.class == Ariadna::Profile
54
54
  end
55
55
  end
56
+
57
+ context "can find a web property by id or name" do
58
+ it "should return an empty array if can not find anything" do
59
+ property = @account.properties.find({})
60
+ property.size.should == 0
61
+ end
62
+
63
+ it "should return the requested account filtered by name" do
64
+ property = @account.properties.find(:name => "web_property_name")
65
+ property.id.should == "UA-909090-1"
66
+ end
67
+
68
+ it "should return the requested account filtered by id" do
69
+ property = @account.properties.find(:id => "UA-909090-1")
70
+ property.name.should == 'web_property_name'
71
+ end
72
+ end
56
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ariadna
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.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: 2012-04-28 00:00:00.000000000 Z
12
+ date: 2012-04-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pry