Skip to content

Commit 173ee4f

Browse files
committed
Code was re organised #11
1 parent f9f49d2 commit 173ee4f

27 files changed

+434
-44
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ build/
4848
/log/
4949
/tmp
5050
*.log
51+
test.html.haml

Gemfile

+9-5
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,20 @@ gem 'bootstrap-sass'
7070

7171
# The following gems for testing purpose in development and testing environment
7272
group :development, :test do
73-
# Rspec is used to write the test cases
73+
# Rspec is used to write the test cases
7474
gem 'rspec-rails'
75-
# Use factory girl to pass random data for test cases
75+
# Use factory girl to pass random data for test cases
7676
gem 'factory_girl_rails'
77-
# Use faker to generate fake strings and data
77+
# Use faker to generate fake strings and data
7878
gem 'faker'
79-
# Use to clean database after executing a test case
79+
# Use to clean database after executing a test case
8080
gem 'database_cleaner'
81-
# Use to track how much code has been tested
81+
# Use to track how much code has been tested
8282
gem 'simplecov'
83+
# Webmock to stub http requests
84+
gem 'webmock'
85+
# VCR to record the responses from web and replay them when needed
86+
gem 'vcr'
8387
end
8488

8589
gem 'simple_form'

Gemfile.lock

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ GEM
5858
execjs
5959
coffee-script-source (1.7.0)
6060
connection_pool (2.0.0)
61+
crack (0.4.2)
62+
safe_yaml (~> 1.0.0)
6163
database_cleaner (1.3.0)
6264
devise (3.2.4)
6365
bcrypt (~> 3.0)
@@ -210,6 +212,7 @@ GEM
210212
rspec-mocks (~> 3.0.0)
211213
rspec-support (~> 3.0.0)
212214
rspec-support (3.0.2)
215+
safe_yaml (1.0.3)
213216
sass (3.2.19)
214217
sass-rails (4.0.3)
215218
railties (>= 4.0.0, < 5.0)
@@ -270,8 +273,12 @@ GEM
270273
execjs (>= 0.3.0)
271274
json (>= 1.8.0)
272275
uuidtools (2.1.4)
276+
vcr (2.9.2)
273277
warden (1.2.3)
274278
rack (>= 1.0)
279+
webmock (1.18.0)
280+
addressable (>= 2.3.6)
281+
crack (>= 0.3.2)
275282

276283
PLATFORMS
277284
ruby
@@ -307,3 +314,5 @@ DEPENDENCIES
307314
therubyracer
308315
turbolinks
309316
uglifier (>= 1.3.0)
317+
vcr
318+
webmock
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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/
4+
xyz = ->
5+
alert "welcome"
6+
return

app/assets/stylesheets/bootstrap_config.scss

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@import "bootstrap";
2+
23
body
34
{
45
padding-top: 80px;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the spreadsheets 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,5 +2,4 @@ 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-
65
end

app/controllers/callbacks_controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def setSheet
5454
end
5555

5656
def insertEmailInDatabase(tkn)
57-
@tkn = tkn
58-
session = GoogleDrive.login_with_oauth(@tkn["access_token"])
59-
@ws = session.spreadsheet_by_key(@tkn['spreadsheet_id']).worksheets[0]
57+
@tkn = tkn
58+
session = GoogleDrive.login_with_oauth(@tkn["access_token"])
59+
@ws = session.spreadsheet_by_key(@tkn['spreadsheet_id']).worksheets[0]
6060

6161
rowcount = @ws.rows.count
6262

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class SpreadsheetsController < ApplicationController
2+
include GoogleSpreadsheets
3+
4+
def index
5+
@spreadsheets = Spreadsheet.all.to_a
6+
end
7+
8+
def new
9+
@req = params
10+
=begin
11+
if params[:access_token]
12+
spreadsheet = Spreadsheet.where(access_token: params['access_token'])[0]
13+
else
14+
spreadsheet = Spreadsheet.new
15+
spreadsheet.add_tokens(request.env['omniauth.auth'].fetch('credentials'))
16+
end
17+
18+
if spreadsheet.save
19+
# Spreadsheets from google
20+
@spreadsheets = get_spreadsheets(spreadsheet)
21+
@token = spreadsheet.access_token
22+
@msg = 'work'
23+
else
24+
@msg = 'no work'
25+
# Handle if data does not get saved
26+
end
27+
=end
28+
end
29+
30+
def edit
31+
token = spreadsheet_params['token']
32+
spreadsheet = Spreadsheet.where(access_token: token)[0]
33+
spreadsheet.add_spreadsheet_credentials(spreadsheet_params)
34+
#binding.pry
35+
spreadsheet.save
36+
37+
@spreadsheets = Spreadsheet.all.to_a
38+
39+
render action: 'index'
40+
end
41+
42+
def update
43+
spreadsheet = Spreadsheet.find(params['id'])
44+
@worksheet = get_worksheets(spreadsheet)
45+
User.add_users_from_worksheet(@worksheet)
46+
redirect_to users_path
47+
end
48+
49+
def destroy
50+
Spreadsheet.find(params['id']).delete
51+
redirect_to spreadsheets_path
52+
end
53+
54+
def spreadsheet_params
55+
params.permit(:title, :id, :token)
56+
end
57+
end

app/helpers/spreadsheets_helper.rb

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

app/models/spreadsheet.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Spreadsheet
2+
include Mongoid::Document
3+
4+
field :spreadsheet_id, type: String
5+
field :spreadsheet_title, type: String
6+
field :refresh_token, type: String
7+
field :expires_at, type: Time
8+
field :access_token, type: String
9+
10+
validates :access_token, uniqueness: true
11+
12+
def add_tokens(tokens = {})
13+
self['access_token'] = tokens.fetch('token')
14+
self['refresh_token'] = tokens.fetch('refresh_token')
15+
self['expires_at'] = tokens.fetch('expires_at')
16+
end
17+
18+
def add_spreadsheet_credentials(credentials = {})
19+
#binding.pry
20+
self['spreadsheet_id'] = credentials['id']
21+
self['spreadsheet_title'] = credentials['title']
22+
end
23+
24+
def access_token
25+
self['access_token']
26+
end
27+
28+
end

app/models/user.rb

+8
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ class User
1414

1515
belongs_to :newsletter, counter_cache: :users_count
1616

17+
def self.add_users_from_worksheet(worksheet, column = 1)
18+
worksheet.rows.count.times do |i|
19+
User.create(email_id: worksheet[i + 1, column],
20+
is_subscribed: true,
21+
joined_on: Date.today,
22+
source: 'Google Spreadsheet')
23+
end
24+
end
1725
end

app/views/layouts/application.html.haml

+24-24
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@
1212
.navbar-brand
1313
%h4 LightAir
1414

15-
%ul.nav.navbar-nav
16-
%li.active
17-
%a{href: "#{home_index_path}"}
18-
%h4 Home
19-
%li
20-
%a{href: "#"}
21-
%h4 Newsletters
22-
%li.dropdown
23-
%a.dropdown-toggle{"data-toggle" => "dropdown", href: "#"}
24-
%h4
25-
Add Users
26-
%span.caret
27-
%ul.dropdown-menu{role: "menu", "aria-labelledby" => "dropdownMenu1" }
28-
%li
29-
%a{role: "menuitem", tabindex: "-1", href: "#{new_user_path}"}Manually
30-
%li
31-
%a{role: "menuitem", tabindex: "-2", href: "#{callback_index_path}"}Google Spreadsheets
32-
%li
33-
%a{role: "menuitem", tabindex: "-1", href: "#{linkedin_path}"}Linkedin Connections
34-
%li
35-
%a{href: "#{users_path}"}
36-
%h4 All Users
15+
%div.nav-collapse
16+
%ul.nav.navbar-nav
17+
%li.active
18+
%a{href: "#{home_index_path}"}
19+
%h4 Home
20+
%li
21+
%a{href: "#"}
22+
%h4 Newsletters
23+
%li
24+
%a{href: "#{users_path}"}
25+
%h4 Show Users
26+
%li.dropdown
27+
%a.dropdown-toggle{"data-toggle" => "dropdown"}
28+
%h4
29+
Add Users
30+
%span.caret
31+
%ul.dropdown-menu{role: "menu", "aria-labelledby" => "dropdownMenu1" }
32+
%li
33+
%a{role: "menuitem", tabindex: "-1", href: "#{new_user_path}"}Add Manually
34+
%li
35+
%a{role: "menuitem", tabindex: "-2", href: "#{spreadsheets_path}"}Google Spreadsheets
3736

3837

3938
%ul.nav.navbar-nav.navbar-right
4039
%li.active
4140
%a{href: "#"}
42-
Subscriber count :
43-
=User.count
41+
%h4
42+
Subscriber count :
43+
=User.count
4444
%div.container
4545
%div.row
4646
%div.col-sm-9= yield

app/views/spreadsheets/edit.html.haml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
=debug @sp
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
%h4
2+
=link_to 'Add New Spreadsheet', 'auth/google/'
3+
4+
%table.table.table-striped
5+
%tr.info
6+
%td
7+
%strong #
8+
%td
9+
%strong
10+
Spreadsheets (
11+
=Spreadsheet.count
12+
)
13+
%td
14+
%strong Operations
15+
- i = 0
16+
- @spreadsheets.each do |ss|
17+
%tr
18+
%td
19+
= i = i + 1
20+
%td
21+
- if ss['spreadsheet_title']
22+
=ss['spreadsheet_title']
23+
-else
24+
=link_to 'Spreadsheet not initialised', new_spreadsheet_path(access_token: ss['access_token'])
25+
%td
26+
=link_to 'Delete', spreadsheet_path(ss), method: :delete
27+
-if ss['spreadsheet_title']
28+
|
29+
=link_to 'Update', spreadsheet_path(ss), method: :patch
30+
31+

app/views/spreadsheets/new.html.haml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=@req
2+
%br
3+
-#count = @spreadsheets.count
4+
=#count
5+
-#count.times do |i|
6+
=#link_to @spreadsheets[i]['title'], edit_spreadsheet_path(@spreadsheets[i]['id'], title: @spreadsheets[i]['title'], token: @token) if @spreadsheets[i]['mimeType'].include?('spreadsheet')
7+
%br

config/application.rb

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
module Lightair
1717
class Application < Rails::Application
1818
config.assets.precompile += %w(*.png, *.jpg, *.jpeg, *.gif)
19+
config.autoload_paths += Dir["#{config.root}/lib/**/"]
1920
# Settings in config/environments/* take precedence over those specified here.
2021
# Application configuration should go into files in config/initializers
2122
# -- all .rb files in that directory are automatically loaded.

config/initializers/google_oauth.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
Rails.application.config.middleware.use OmniAuth::Builder do
22
provider :google_oauth2, ENV['GOOGLE_ID'], ENV['GOOGLE_KEY'],
33
{
4-
name: "google",
4+
name: 'google',
55
scope: 'userinfo.profile,userinfo.email,drive,https://spreadsheets.google.com/feeds',
66
prompt: 'consent',
7-
access_type: "offline",
7+
access_type: 'offline',
8+
#redirect_uri: 'http://localhost:8080/auth/google/spreadsheets'
89
}
9-
10-
#provider :linkedin, '75cmlzl0cpmwa2', 'I5y0aRBkfpgMmTqp', redirect_uri: 'http://localhost:8080/auth/linkedin/callback', scope:'r_emailaddress r_network r_contactinfo rw_company_admin rw_nus rw_groups w_messages r_basicprofile r_fullprofile'
1110
end

config/routes.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
Rails.application.routes.draw do
2-
2+
=begin
33
get '/home/sheets', to: 'home#add_from_google', as: 'add_from_google'
44
get '/auth/:provider/callback', to: 'callbacks#omniauth'
55
get '/linkedin', to: 'callbacks#linkedin', as: 'linkedin'
66
get '/callbacks/sheet/:id', to: 'callbacks#setSheet', as: 'set_sheet'
77
get '/callbacks/index', to: 'callbacks#index', as: 'callback_index'
88
get '/callbacks/update/:id', to: 'callbacks#update', as: 'callback_update'
9-
10-
resources :users
11-
resources :newsletters
12-
resources :home
9+
=end
10+
get '/auth/:provider/callback', to: 'spreadsheets#new'
11+
resources :users, :newsletters, :home, :spreadsheets
1312

14-
root :to => "home#index"
13+
root :to => 'home#index'
1514

1615
# The priority is based upon order of creation: first created -> highest priority.
1716
# See how all your routes lay out with "rake routes".

0 commit comments

Comments
 (0)