Class: SoundcloudAuth::GenericUser
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SoundcloudAuth::GenericUser
- Defined in:
- app/models/soundcloud_auth/generic_user.rb
Constant Summary collapse
- SOUNDCLOUD_ATTRIBUTES =
[ :plan, :avatar_url, :website_title, :permalink, :city, :uri, :country, :username, :discogs_name, :website, :full_name, :followers_count, :description, :permalink_url, :followings_count, :track_count, :myspace_name ]
Class Method Summary collapse
- .create_from_soundcloud_hash_and_token(user_info, access_token) ⇒ Object
- .from_remember_token(token) ⇒ Object
- .identify_or_create_from_access_token(token, secret = nil) ⇒ Object
- .new_from_soundcloud_hash(hash) ⇒ Object
- .table_name ⇒ Object
Instance Method Summary collapse
- #assign_soundcloud_attributes(hash) ⇒ Object
- #forget_me ⇒ Object
- #remember_me ⇒ Object
- #soundcloud ⇒ Object
- #token ⇒ Object
- #update_soundcloud_attributes(hash) ⇒ Object
- #utilize_default_validations ⇒ Object
Class Method Details
.create_from_soundcloud_hash_and_token(user_info, access_token) ⇒ Object
85 86 87 88 89 90 91 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 85 def self.create_from_soundcloud_hash_and_token(user_info, access_token) user = User.new_from_soundcloud_hash(user_info) user.access_token = access_token.token user.access_secret = access_token.secret user.save user end |
.from_remember_token(token) ⇒ Object
49 50 51 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 49 def self.from_remember_token(token) first(:conditions => ["remember_token = ? AND remember_token_expires_at > ?", token, Time.now]) end |
.identify_or_create_from_access_token(token, secret = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 64 def self.identify_or_create_from_access_token(token, secret=nil) raise ArgumentError, 'Must authenticate with an OAuth::AccessToken or the string access token and secret.' unless (token && secret) || token.is_a?(OAuth::AccessToken) token = OAuth::AccessToken.new(SoundcloudAuth.consumer, token, secret) unless token.is_a?(OAuth::AccessToken) response = token.get(SoundcloudAuth.path_prefix + '/me.json') user_info = JSON.parse(response.body) if user = User.find_by_soundcloud_id(user_info['id'].to_s) user.assign_soundcloud_attributes(user_info) user.access_token = token.token user.access_secret = token.secret user.save user else User.create_from_soundcloud_hash_and_token(user_info, token) end end |
.new_from_soundcloud_hash(hash) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 34 def self.new_from_soundcloud_hash(hash) raise ArgumentError, 'Invalid hash: must include id.' unless hash.key?('id') user = User.new user.soundcloud_id = hash['id'].to_s SOUNDCLOUD_ATTRIBUTES.each do |att| user.send("#{att}=", hash[att.to_s]) if user.respond_to?("#{att}=") end user end |
.table_name ⇒ Object
32 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 32 def self.table_name; 'users' end |
Instance Method Details
#assign_soundcloud_attributes(hash) ⇒ Object
53 54 55 56 57 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 53 def assign_soundcloud_attributes(hash) SOUNDCLOUD_ATTRIBUTES.each do |att| send("#{att}=", hash[att.to_s]) if respond_to?("#{att}=") end end |
#forget_me ⇒ Object
118 119 120 121 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 118 def forget_me self.remember_token = self.remember_token_expires_at = nil self.save end |
#remember_me ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 105 def remember_me return false unless respond_to?(:remember_token) self.remember_token = ActiveSupport::SecureRandom.hex(10) self.remember_token_expires_at = Time.now + SoundcloudAuth.remember_for.days save {:value => self.remember_token, :expires => self.remember_token_expires_at} end |
#soundcloud ⇒ Object
101 102 103 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 101 def soundcloud Soundcloud.register({:access_token => token}) end |
#token ⇒ Object
93 94 95 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 93 def token OAuth::AccessToken.new(SoundcloudAuth.consumer, access_token, access_secret) end |
#update_soundcloud_attributes(hash) ⇒ Object
59 60 61 62 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 59 def update_soundcloud_attributes(hash) assign_soundcloud_attributes(hash) save end |
#utilize_default_validations ⇒ Object
97 98 99 |
# File 'app/models/soundcloud_auth/generic_user.rb', line 97 def utilize_default_validations true end |