socialite 0.0.1.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/.autotest +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +10 -0
  5. data/.yardopts +8 -0
  6. data/Gemfile +8 -0
  7. data/Gemfile.lock +208 -0
  8. data/LICENSE +176 -0
  9. data/README.md +63 -0
  10. data/Rakefile +34 -0
  11. data/app/assets/images/socialite/.gitkeep +0 -0
  12. data/app/assets/javascripts/socialite/.gitkeep +0 -0
  13. data/app/assets/stylesheets/socialite/socialite.css +40 -0
  14. data/app/assets/stylesheets/socialite.css +7 -0
  15. data/app/controllers/socialite/identities_controller.rb +53 -0
  16. data/app/controllers/socialite/session_controller.rb +28 -0
  17. data/app/controllers/socialite/user_controller.rb +39 -0
  18. data/app/helpers/socialite/authentication_helper.rb +15 -0
  19. data/app/models/socialite/facebook_identity.rb +7 -0
  20. data/app/models/socialite/identity.rb +6 -0
  21. data/app/models/socialite/user.rb +42 -0
  22. data/app/views/socialite/identities/_identities.html.haml +14 -0
  23. data/app/views/socialite/session/new.html.haml +14 -0
  24. data/app/views/socialite/user/_form.html.haml +13 -0
  25. data/app/views/socialite/user/edit.html.haml +1 -0
  26. data/app/views/socialite/user/show.html.haml +16 -0
  27. data/config/initializers/simple_form.rb +90 -0
  28. data/config/locales/simple_form.en.yml +23 -0
  29. data/config/routes.rb +10 -0
  30. data/db/migrate/20110914215410_create_users.rb +14 -0
  31. data/db/migrate/20110925224222_create_identities.rb +26 -0
  32. data/db/migrate/20110926005551_create_facebook_identities.rb +12 -0
  33. data/features/authentication/facebook_signin.feature +5 -0
  34. data/features/authentication/twitter_signin.feature +5 -0
  35. data/features/identities/facebook_management.feature +14 -0
  36. data/features/identities/twitter_management.feature +7 -0
  37. data/features/registration/facebook_signup.feature +10 -0
  38. data/features/registration/twitter_signup.feature +0 -0
  39. data/features/step_definitions/authentication_steps.rb +31 -0
  40. data/features/step_definitions/common_steps.rb +13 -0
  41. data/features/step_definitions/identity_steps.rb +5 -0
  42. data/features/step_definitions/web_steps.rb +254 -0
  43. data/features/support/env.rb +58 -0
  44. data/features/support/hooks.rb +3 -0
  45. data/features/support/omniauth.rb +31 -0
  46. data/features/support/paths.rb +34 -0
  47. data/features/support/selectors.rb +39 -0
  48. data/lib/socialite/api_wrappers/facebook.rb +67 -0
  49. data/lib/socialite/api_wrappers/twitter.rb +19 -0
  50. data/lib/socialite/base_identity.rb +96 -0
  51. data/lib/socialite/controller_support.rb +136 -0
  52. data/lib/socialite/engine.rb +32 -0
  53. data/lib/socialite/service_config.rb +14 -0
  54. data/lib/socialite/version.rb +3 -0
  55. data/lib/socialite.rb +37 -0
  56. data/lib/tasks/.gitkeep +0 -0
  57. data/lib/tasks/cucumber.rake +65 -0
  58. data/lib/tasks/socialite_tasks.rake +4 -0
  59. data/script/cucumber +10 -0
  60. data/script/rails +6 -0
  61. data/socialite.gemspec +39 -0
  62. data/spec/dummy/Rakefile +7 -0
  63. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  64. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  65. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  66. data/spec/dummy/app/controllers/home_controller.rb +11 -0
  67. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  68. data/spec/dummy/app/views/home/index.html.haml +12 -0
  69. data/spec/dummy/app/views/home/show.html.haml +6 -0
  70. data/spec/dummy/app/views/layouts/application.html.erb +18 -0
  71. data/spec/dummy/config/application.rb +48 -0
  72. data/spec/dummy/config/boot.rb +20 -0
  73. data/spec/dummy/config/database.yml +9 -0
  74. data/spec/dummy/config/environment.rb +5 -0
  75. data/spec/dummy/config/environments/development.rb +30 -0
  76. data/spec/dummy/config/environments/production.rb +60 -0
  77. data/spec/dummy/config/environments/test.rb +42 -0
  78. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  79. data/spec/dummy/config/initializers/inflections.rb +10 -0
  80. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  81. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  82. data/spec/dummy/config/initializers/session_store.rb +8 -0
  83. data/spec/dummy/config/initializers/socialite.rb +6 -0
  84. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  85. data/spec/dummy/config/locales/en.yml +5 -0
  86. data/spec/dummy/config/routes.rb +11 -0
  87. data/spec/dummy/config.ru +4 -0
  88. data/spec/dummy/db/schema.rb +43 -0
  89. data/spec/dummy/public/404.html +26 -0
  90. data/spec/dummy/public/422.html +26 -0
  91. data/spec/dummy/public/500.html +26 -0
  92. data/spec/dummy/public/favicon.ico +0 -0
  93. data/spec/dummy/script/rails +6 -0
  94. data/spec/factories/facebook.rb +5 -0
  95. data/spec/factories/identity.rb +9 -0
  96. data/spec/factories/twitter.rb +6 -0
  97. data/spec/factories/user.rb +16 -0
  98. data/spec/models/facebook_spec.rb +28 -0
  99. data/spec/models/identity_spec.rb +9 -0
  100. data/spec/models/user_spec.rb +27 -0
  101. data/spec/spec_helper.rb +29 -0
  102. data/spec/support/.gitkeep +0 -0
  103. data/spec/support/database_loader.rb +13 -0
  104. data/spec/support/databases.yml +14 -0
  105. data/spec/support/identity_shared_example.rb +67 -0
  106. metadata +409 -0
