turboauth 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.
Files changed (2) hide show
  1. data/lib/turboauth.rb +130 -0
  2. metadata +47 -0
data/lib/turboauth.rb ADDED
@@ -0,0 +1,130 @@
1
+ class TurboAuth
2
+ TURBOAUTH_ROOT = File.expand_path('../', File.dirname(__FILE__))
3
+
4
+ def self.facebook
5
+ self.add_gems
6
+ self.add_to_gitignore
7
+ self.add_facebook_yml
8
+ self.add_routes
9
+ self.add_omniauth_rb
10
+ self.add_facebook_rb
11
+ self.add_coffee_script
12
+ self.add_test_controller
13
+ self.add_to_app_controller
14
+ self.add_sessions_controller
15
+ self.edit_application_layout
16
+ self.create_index_view
17
+ self.add_user_migration
18
+ self.migrate
19
+ self.add_to_user_model
20
+ self.bundle
21
+ end
22
+
23
+ def self.add_gems
24
+ File.open('Gemfile', 'a') do |file|
25
+ file.puts "\n#omniauth gems\ngem 'omniauth'\ngem 'omniauth-facebook', '1.4.0'\n"
26
+ end
27
+ end
28
+
29
+ def self.add_to_gitignore
30
+ File.open('.gitignore', 'a') do |file|
31
+ file.puts "\n#ignore file with facebook keys\nfacebook.yml\n"
32
+ end
33
+ end
34
+
35
+ def self.add_facebook_yml
36
+ File.new('config/facebook.yml', 'w')
37
+ File.open('config/facebook.yml', 'a') do |file|
38
+ file.puts "development:\n app_id: PUT_YOUR_APP_ID_HERE\n secret: PUT_YOUR_APP_SECRET_HERE\n\ntest:\n app_id: PUT_YOUR_APP_ID_HERE\n secret: PUT_YOUR_APP_SECRET_HERE\n\nproduction:\n app_id: PUT_YOUR_APP_ID_HERE\n secret: PUT_YOUR_APP_SECRET_HERE"
39
+ end
40
+ end
41
+
42
+ def self.add_routes
43
+ File.open('config/routes.rb', 'a+') { |source_file|
44
+ contents = source_file.read
45
+ newlines = "Application.routes.draw do\nroot 'test#index'\n\nmatch 'auth/:provider/callback', to: 'sessions#create', via: [:get, :post]\nmatch 'auth/failure', to: redirect('/'), via: [:get, :post]\nmatch 'signout', to: 'sessions#destroy', as: 'signout', via: [:get, :post]\n"
46
+ contents.gsub!(/[A][p][p][l][i][c][a][t][i][o][n][.][r][o][u][t][e][s][.][d][r][a][w][ ][d][o]/, newlines)
47
+ File.open('config/routes.rb', "w+") { |f| f.write(contents) }
48
+ }
49
+ end
50
+
51
+ def self.add_facebook_rb
52
+ File.new('config/initializers/facebook.rb', 'w')
53
+ File.open('config/initializers/facebook.rb', 'a') do |file|
54
+ file.puts "FACEBOOK_CONFIG = YAML.load_file(\"\#{::Rails.root}/config/facebook.yml\")[::Rails.env]\n"
55
+ end
56
+ end
57
+
58
+ def self.add_omniauth_rb
59
+ File.new('config/initializers/omniauth.rb', 'w')
60
+ File.open('config/initializers/omniauth.rb', 'a') do |file|
61
+ file.puts "OmniAuth.config.logger = Rails.logger\n\nRails.application.config.middleware.use OmniAuth::Builder do\n provider:facebook, FACEBOOK_CONFIG['app_id'], FACEBOOK_CONFIG['secret']\nend\n"
62
+ end
63
+ end
64
+
65
+ def self.add_coffee_script
66
+ File.new('app/assets/javascripts/facebook.js.coffee.erb', 'w')
67
+ File.open('app/assets/javascripts/facebook.js.coffee.erb', 'a') do |file|
68
+ file.puts "jQuery ->\n $('body').prepend('<div id=\"fb-root\"></div>')\n\n $.ajax\n url: '\#{window.location.protocol}//connect.facebook.net/en_US/all.js'\n dataType: 'script'\n cache: true\n\n window.fbAsyncInit = ->\n FB.init(appId: <%= FACEBOOK_CONFIG['app_id'] %>, cookie:true)\n\n $('#sign_in').click (e) ->\n e.preventDefault()\n FB.login (response) ->\n window.location = '/auth/facebook/callback' if response.authResponse\n\n $('#sign_out').click (e) ->\n FB.getLoginStatus (response) ->\n FB.logout() if response.authResponse \n true"
69
+ end
70
+ end
71
+
72
+ def self.add_test_controller
73
+ File.new('app/controllers/test_controller.rb', 'w')
74
+ File.open('app/controllers/test_controller.rb', 'a') do |file|
75
+ file.puts "class TestController < ApplicationController\n\n def index\n end\n\nend"
76
+ end
77
+ end
78
+
79
+ def self.add_to_app_controller
80
+ File.open('app/controllers/application_controller.rb', 'a+') { |source_file|
81
+ contents = source_file.read
82
+ newlines = " private\n def current_user\n @current_user ||= User.find(session[:user_id]) if session[:user_id]\n end\n helper_method :current_user\n\nend"
83
+ contents.gsub!(/[e][n][d]/, newlines)
84
+ File.open('app/controllers/application_controller.rb', "w+") { |f| f.write(contents) }
85
+ }
86
+ end
87
+
88
+ def self.add_sessions_controller
89
+ File.new('app/controllers/sessions_controller.rb', 'w')
90
+ File.open('app/controllers/sessions_controller.rb', 'a') do |file|
91
+ file.puts "class SessionsController < ApplicationController\n\n def create\n user = User.from_omniauth(env['omniauth.auth'])\n session[:user_id] = user.id\n redirect_to root_url\n end\n\n def destroy\n session[:user_id] = nil\n redirect_to root_url\n end\n\nend"
92
+ end
93
+ end
94
+
95
+ def self.add_to_user_model
96
+ File.open('app/models/user.rb', 'a+') { |source_file|
97
+ contents = source_file.read
98
+ newlines = "\n def self.from_omniauth(auth)\n where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|\n user.provider = auth.provider\n user.uid = auth.uid\n user.name = auth.info.name\n user.oauth_token = auth.credentials.oauth_token\n user.oauth_expires_at = Time.at(auth.credentials.expires_at)\n user.save!\n end\n end\n\nend"
99
+ contents.gsub!(/[e][n][d]/, newlines)
100
+ File.open('app/models/user.rb', "w+") { |f| f.write(contents) }
101
+ }
102
+ end
103
+
104
+ def self.edit_application_layout
105
+ File.open('app/views/layouts/application.html.erb', 'a+') { |source_file|
106
+ contents = source_file.read
107
+ newlines = "</title>\n\n <div id='user-widget'>\n <% if current_user %>\n Welcome <strong> <%= current_user.name %></strong>!\n <%= link_to 'Sign Out', signout_path, id: 'sign_out' %>\n <% else %>\n <%= link_to 'Sign in with Facebook', '/auth/facebook', id: 'sign_in' %>\n <% end %>\n </div>\n"
108
+ contents.gsub!(/[<][\/][t][i][t][l][e][>]/, newlines)
109
+ File.open('app/views/layouts/application.html.erb', "w+") { |f| f.write(contents) }
110
+ }
111
+ end
112
+
113
+ def self.create_index_view
114
+ Dir.mkdir('app/views/test')
115
+ File.open('app/views/test/index.html.erb', 'a')
116
+ end
117
+
118
+ def self.add_user_migration
119
+ system ( "rails g model user provider uid name oauth_token oauth_expires_at:datetime" )
120
+ end
121
+
122
+ def self.migrate
123
+ system ( "rake db:migrate" )
124
+ end
125
+
126
+ def self.bundle
127
+ system ( "bundle" )
128
+ end
129
+
130
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turboauth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Owens
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem to run after creating a new rails app to setup basic authentication
15
+ via oauth with the least effort possible on the part of the user.
16
+ email: samueldowens@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/turboauth.rb
22
+ homepage: http://rubygems.org/gems/turboauth
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.25
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: A gem for speedy auth setup in new Rails apps.
47
+ test_files: []