g_ruby 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c30db117724c3d5efe55618c3a0b84ac1c541c73
4
+ data.tar.gz: 71f46c5ec2ccfcafe219275c646ffe5e47a36853
5
+ SHA512:
6
+ metadata.gz: d0dcbfdbea847a75e7015645832d391758869d21c1e3a524790c7e138d51018ee9087a9f53b9087ed10880b6c220e9b5fe3a0e0e8e5d019d6865d6a2f7de44d5
7
+ data.tar.gz: b58414db3ad6aac8dda42ad77b8b2b55324057baf94f2cfd1f8f2022287202dc4e9e45bdb0b37d343b7827b5ac9e7292978256dd67b4b370e4ab5d2157674a86
@@ -0,0 +1,114 @@
1
+ class GRuby::Analytics
2
+
3
+ require 'g_ruby/util'
4
+
5
+ GA_BASE = "https://www.googleapis.com/analytics/v3/data/ga?access_token="
6
+
7
+ #---------------------------------------------------------------------------------------------------
8
+
9
+ #GRuby::Analytics.get(access_token, start_date, end_date, profile_id, metrics, dimensions, sort, max_results)
10
+ def self.get(access_token, start_date, end_date, profile_id, metrics, dimensions=nil, sort=nil, limit=nil)
11
+ #begin
12
+ url = GA_BASE + "#{access_token}&start-date=#{start_date}&end-date=#{end_date}&ids=ga:#{profile_id}&metrics=#{metrics}"
13
+ url = url + "&dimensions=#{dimensions}" if !dimensions.blank?
14
+ url = url + "&sort=#{sort}" if !sort.blank?
15
+ url = url + "&max-results=#{limit}" if !limit.blank?
16
+ a = GRuby::Util.get_json(Nestful.get(url))
17
+ if !a.blank?
18
+ if !a["totalsForAllResults"].blank?
19
+ if dimensions.blank?
20
+ return a["totalsForAllResults"]
21
+ else
22
+ return a["rows"]
23
+ end
24
+ end
25
+ end
26
+ return nil
27
+ #rescue => e
28
+ #return {status: "failed", error: e.inspect}
29
+ #end
30
+ end
31
+
32
+ #---------------------------------------------------------------------------------------------------
33
+
34
+ #GRuby::Analytics.get_with_delta(ak.app_password, genesis, date_today, ak.entity_name, "ga:visits", "ga:socialNetwork", "-ga:visits", 11, refresh)
35
+ #GRuby::Analytics.get_with_delta(access_token, start_date, end_date, profile_id, metrics, dimensions, sort, max_results)
36
+ def self.get_with_delta(access_token, start_date, end_date, profile_id, metrics, dimensions=nil, sort=nil, limit=nil, refresh=nil)
37
+ #begin
38
+ d30_days_ago = (Date.today - 30).to_time.strftime("%Y-%m-%d").to_s
39
+ d60_days_ago = (Date.today - 60).to_time.strftime("%Y-%m-%d").to_s
40
+ date_today = Time.now.strftime("%Y-%m-%d")
41
+
42
+ a0 = GRuby::Analytics.get(access_token, start_date, end_date, profile_id, metrics, dimensions, sort, limit)
43
+ a30 = GRuby::Analytics.get(access_token, d30_days_ago, date_today, profile_id, metrics, dimensions, sort, nil)
44
+ a60 = GRuby::Analytics.get(access_token, d60_days_ago, d30_days_ago, profile_id, metrics, dimensions, sort, nil)
45
+
46
+ if a0.class.to_s != "Array"
47
+ return a0 if a0[:status] == "failed"
48
+ elsif a30.class.to_s != "Array"
49
+ return a30 if a0[:status] == "failed"
50
+ elsif a60.class.to_s != "Array"
51
+ return a60 if a0[:status] == "failed"
52
+ end
53
+
54
+ response_obj = []
55
+
56
+ if dimensions.blank?
57
+ metrics.split(",").each do |metric|
58
+ d_this = a30[metric].to_i
59
+ d_last = a60[metric].to_i
60
+ dd = GRuby::Analytics.generate_delta_explaination(d_this, d_last, refresh)
61
+ response_obj << {tag: metric.gsub("ga:", ""), val: a0[metric], delta: ((d_this - d_last) * 100 / d_last), delta_explaination: dd}
62
+ end
63
+ elsif dimensions.split(",").count == 1 and metrics.split(",").count == 1
64
+ a0.each do |i|
65
+ d_this = nil
66
+ d_last = nil
67
+ a30.each do |j|
68
+ d_this = j[1].to_i if j[0] == i[0]
69
+ end
70
+ a60.each do |k|
71
+ d_last = k[1].to_i if k[0] == i[0]
72
+ end
73
+ dd = GRuby::Analytics.generate_delta_explaination(d_this, d_last, refresh)
74
+ del = ((d_this.to_i - d_last.to_i) * 100 / d_last.to_f)
75
+ response_obj << {tag: metrics.gsub("ga:", ""), val: i[1], dimension: i[0], delta: del, delta_explaination: dd}
76
+ end
77
+ elsif dimensions.split(",").count > 1 and metrics.split(",").count == 1
78
+ a0.each do |i|
79
+ d_this = nil
80
+ d_last = nil
81
+ if dimensions == "ga:source,ga:medium"
82
+ k3 = i[1]=='referral' ? 'OtherSources' : (i[1]=='(none)' and i[0]=="(direct)") ? 'DirectSource' : 'searchEngine'
83
+ elsif dimensions == "ga:keyword,ga:medium"
84
+ k3 = i[1] == 'organic' ? 'OrganicKeywords' : 'PaidKeywords'
85
+ end
86
+ a30.each do |j|
87
+ d_this = j[2] if (j[0] == i[0] and j[1] == i[1])
88
+ end
89
+ a60.each do |k|
90
+ d_last = k[2] if (k[0] == i[0] and k[1] == i[1])
91
+ end
92
+ dd = GRuby::Analytics.generate_delta_explaination(d_this, d_last, refresh)
93
+ del = ((d_this.to_i - d_last.to_i) * 100 / d_last.to_f)
94
+ response_obj << {tag: i[0], val: i[2], dimension: k3, delta: del, delta_explaination: dd}
95
+ end
96
+ end
97
+
98
+ return response_obj
99
+ #rescue => e
100
+ #return {status: "failed", error: e.inspect}
101
+ #end
102
+ end
103
+
104
+ def self.generate_delta_explaination(d_this, d_last, refresh)
105
+ if !d_this.blank? and !d_last.blank?
106
+ d_this_to_s = d_this.to_s.gsub(".0", "")
107
+ d_last_to_s = d_last.to_s.gsub(".0", "")
108
+ return (refresh == "last_30") ? "Last 31 to 60 days: #{d_last_to_s}" : "Last 30 days: #{d_this_to_s}; Last 31 to 60 days: #{d_last_to_s}"
109
+ else
110
+ return nil
111
+ end
112
+ end
113
+
114
+ end
@@ -0,0 +1,18 @@
1
+ class GRuby::Auth
2
+
3
+ require 'g_ruby/util'
4
+
5
+ #GRuby::Auth.oauth2_url(state)
6
+ def self.oauth2_url(state)
7
+ a = "https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&approval_prompt=auto&client_id=#{GOOGLE_CLIENTID}&redirect_uri=#{GOOGLE_CALLBACK}&scope=https://www.googleapis.com/auth/analytics.readonly"
8
+ a = a + "&state=#{state}" if !state.blank?
9
+ return a
10
+ end
11
+
12
+ #GRuby::Auth.token(auth_code)
13
+ def self.token(auth_code)
14
+ g = Nestful.post "https://accounts.google.com/o/oauth2/token?method=POST&grant_type=authorization_code&code=#{auth_code}&client_id=#{GOOGLE_CLIENTID}&client_secret=#{GOOGLE_SECRET}&redirect_uri=#{GOOGLE_CALLBACK}"
15
+ return GRuby::Util.get_json(g)
16
+ end
17
+
18
+ end
@@ -0,0 +1,17 @@
1
+ class GRuby::Util
2
+
3
+ #GRuby::Util.get_json(nestful_response)
4
+ def self.get_json(nestful_response)
5
+ begin
6
+ if !nestful_response.blank?
7
+ if !nestful_response.body.blank?
8
+ return JSON.parse(nestful_response.body)
9
+ end
10
+ end
11
+ return nil
12
+ rescue
13
+ return nil
14
+ end
15
+ end
16
+
17
+ end
data/lib/g_ruby.rb ADDED
@@ -0,0 +1,8 @@
1
+ class GRuby
2
+
3
+ end
4
+
5
+ require 'nestful'
6
+ require 'nokogiri'
7
+ require 'g_ruby/auth'
8
+ require 'g_ruby/analytics'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: g_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pykih Software
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nestful
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.3
41
+ description: Currently, we are supporting only Google Analytics APIs
42
+ email: rp@pykih.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/g_ruby.rb
48
+ - lib/g_ruby/analytics.rb
49
+ - lib/g_ruby/auth.rb
50
+ - lib/g_ruby/util.rb
51
+ homepage: https://github.com/pykih/g_ruby
52
+ licenses: []
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.3.6
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 2.0.3
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Ruby Wrapper over Google APIs
74
+ test_files: []