Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Beefed up the AuthHash, pulled name out to the root, specs for #to_hash.
- Loading branch information
Michael Bleigh
committed
Sep 22, 2011
1 parent
ba4bd3e
commit 5733f9a
Showing
2 changed files
with
90 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,48 @@ | ||
require 'hashie/mash' | ||
|
||
module OmniAuth | ||
# The AuthHash is a normalized schema returned by all OmniAuth | ||
# strategies. It maps as much user information as the provider | ||
# is able to provide into the InfoHash (stored as the `'info'` | ||
# key). | ||
class AuthHash < Hashie::Mash | ||
|
||
# Tells you if this is considered to be a valid | ||
# OmniAuth AuthHash. The requirements for that | ||
# are that it has a provider name, a uid, and a | ||
# valid user_info hash. See UserInfo#valid? for | ||
# valid info hash. See InfoHash#valid? for | ||
# more details there. | ||
def valid? | ||
uid? && provider? && (!user_info? || user_info.valid?) | ||
uid? && provider? && name? | ||
end | ||
|
||
def name | ||
return self[:name] if self[:name] | ||
return nil unless info? | ||
return "#{info.first_name} #{info.last_name}".strip if info.first_name? || info.last_name? | ||
return info.nickname if info.nickname? | ||
return info.email if info.email? | ||
nil | ||
end | ||
|
||
def name?; !!name end | ||
|
||
def regular_writer(key, value) | ||
if key.to_s == 'user_info' && !value.is_a?(UserInfo) | ||
value = UserInfo.new(value) | ||
if key.to_s == 'info' && !value.is_a?(InfoHash) | ||
value = InfoHash.new(value) | ||
end | ||
super | ||
end | ||
|
||
class UserInfo < Hashie::Mash | ||
def to_hash | ||
hash = super | ||
hash['name'] ||= name | ||
hash | ||
end | ||
|
||
class InfoHash < Hashie::Mash | ||
def valid? | ||
name? | ||
end | ||
|
||
def name | ||
return self[:name] if name? | ||
return "#{first_name} #{last_name}".strip if first_name? || last_name? | ||
return nickname if nickname? | ||
return email if email? | ||
nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters