Skip to content

Commit abe2bfd

Browse files
committed
Integrated Google Oauth in separate branch
1 parent c77b66f commit abe2bfd

26 files changed

+319
-12
lines changed

Gemfile

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ gem 'spring', group: :development
4343

4444
# Use google_drive to read/write files or spreadsheets from google drive
4545
gem 'omniauth-google-oauth2'
46-
gem 'google_drive'
46+
gem 'google-api-client', require: 'google/api_client'
47+
gem 'google_drive', git: 'git://github.com/SixiS/google-drive-ruby'
4748

4849
# Use linkedin to interface with linkedin api
50+
#gem 'omniauth-linkedin-oauth2'
4951
gem 'linkedin'
5052

5153
# Use sendrid to access sendgrid api to send mails and get their status
@@ -81,3 +83,5 @@ group :development, :test do
8183
end
8284

8385
gem 'simple_form'
86+
gem 'pry'
87+
gem 'rest_client'

Gemfile.lock

+49-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
GIT
2+
remote: git://github.com/SixiS/google-drive-ruby
3+
revision: 7a58b426959727aef9d7e99a9942e945f836875a
4+
specs:
5+
google_drive (0.3.9)
6+
nokogiri (>= 1.4.4, != 1.5.2, != 1.5.1)
7+
oauth (>= 0.3.6)
8+
oauth2 (>= 0.5.0)
9+
110
GEM
211
remote: https://rubygems.org/
312
specs:
@@ -27,14 +36,20 @@ GEM
2736
minitest (~> 5.1)
2837
thread_safe (~> 0.1)
2938
tzinfo (~> 1.1)
39+
addressable (2.3.6)
3040
arel (5.0.1.20140414130214)
41+
autoparse (0.3.3)
42+
addressable (>= 2.3.1)
43+
extlib (>= 0.9.15)
44+
multi_json (>= 1.0.0)
3145
bcrypt (3.1.7)
3246
bootstrap-sass (3.2.0.0)
3347
sass (~> 3.2)
3448
bson (2.3.0)
3549
builder (3.2.2)
3650
celluloid (0.15.2)
3751
timers (~> 1.1.0)
52+
coderay (1.1.0)
3853
coffee-rails (4.0.1)
3954
coffee-script (>= 2.2.0)
4055
railties (>= 4.0.0, < 5.0)
@@ -57,6 +72,7 @@ GEM
5772
docile (1.1.5)
5873
erubis (2.7.0)
5974
execjs (2.2.1)
75+
extlib (0.9.16)
6076
factory_girl (4.4.0)
6177
activesupport (>= 3.0.0)
6278
factory_girl_rails (4.4.1)
@@ -66,10 +82,17 @@ GEM
6682
i18n (~> 0.5)
6783
faraday (0.9.0)
6884
multipart-post (>= 1.2, < 3)
69-
google_drive (0.3.9)
70-
nokogiri (>= 1.4.4, != 1.5.2, != 1.5.1)
71-
oauth (>= 0.3.6)
72-
oauth2 (>= 0.5.0)
85+
google-api-client (0.7.1)
86+
addressable (>= 2.3.2)
87+
autoparse (>= 0.3.3)
88+
extlib (>= 0.9.15)
89+
faraday (>= 0.9.0)
90+
jwt (>= 0.1.5)
91+
launchy (>= 2.1.1)
92+
multi_json (>= 1.0.0)
93+
retriable (>= 1.4)
94+
signet (>= 0.5.0)
95+
uuidtools (>= 2.1.0)
7396
haml (4.0.5)
7497
tilt
7598
haml-rails (0.5.3)
@@ -88,6 +111,8 @@ GEM
88111
thor (>= 0.14, < 2.0)
89112
json (1.8.1)
90113
jwt (1.0.0)
114+
launchy (2.4.2)
115+
addressable (~> 2.3)
91116
libv8 (3.16.14.3)
92117
linkedin (0.4.7)
93118
hashie (~> 2.0)
@@ -96,6 +121,7 @@ GEM
96121
mail (2.5.4)
97122
mime-types (~> 1.16)
98123
treetop (~> 1.4.8)
124+
method_source (0.8.2)
99125
mime-types (1.25.1)
100126
mini_portile (0.6.0)
101127
minitest (5.3.5)
@@ -111,6 +137,7 @@ GEM
111137
multi_json (1.10.1)
112138
multi_xml (0.5.5)
113139
multipart-post (2.0.0)
140+
netrc (0.7.7)
114141
nokogiri (1.6.2.1)
115142
mini_portile (= 0.6.0)
116143
oauth (0.4.7)
@@ -135,6 +162,10 @@ GEM
135162
origin (2.1.1)
136163
orm_adapter (0.5.0)
137164
polyglot (0.3.5)
165+
pry (0.10.0)
166+
coderay (~> 1.1.0)
167+
method_source (~> 0.8.1)
168+
slop (~> 3.4)
138169
rack (1.5.2)
139170
rack-test (0.6.2)
140171
rack (>= 1.0)
@@ -160,6 +191,9 @@ GEM
160191
redis-namespace (1.5.0)
161192
redis (~> 3.0, >= 3.0.4)
162193
ref (1.0.5)
194+
rest_client (1.7.3)
195+
netrc (~> 0.7.7)
196+
retriable (1.4.1)
163197
rspec-core (3.0.2)
164198
rspec-support (~> 3.0.0)
165199
rspec-expectations (3.0.2)
@@ -194,6 +228,11 @@ GEM
194228
json
195229
redis (>= 3.0.6)
196230
redis-namespace (>= 1.3.1)
231+
signet (0.5.1)
232+
addressable (>= 2.2.3)
233+
faraday (>= 0.9.0.rc5)
234+
jwt (>= 0.1.5)
235+
multi_json (>= 1.0.0)
197236
simple_form (3.0.2)
198237
actionpack (~> 4.0)
199238
activemodel (~> 4.0)
@@ -202,6 +241,7 @@ GEM
202241
multi_json
203242
simplecov-html (~> 0.8.0)
204243
simplecov-html (0.8.0)
244+
slop (3.5.0)
205245
spring (1.1.3)
206246
sprockets (2.11.0)
207247
hike (~> 1.2)
@@ -229,6 +269,7 @@ GEM
229269
uglifier (2.5.1)
230270
execjs (>= 0.3.0)
231271
json (>= 1.8.0)
272+
uuidtools (2.1.4)
232273
warden (1.2.3)
233274
rack (>= 1.0)
234275

@@ -243,15 +284,18 @@ DEPENDENCIES
243284
devise_invitable
244285
factory_girl_rails
245286
faker
246-
google_drive
287+
google-api-client
288+
google_drive!
247289
haml
248290
haml-rails
249291
jbuilder (~> 2.0)
250292
jquery-rails
251293
linkedin
252294
mongoid
253295
omniauth-google-oauth2
296+
pry
254297
rails (= 4.1.2)
298+
rest_client
255299
rspec-rails
256300
sass-rails (~> 4.0.3)
257301
sdoc (~> 0.4.0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the callbacks controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

app/controllers/application_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base
22
# Prevent CSRF attacks by raising an exception.
33
# For APIs, you may want to use :null_session instead.
44
protect_from_forgery with: :exception
5+
56
end
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
class CallbacksController < ApplicationController
2+
3+
def index
4+
#Token.delete_all
5+
tkn = Token.first
6+
7+
if tkn.nil?
8+
redirect_to "/auth/google" and return
9+
else
10+
if tkn['spreadsheet_id'].nil?
11+
@ws = getSheet(tkn)
12+
render action: 'create' and return
13+
end
14+
end
15+
@sheets = Token.all.to_a
16+
end
17+
18+
def omniauth
19+
tkn = Token.new
20+
req = request.env["omniauth.auth"]
21+
auth_token = request.env["omniauth.auth"].fetch("credentials")
22+
tkn["access_token"] = auth_token.fetch("token")
23+
tkn["refresh_token"] = auth_token.fetch("refresh_token")
24+
tkn["expires_at"] = auth_token.fetch("expires_at")
25+
26+
if tkn.save
27+
@ws = getSheet(tkn)
28+
else
29+
@ws = {error: "not able to save"}
30+
end
31+
32+
render action: 'create' and return
33+
end
34+
35+
def getSheet(tkn)
36+
client = Google::APIClient.new
37+
client.authorization.access_token = tkn['access_token']
38+
#session = GoogleDrive.login_with_oauth(tkn["access_token"])
39+
drive = client.discovered_api('drive', 'v3')
40+
41+
wks = client.execute(
42+
api_method: drive.files.list,
43+
parameters: {},
44+
headers: {'Content-Type'=>'application/json'}
45+
)
46+
JSON.parse(wks.data.to_json)
47+
end
48+
49+
def setSheet
50+
tkn = Token.first
51+
tkn['spreadsheet_id'] = params['id']
52+
tkn.save
53+
54+
insertEmailInDatabase(tkn)
55+
56+
redirect_to users_path
57+
end
58+
59+
def insertEmailInDatabase(tkn)
60+
@tkn = tkn
61+
session = GoogleDrive.login_with_oauth(@tkn["access_token"])
62+
@ws = session.spreadsheet_by_key(@tkn['spreadsheet_id']).worksheets[0]
63+
64+
rowcount = @ws.rows.count
65+
usercount = User.count
66+
#if rowcount > usercount
67+
(rowcount).times do |i|
68+
User.create(email_id: @ws[i + 1, 1], # + usercount, 1],
69+
is_subscribed: true,
70+
joined_on: Date.today,
71+
source: "Google Spreadsheet")
72+
end
73+
#end
74+
75+
#render action: "test"
76+
end
77+
78+
def refresh_token(tkn)
79+
data = {
80+
client_id: ENV['GOOGLE_ID'],
81+
client_secret: ENV['GOOGLE_KEY'],
82+
refresh_token: tkn['refresh_token'],
83+
grant_type: 'refresh_token'
84+
}
85+
re = ActiveSupport::JSON.decode(RestClient.post 'https://accounts.google.com/o/oauth2/token', data)
86+
=begin
87+
client = Google::APIClient.new
88+
client.authorization.access_token = tkn['access_token']
89+
client.authorization.client_id = ENV["GOOGLE_ID"]
90+
client.authorization.client_secret = ENV["GOOGLE_KEY"]
91+
client.authorization.refresh_token = tkn["refresh_token"]
92+
re = client.authorization.update_token!
93+
=end
94+
sheets = Token.where(spreadsheet_id: tkn['spreadsheet_id'])[0]
95+
sheets['access_token'] = re['access_token']
96+
sheets['expires_at'] = (Time.now + re['expires_in'].second).localtime
97+
sheets.save
98+
99+
re
100+
end
101+
102+
def update
103+
tkn = Token.where(spreadsheet_id: params['id'])
104+
@tkn = tkn[0]
105+
unless Time.now > tkn[0]['expires_at']
106+
@ref = refresh_token tkn[0]
107+
end
108+
tkn = Token.where(spreadsheet_id: params['id'])
109+
insertEmailInDatabase(tkn[0])
110+
redirect_to users_path
111+
#render action: 'test'
112+
end
113+
114+
end

app/controllers/home_controller.rb

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
class HomeController < ApplicationController
22
def index
33
end
4+
5+
def add_from_google
6+
@sheets = Token.first
7+
@client = Google::APIClient.new
8+
@client.authorization.access_token = @sheets['access_token']
9+
@client.authorization.client_id = ENV["GOOGLE_ID"]
10+
@client.authorization.client_secret = ENV["GOOGLE_KEY"]
11+
@client.authorization.refresh_token = @sheets["refresh_token"]
12+
@re = @client.auto_refresh_token
13+
#session = GoogleDrive.login_with_oauth(tkn["access_token"])
14+
drive = @client.discovered_api('drive', 'v2')
15+
16+
@wks = @client.execute(
17+
api_method: drive.files.watch,
18+
parameters: {fileId: @sheets["spreadsheet_id"]},
19+
headers: {'Content-Type'=>'application/json'}
20+
)
21+
end
422
end

app/helpers/callbacks_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module CallbacksHelper
2+
end

app/models/token.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Token
2+
include Mongoid::Document
3+
4+
field :spreadsheet_id, type: String
5+
field :refresh_token, type: String
6+
field :expires_at, type: Time
7+
field :access_token, type: String
8+
end

app/views/callbacks/create.html.haml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-count = @ws["items"].count
2+
-count.times do |i|
3+
=link_to @ws["items"][i]["title"], set_sheet_path(@ws["items"][i]["id"]) if @ws["items"][i]["mimeType"].include?("spreadsheet")
4+
%br

app/views/callbacks/createv1.html.erb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<% #@ws['items'].each do |i| %>
2+
<% #i.each do |j| %>
3+
<%#=j%>
4+
<%# end %>
5+
<%# end %>
6+
7+
<% @ws['items'].each do |i| %>
8+
<%= link_to i['title'], set_sheet_path(i['id']) if i['mimeType'].include?("spreadsheet") %><br>
9+
<% end %>

app/views/callbacks/failure.html.haml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is callbacks#failure

app/views/callbacks/index.html.haml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=@msg
2+
=#debug @ref
3+
4+
%br
5+
= Time.now
6+
%table.table
7+
-@sheets.count.times do |i|
8+
%tr
9+
%td
10+
= @sheets[i]["access_token"]
11+
%td
12+
= link_to "Update", callback_update_path(@sheets[i]["spreadsheet_id"])
13+
%td
14+
= @sheets[i]["expires_at"]

app/views/callbacks/setSheet.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<% @ws.rows.each do |r| %>
2+
<% r.each do |c| %>
3+
<%= c %><br>
4+
<%end%>
5+
<%end%>

app/views/callbacks/test.html.haml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=debug @tkn
2+
=debug @ref.class
3+
=debug @ref
4+
=debug @tkn['expires_at'].localtime

0 commit comments

Comments
 (0)