Class: Trusty::Omniauth::ProviderMapper
- Inherits:
-
Object
- Object
- Trusty::Omniauth::ProviderMapper
- Includes:
- MappingHelpers
- Defined in:
- lib/trusty/omniauth/provider_mapper.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#provider_attributes ⇒ Object
readonly
Returns the value of attribute provider_attributes.
-
#provider_identity ⇒ Object
readonly
Returns the value of attribute provider_identity.
-
#provider_name ⇒ Object
readonly
Returns the value of attribute provider_name.
-
#provider_user ⇒ Object
readonly
Returns the value of attribute provider_user.
Instance Method Summary collapse
-
#attributes(*filter_attribute_names) ⇒ Object
General ######.
-
#build_identity(attributes = {}) ⇒ Object
IDENTITY.
- #build_identity_for_user(user) ⇒ Object
-
#build_user(attributes = {}) ⇒ Object
USER.
-
#find_identities_for_user(user) ⇒ Object
Query existing.
- #identities_exist? ⇒ Boolean
-
#initialize(provider_attributes, options = {}) ⇒ ProviderMapper
constructor
provider_attributes = OmniAuth data options = - :user_model = User model - :user_attributes = Hash of attributes to merge into user_attributes - :user_attributes_names = Array of attribute names to copy from Omniauth data (default: User.column_names) - :identity_model = Identity model - :identity_attributes = Hash of attributes to merge into identity_attributes - :identity_attribute_names = Array of attribute names to copy from Omniauth data (default: Identity.column_names) - :identity_required_criteria = Hash of criteria to use to find identities, and also to merge into attributes - :unique_identifiers = Array of column names that identify a model uniquely with omniauth data - :skip_raw_info (default: false) = Boolean whether to exclude OmniAuth “extra” data in identity_attributes - :skip_nils (default: true) = Boolean whether to remove attributes with nil values.
-
#matched_identities ⇒ Object
Matched identities based on omniauth unique identifiers (provider, uid).
-
#matched_users ⇒ Object
Matched users based on omniauth unique identifiers (email).
- #multiple_identities? ⇒ Boolean
- #multiple_users? ⇒ Boolean
- #single_identity ⇒ Object
- #single_identity? ⇒ Boolean
- #single_user ⇒ Object
- #single_user? ⇒ Boolean
- #update_identity!(identity) ⇒ Object
- #users_exist? ⇒ Boolean
Methods included from MappingHelpers
Constructor Details
#initialize(provider_attributes, options = {}) ⇒ ProviderMapper
provider_attributes = OmniAuth data options =
-
:user_model = User model
-
:user_attributes = Hash of attributes to merge into user_attributes
-
:user_attributes_names = Array of attribute names to copy from Omniauth data (default: User.column_names)
-
:identity_model = Identity model
-
:identity_attributes = Hash of attributes to merge into identity_attributes
-
:identity_attribute_names = Array of attribute names to copy from Omniauth data (default: Identity.column_names)
-
:identity_required_criteria = Hash of criteria to use to find identities, and also to merge into attributes
-
:unique_identifiers = Array of column names that identify a model uniquely with omniauth data
-
:skip_raw_info (default: false) = Boolean whether to exclude OmniAuth “extra” data in identity_attributes
-
:skip_nils (default: true) = Boolean whether to remove attributes with nil values
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 24 def initialize(provider_attributes, = {}) @provider_attributes = provider_attributes.with_indifferent_access @provider_name = provider_attributes['provider'] = { :user_attributes => {}, :identity_attributes => {}, :skip_raw_info => false, :skip_nils => true }.merge() @provider_identity = ModelMapper.new(self, :model => [:identity_model] || Identity, :attributes => [:identity_attributes], :attribute_names => [:identity_attribute_names], :unique_identifiers => [:unique_identifiers] || [:provider, :uid], :required_criteria => [:identity_required_criteria] ) @provider_user = ModelMapper.new(self, :model => [:user_model] || User, :attributes => [:user_attributes], :attribute_names => [:user_attribute_names], :unique_identifiers => [:unique_identifiers] || [:email] ) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 9 def end |
#provider_attributes ⇒ Object (readonly)
Returns the value of attribute provider_attributes.
9 10 11 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 9 def provider_attributes @provider_attributes end |
#provider_identity ⇒ Object (readonly)
Returns the value of attribute provider_identity.
10 11 12 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 10 def provider_identity @provider_identity end |
#provider_name ⇒ Object (readonly)
Returns the value of attribute provider_name.
9 10 11 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 9 def provider_name @provider_name end |
#provider_user ⇒ Object (readonly)
Returns the value of attribute provider_user.
10 11 12 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 10 def provider_user @provider_user end |
Instance Method Details
#attributes(*filter_attribute_names) ⇒ Object
General ######
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 124 def attributes(*filter_attribute_names) puts "provider_attributes: #{provider_attributes.inspect}" unless @attributes info = provider_attributes.fetch('info', {}) credentials = provider_attributes['credentials'] name = clean(info['name']) { [info['first_name'], info['last_name']].join(" ").strip } first_name = clean(info['first_name']) { name.split(/\s+/, 2).first } last_name = clean(info['last_name']) { name.split(/\s+/, 2).last } @attributes = { :provider => provider_name, :uid => clean(provider_attributes['uid']), :name => name, :email => clean(info['email'], :downcase, :strip), :verified => provider_attributes['extra']['raw_info']['verified_email'] == true, :username => clean(info['nickname']), :first_name => first_name, :middle_name => clean(info['middle_name']), :last_name => last_name, :phone => clean(info['phone']), :image_url => info['image'], :profile_url => info.fetch('urls', {})['public_profile'], :token_type => clean(credentials['token_type']), :token => clean(credentials['token']), :secret => clean(credentials['secret']), :refresh_token => clean(credentials['refresh_token']), :expires => credentials['expires'] == true, :expires_at => (Time.at(credentials['expires_at']) rescue nil), :raw_info => provider_attributes['extra'].except('access_token').as_json, # extra :location => info['location'], :time_zone => info['time_zone'] || Time.zone.name, :locale => info['locale'] || I18n.locale }.with_indifferent_access @attributes.reject!{|_,value| value.nil?} if [:skip_nils] @attributes.delete(:raw_info) if [:skip_raw_info] end if filter_attribute_names.any? @attributes.slice(*filter_attribute_names) else @attributes.dup end end |
#build_identity(attributes = {}) ⇒ Object
IDENTITY
105 106 107 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 105 def build_identity(attributes = {}) @provider_identity.build_record(attributes) end |
#build_identity_for_user(user) ⇒ Object
109 110 111 112 113 114 115 116 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 109 def build_identity_for_user(user) # build_identity.tap do |identity| # # this assumes there is an inverse relationship automatically created # # such as user.identities would now contain this identity for the user # identity.user = user # end build_identity(user: user) end |
#build_user(attributes = {}) ⇒ Object
USER
99 100 101 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 99 def build_user(attributes = {}) @provider_user.build_record(attributes) end |
#find_identities_for_user(user) ⇒ Object
Query existing
51 52 53 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 51 def find_identities_for_user(user) @provider_identity.find_records(user_id: user.id) end |
#identities_exist? ⇒ Boolean
60 61 62 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 60 def identities_exist? matched_identities.any? end |
#matched_identities ⇒ Object
Matched identities based on omniauth unique identifiers (provider, uid)
56 57 58 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 56 def matched_identities @matched_identities ||= @provider_identity.find_records end |
#matched_users ⇒ Object
Matched users based on omniauth unique identifiers (email)
73 74 75 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 73 def matched_users @matched_users ||= @provider_user.find_records end |
#multiple_identities? ⇒ Boolean
68 69 70 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 68 def multiple_identities? matched_identities.size > 1 end |
#multiple_users? ⇒ Boolean
85 86 87 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 85 def multiple_users? matched_users.size > 1 end |
#single_identity ⇒ Object
93 94 95 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 93 def single_identity @single_identity ||= matched_identities.first if single_identity? end |
#single_identity? ⇒ Boolean
64 65 66 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 64 def single_identity? matched_identities.size == 1 end |
#single_user ⇒ Object
89 90 91 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 89 def single_user @single_user ||= matched_users.first if single_user? end |
#single_user? ⇒ Boolean
81 82 83 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 81 def single_user? matched_users.size == 1 end |
#update_identity!(identity) ⇒ Object
118 119 120 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 118 def update_identity!(identity) @provider_identity.update_record!(identity) end |
#users_exist? ⇒ Boolean
77 78 79 |
# File 'lib/trusty/omniauth/provider_mapper.rb', line 77 def users_exist? matched_users.any? end |