@@ -0,0 +1,254 @@
1
+ # TL;DR: YOU SHOULD DELETE THIS FILE
2
+ #
3
+ # This file was generated by Cucumber-Rails and is only here to get you a head start
4
+ # These step definitions are thin wrappers around the Capybara/Webrat API that lets you
5
+ # visit pages, interact with widgets and make assertions about page content.
6
+ #
7
+ # If you use these step definitions as basis for your features you will quickly end up
8
+ # with features that are:
9
+ #
10
+ # * Hard to maintain
11
+ # * Verbose to read
12
+ #
13
+ # A much better approach is to write your own higher level step definitions, following
14
+ # the advice in the following blog posts:
15
+ #
16
+ # * http://benmabey.com/2008/05/19/imperative-vs-declarative-scenarios-in-user-stories.html
17
+ # * http://dannorth.net/2011/01/31/whose-domain-is-it-anyway/
18
+ # * http://elabs.se/blog/15-you-re-cuking-it-wrong
19
+ #
20
+
21
+
22
+ require 'uri'
23
+ require 'cgi'
24
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
25
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "selectors"))
26
+
27
+ module WithinHelpers
28
+ def with_scope(locator)
29
+ locator ? within(*selector_for(locator)) { yield } : yield
30
+ end
31
+ end
32
+ World(WithinHelpers)
33
+
34
+ # Single-line step scoper
35
+ When /^(.*) within (.*[^:])$/ do |step, parent|
36
+ with_scope(parent) { When step }
37
+ end
38
+
39
+ # Multi-line step scoper
40
+ When /^(.*) within (.*[^:]):$/ do |step, parent, table_or_string|
41
+ with_scope(parent) { When "#{step}:", table_or_string }
42
+ end
43
+
44
+ Given /^(?:|I )am on (.+)$/ do |page_name|
45
+ visit path_to(page_name)
46
+ end
47
+
48
+ When /^(?:|I )go to (.+)$/ do |page_name|
49
+ visit path_to(page_name)
50
+ end
51
+
52
+ When /^(?:|I )press "([^"]*)"$/ do |button|
53
+ click_button(button)
54
+ end
55
+
56
+ When /^(?:|I )follow "([^"]*)"$/ do |link|
57
+ click_link(link)
58
+ end
59
+
60
+ When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
61
+ fill_in(field, :with => value)
62
+ end
63
+
64
+ When /^(?:|I )fill in "([^"]*)" for "([^"]*)"$/ do |value, field|
65
+ fill_in(field, :with => value)
66
+ end
67
+
68
+ # Use this to fill in an entire form with data from a table. Example:
69
+ #
70
+ # When I fill in the following:
71
+ # | Account Number | 5002 |
72
+ # | Expiry date | 2009-11-01 |
73
+ # | Note | Nice guy |
74
+ # | Wants Email? | |
75
+ #
76
+ # TODO: Add support for checkbox, select or option
77
+ # based on naming conventions.
78
+ #
79
+ When /^(?:|I )fill in the following:$/ do |fields|
80
+ fields.rows_hash.each do |name, value|
81
+ When %{I fill in "#{name}" with "#{value}"}
82
+ end
83
+ end
84
+
85
+ When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
86
+ select(value, :from => field)
87
+ end
88
+
89
+ When /^(?:|I )check "([^"]*)"$/ do |field|
90
+ check(field)
91
+ end
92
+
93
+ When /^(?:|I )uncheck "([^"]*)"$/ do |field|
94
+ uncheck(field)
95
+ end
96
+
97
+ When /^(?:|I )choose "([^"]*)"$/ do |field|
98
+ choose(field)
99
+ end
100
+
101
+ When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"$/ do |path, field|
102
+ attach_file(field, File.expand_path(path))
103
+ end
104
+
105
+ Then /^(?:|I )should see "([^"]*)"$/ do |text|
106
+ if page.respond_to? :should
107
+ page.should have_content(text)
108
+ else
109
+ assert page.has_content?(text)
110
+ end
111
+ end
112
+
113
+ Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp|
114
+ regexp = Regexp.new(regexp)
115
+
116
+ if page.respond_to? :should
117
+ page.should have_xpath('//*', :text => regexp)
118
+ else
119
+ assert page.has_xpath?('//*', :text => regexp)
120
+ end
121
+ end
122
+
123
+ Then /^(?:|I )should not see "([^"]*)"$/ do |text|
124
+ if page.respond_to? :should
125
+ page.should have_no_content(text)
126
+ else
127
+ assert page.has_no_content?(text)
128
+ end
129
+ end
130
+
131
+ Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp|
132
+ regexp = Regexp.new(regexp)
133
+
134
+ if page.respond_to? :should
135
+ page.should have_no_xpath('//*', :text => regexp)
136
+ else
137
+ assert page.has_no_xpath?('//*', :text => regexp)
138
+ end
139
+ end
140
+
141
+ Then /^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/ do |field, parent, value|
142
+ with_scope(parent) do
143
+ field = find_field(field)
144
+ field_value = (field.tag_name == 'textarea') ? field.text : field.value
145
+ if field_value.respond_to? :should
146
+ field_value.should =~ /#{value}/
147
+ else
148
+ assert_match(/#{value}/, field_value)
149
+ end
150
+ end
151
+ end
152
+
153
+ Then /^the "([^"]*)" field(?: within (.*))? should not contain "([^"]*)"$/ do |field, parent, value|
154
+ with_scope(parent) do
155
+ field = find_field(field)
156
+ field_value = (field.tag_name == 'textarea') ? field.text : field.value
157
+ if field_value.respond_to? :should_not
158
+ field_value.should_not =~ /#{value}/
159
+ else
160
+ assert_no_match(/#{value}/, field_value)
161
+ end
162
+ end
163
+ end
164
+
165
+ Then /^the "([^"]*)" field should have the error "([^"]*)"$/ do |field, error_message|
166
+ element = find_field(field)
167
+ classes = element.find(:xpath, '..')[:class].split(' ')
168
+
169
+ form_for_input = element.find(:xpath, 'ancestor::form[1]')
170
+ using_formtastic = form_for_input[:class].include?('formtastic')
171
+ error_class = using_formtastic ? 'error' : 'field_with_errors'
172
+
173
+ if classes.respond_to? :should
174
+ classes.should include(error_class)
175
+ else
176
+ assert classes.include?(error_class)
177
+ end
178
+
179
+ if page.respond_to?(:should)
180
+ if using_formtastic
181
+ error_paragraph = element.find(:xpath, '../*[@class="inline-errors"][1]')
182
+ error_paragraph.should have_content(error_message)
183
+ else
184
+ page.should have_content("#{field.titlecase} #{error_message}")
185
+ end
186
+ else
187
+ if using_formtastic
188
+ error_paragraph = element.find(:xpath, '../*[@class="inline-errors"][1]')
189
+ assert error_paragraph.has_content?(error_message)
190
+ else
191
+ assert page.has_content?("#{field.titlecase} #{error_message}")
192
+ end
193
+ end
194
+ end
195
+
196
+ Then /^the "([^"]*)" field should have no error$/ do |field|
197
+ element = find_field(field)
198
+ classes = element.find(:xpath, '..')[:class].split(' ')
199
+ if classes.respond_to? :should
200
+ classes.should_not include('field_with_errors')
201
+ classes.should_not include('error')
202
+ else
203
+ assert !classes.include?('field_with_errors')
204
+ assert !classes.include?('error')
205
+ end
206
+ end
207
+
208
+ Then /^the "([^"]*)" checkbox(?: within (.*))? should be checked$/ do |label, parent|
209
+ with_scope(parent) do
210
+ field_checked = find_field(label)['checked']
211
+ if field_checked.respond_to? :should
212
+ field_checked.should be_true
213
+ else
214
+ assert field_checked
215
+ end
216
+ end
217
+ end
218
+
219
+ Then /^the "([^"]*)" checkbox(?: within (.*))? should not be checked$/ do |label, parent|
220
+ with_scope(parent) do
221
+ field_checked = find_field(label)['checked']
222
+ if field_checked.respond_to? :should
223
+ field_checked.should be_false
224
+ else
225
+ assert !field_checked
226
+ end
227
+ end
228
+ end
229
+
230
+ Then /^(?:|I )should be on (.+)$/ do |page_name|
231
+ current_path = URI.parse(current_url).path
232
+ if current_path.respond_to? :should
233
+ current_path.should == path_to(page_name)
234
+ else
235
+ assert_equal path_to(page_name), current_path
236
+ end
237
+ end
238
+
239
+ Then /^(?:|I )should have the following query string:$/ do |expected_pairs|
240
+ query = URI.parse(current_url).query
241
+ actual_params = query ? CGI.parse(query) : {}
242
+ expected_params = {}
243
+ expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')}
244
+
245
+ if actual_params.respond_to? :should
246
+ actual_params.should == expected_params
247
+ else
248
+ assert_equal expected_params, actual_params
249
+ end
250
+ end
251
+
252
+ Then /^show me the page$/ do
253
+ save_and_open_page
254
+ end
@@ -0,0 +1,58 @@
1
+ ENV["RAILS_ENV"] ||= "test"
2
+ require File.expand_path("../../../spec/dummy/config/environment.rb", __FILE__)
3
+ ENV["RAILS_ROOT"] ||= File.dirname(__FILE__) + "../../../spec/dummy"
4
+
5
+ # IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
6
+ # It is recommended to regenerate this file in the future when you upgrade to a
7
+ # newer version of cucumber-rails. Consider adding your own code to a new file
8
+ # instead of editing this one. Cucumber will automatically load all features/**/*.rb
9
+ # files.
10
+
11
+ require 'cucumber/rails'
12
+
13
+ # Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
14
+ # order to ease the transition to Capybara we set the default here. If you'd
15
+ # prefer to use XPath just remove this line and adjust any selectors in your
16
+ # steps to use the XPath syntax.
17
+ Capybara.default_selector = :css
18
+
19
+ # By default, any exception happening in your Rails application will bubble up
20
+ # to Cucumber so that your scenario will fail. This is a different from how
21
+ # your application behaves in the production environment, where an error page will
22
+ # be rendered instead.
23
+ #
24
+ # Sometimes we want to override this default behaviour and allow Rails to rescue
25
+ # exceptions and display an error page (just like when the app is running in production).
26
+ # Typical scenarios where you want to do this is when you test your error pages.
27
+ # There are two ways to allow Rails to rescue exceptions:
28
+ #
29
+ # 1) Tag your scenario (or feature) with @allow-rescue
30
+ #
31
+ # 2) Set the value below to true. Beware that doing this globally is not
32
+ # recommended as it will mask a lot of errors for you!
33
+ #
34
+ ActionController::Base.allow_rescue = false
35
+
36
+ # Remove/comment out the lines below if your app doesn't have a database.
37
+ # For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
38
+ begin
39
+ DatabaseCleaner.strategy = :transaction
40
+ rescue NameError
41
+ raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
42
+ end
43
+
44
+ # You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
45
+ # See the DatabaseCleaner documentation for details. Example:
46
+ #
47
+ # Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
48
+ # DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
49
+ # end
50
+ #
51
+ # Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
52
+ # DatabaseCleaner.strategy = :transaction
53
+ # end
54
+
55
+
56
+ # Run any available migration
57
+ ActiveRecord::Migrator.migrate File.expand_path('../../../spec/dummy/db/migrate/', __FILE__)
58
+
@@ -0,0 +1,3 @@
1
+ Before do
2
+ @default_user = { :email => 'foobar@example.com', :password => 'default' }
3
+ end
@@ -0,0 +1,31 @@
1
+ Before '@omniauth' do
2
+ OmniAuth.config.test_mode = true
3
+
4
+ # the symbol passed to mock_auth is the same as the name of the provider set up in the initializer
5
+ # OmniAuth.config.mock_auth[:google] = {
6
+ # "provider"=>"google",
7
+ # "uid"=>"http://xxxx.com/openid?id=118181138998978630963",
8
+ # "user_info"=>{"email"=>"test@xxxx.com", "first_name"=>"Test", "last_name"=>"User", "name"=>"Test User"}
9
+ # }
10
+
11
+ OmniAuth.config.add_mock(:facebook, {
12
+ 'uid' => '1234',
13
+ 'extra' => {
14
+ 'user_hash' => {
15
+ 'email' => 'foobar@example.com',
16
+ 'first_name' => 'John',
17
+ 'last_name' => 'Doe',
18
+ 'gender' => 'Male'
19
+ }
20
+ }
21
+ })
22
+
23
+ OmniAuth.config.add_mock(:twitter, {
24
+ :uid => '12345',
25
+ :nickname => 'zapnap'
26
+ })
27
+ end
28
+
29
+ After '@omniauth' do
30
+ OmniAuth.config.test_mode = false
31
+ end
@@ -0,0 +1,34 @@
1
+ module NavigationHelpers
2
+ # Maps a name to a path. Used by the
3
+ #
4
+ # When /^I go to (.+)$/ do |page_name|
5
+ #
6
+ # step definition in web_steps.rb
7
+ #
8
+ def path_to(page_name)
9
+ case page_name
10
+
11
+ when /^the home\s?page$/
12
+ '/'
13
+
14
+ # Add more mappings here.
15
+ # Here is an example that pulls values out of the Regexp:
16
+ #
17
+ # when /^(.*)'s profile page$/i
18
+ # user_profile_path(User.find_by_login($1))
19
+
20
+ else
21
+ begin
22
+ page_name =~ /^the (.*) page$/
23
+ path_components = $1.split(/\s+/)
24
+ self.send(path_components.push('path').join('_').to_sym)
25
+ rescue NoMethodError, ArgumentError
26
+ raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
27
+ "Now, go and add a mapping in #{__FILE__}"
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ World(NavigationHelpers)
34
+ World(Rails.application.routes.url_helpers)
@@ -0,0 +1,39 @@
1
+ module HtmlSelectorsHelpers
2
+ # Maps a name to a selector. Used primarily by the
3
+ #
4
+ # When /^(.+) within (.+)$/ do |step, scope|
5
+ #
6
+ # step definitions in web_steps.rb
7
+ #
8
+ def selector_for(locator)
9
+ case locator
10
+
11
+ when "the page"
12
+ "html > body"
13
+
14
+ # Add more mappings here.
15
+ # Here is an example that pulls values out of the Regexp:
16
+ #
17
+ # when /^the (notice|error|info) flash$/
18
+ # ".flash.#{$1}"
19
+
20
+ # You can also return an array to use a different selector
21
+ # type, like:
22
+ #
23
+ # when /the header/
24
+ # [:xpath, "//header"]
25
+
26
+ # This allows you to provide a quoted selector as the scope
27
+ # for "within" steps as was previously the default for the
28
+ # web steps:
29
+ when /^"(.+)"$/
30
+ $1
31
+
32
+ else
33
+ raise "Can't find mapping from \"#{locator}\" to a selector.\n" +
34
+ "Now, go and add a mapping in #{__FILE__}"
35
+ end
36
+ end
37
+ end
38
+
39
+ World(HtmlSelectorsHelpers)
@@ -0,0 +1,67 @@
1
+ module Socialite
2
+ module ApiWrappers
3
+ module Facebook
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ require 'koala'
8
+ end
9
+
10
+ module ClassMethods
11
+ def lookup(unique_id, options={})
12
+ options.reverse_merge!({:access_token => nil})
13
+ api_connection(options[:access_token]).get_object(unique_id)
14
+ end
15
+
16
+ def api_connection(access_token=nil)
17
+ Koala::Facebook::API.new(access_token)
18
+ end
19
+ end
20
+
21
+ module InstanceMethods
22
+ def account_url
23
+ "http://facebook.com/#{login}"
24
+ end
25
+
26
+ def access_token
27
+ raise unless super
28
+ end
29
+
30
+ def api
31
+ api_connection
32
+ end
33
+
34
+ def checkins
35
+ api.get_connections(unique_id, 'checkins')
36
+ end
37
+
38
+ def friends
39
+ api.get_connections(unique_id, 'friends')
40
+ end
41
+
42
+ def login
43
+ raise unless super
44
+ end
45
+
46
+ def picture
47
+ api.get_picture(unique_id)
48
+ end
49
+
50
+ def info
51
+ api.get_object(unique_id)
52
+ end
53
+
54
+ def unique_id
55
+ raise unless super
56
+ end
57
+
58
+ protected
59
+
60
+ def api_connection
61
+ @api_connection ||= self.class.api_connection(access_token)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+
@@ -0,0 +1,19 @@
1
+ module Socialite
2
+ module ApiWrappers
3
+ module Twitter
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ require 'twitter'
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ end
13
+
14
+ module InstanceMethods
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,96 @@
1
+ module Socialite
2
+ module BaseIdentity
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ belongs_to :user
7
+ serialize :auth_hash
8
+
9
+ # Ensure that before validation happens that the provider
10
+ # database column matches what is inside of the auth_hash
11
+ # dataset.
12
+ before_validation do |identity|
13
+ if identity.auth_hash.present?
14
+ identity.provider = identity.auth_hash.delete('provider') if identity.provider.blank?
15
+ identity.unique_id = identity.auth_hash.delete('uid') if identity.unique_id.blank?
16
+ end
17
+ end
18
+
19
+ # Ensure each user has only a single identity per provider type
20
+ validates :provider,
21
+ :uniqueness => {:scope => :user_id, :case_sensitive => false},
22
+ :presence => true
23
+
24
+ # Ensure an identity is never reused by another account
25
+ validates :unique_id,
26
+ :uniqueness => {:scope => :provider},
27
+ :presence => true
28
+
29
+ # Ensure an associated user exists before creating the identity
30
+ # validates_associated :user
31
+ end
32
+
33
+ module ClassMethods
34
+ # Finder method that finds the matching Provider and Unique ID or
35
+ # initializes a new, unsaved, object.
36
+ #
37
+ # @params [Hash] the OAuth authentication hash
38
+ # @returns [Identity]
39
+ def find_or_initialize_by_oauth(auth_hash)
40
+ identity = where(:provider => auth_hash['provider'], :unique_id => auth_hash['uid']).first || new
41
+ identity.auth_hash = auth_hash
42
+ identity
43
+ end
44
+ end
45
+
46
+ module InstanceMethods
47
+ # Method that maps uid to unique_id which is what we store it as.
48
+ #
49
+ # @returns [String]
50
+ # def uid=(new_uid)
51
+ # self.unique_id = new_uid
52
+ # end
53
+
54
+ # Convenience method for accessing the OAuth access token
55
+ #
56
+ # @returns [String] OAuth access token
57
+ # (see #credentials)
58
+ def access_token
59
+ credentials['token']
60
+ end
61
+
62
+ # Convenience method for accessing the OAuth access token secret
63
+ #
64
+ # @returns [String] OAuth access token secret
65
+ # (see #credentials)
66
+ def access_token_secret
67
+ credentials['secret']
68
+ end
69
+
70
+ # Convenience method for accessing the OAuth credentials sub-hash
71
+ #
72
+ # @returns [Hash] OAuth credentials sub-hash
73
+ # (see #access_token)
74
+ # (see #access_token_secret)
75
+ def credentials
76
+ auth_hash['credentials']
77
+ end
78
+
79
+ # Convenience method for accessing the nickname, which is typically
80
+ # set to the login name used for that provider.
81
+ #
82
+ # @returns [String] user nickname for the provider identity
83
+ def nickname
84
+ user_info['nickname']
85
+ end
86
+
87
+ # Convenience method for accessing the user information from the
88
+ # OAuth provider.
89
+ #
90
+ # @returns [Hash] the user information sub-hash
91
+ def user_info
92
+ auth_hash['user_info']
93
+ end
94
+ end
95
+ end
96
+ end