trusty 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 71584e6ca9a7eb11928889a8d9602bfd90f708d2
4
- data.tar.gz: 242cee98e5ab2ada63c72ed10371c9e363dc47e3
3
+ metadata.gz: 59c014a94c8849d629bc952a1b2ff92501fba0ee
4
+ data.tar.gz: 780f15a4e4cb6edc59d50f7da54effb81f994e72
5
5
  SHA512:
6
- metadata.gz: 5f5f25ff3503cda08da2beb3f7e3b2a8d662b18d4f6e269c2474e84437088147a7decf9814910fd5a39b0941733d8c995677508882ac36b9d82dae6d39f72aa5
7
- data.tar.gz: a0f3ef3a5ad8bc885c260d6f4f706c3a70f0c587ad95ca778b1ebb6b1e59ff69ac25437e57a3c49b068a7e58dd288b45d271588dde41ad017abede1e8d69e16b
6
+ metadata.gz: 897b8770ae13243c1be08704619fb6ba8f90e56827d3823ff153140f8b011ef1e3ca36f99bc1b8ba7ed01ee76a8a8ba7dba89ad2e64736164b25aa97d7a03538
7
+ data.tar.gz: 591cf24333b58f913babbd3473d9a5fa3de3b1b3311dc5d84c7ea8468a0d7c01480ff918b6490fc84e8643992b19fd68bf39039947960c1d32d1abf69b890325
@@ -6,7 +6,7 @@ module Trusty
6
6
  include MappingHelpers
7
7
 
8
8
  attr_reader :unique_identifiers, :required_criteria
9
-
9
+
10
10
  def initialize(provider, options = {})
11
11
  @provider = provider
12
12
  @options = options
@@ -14,32 +14,37 @@ module Trusty
14
14
  @unique_identifiers = @options.fetch(:unique_identifiers, []).map(&:to_s)
15
15
  @required_criteria = stringify_keys @options.fetch(:required_criteria, {})
16
16
  end
17
-
17
+
18
18
  def model
19
19
  @options[:model]
20
20
  end
21
-
21
+
22
22
  def attribute_names
23
23
  # Remove required_criteria so that existing attributes are skipped
24
24
  @attribute_names ||= (@options[:attribute_names] || column_names).map(&:to_s) - required_criteria.keys
25
25
  end
26
-
26
+
27
27
  def attributes(*filter_attribute_names)
28
28
  @attributes ||= @provider.attributes(*attribute_names).merge(stringify_keys @options[:attributes]).merge(required_criteria)
29
-
29
+
30
30
  if filter_attribute_names.any?
31
31
  @attributes.slice(*filter_attribute_names)
32
32
  else
33
33
  @attributes.dup
34
34
  end
35
35
  end
36
-
36
+
37
37
  def build_record(additional_attributes = {})
38
38
  model.new(attributes.merge(required_criteria).merge(additional_attributes), without_protection: true)
39
39
  end
40
-
40
+
41
41
  def find_records(additional_criteria = {})
42
- conditions = model.where( attributes(*unique_identifiers) )
42
+ unique_identifier_attributes = attributes(*unique_identifiers)
43
+ empty_attributes = unique_identifiers - unique_identifier_attributes.keys
44
+
45
+ raise "Missing unique attribute: #{empty_attributes.join(', ')}" if empty_attributes.any?
46
+
47
+ conditions = model.where( unique_identifier_attributes )
43
48
  conditions = conditions.where(additional_criteria) unless additional_criteria.empty?
44
49
  conditions.where(required_criteria)
45
50
  end
@@ -51,7 +56,7 @@ module Trusty
51
56
  record.update_attributes!(attributes, without_protection: true)
52
57
  end
53
58
  end
54
-
59
+
55
60
  def column_names
56
61
  @column_names ||= if model.respond_to? :column_names
57
62
  model.column_names.map(&:to_sym)
@@ -68,4 +73,4 @@ module Trusty
68
73
  end
69
74
  end
70
75
  end
71
- end
76
+ end
@@ -5,20 +5,22 @@ module Trusty
5
5
  module Omniauth
6
6
  class ProviderMapper
7
7
  include MappingHelpers
8
-
8
+
9
9
  attr_reader :provider_name, :provider_attributes, :options
10
10
  attr_reader :provider_identity, :provider_user
11
-
11
+
12
12
  # provider_attributes = OmniAuth data
13
- # options =
13
+ # options =
14
14
  # - :user_model = User model
15
15
  # - :user_attributes = Hash of attributes to merge into user_attributes
16
16
  # - :user_attributes_names = Array of attribute names to copy from Omniauth data (default: User.column_names)
17
+ # - :user_required_criteria = Hash of criteria to use to find users, and also to merge into attributes
18
+ # - :user_identifiers = Array of column names that identify a model uniquely with omniauth data
17
19
  # - :identity_model = Identity model
18
20
  # - :identity_attributes = Hash of attributes to merge into identity_attributes
19
21
  # - :identity_attribute_names = Array of attribute names to copy from Omniauth data (default: Identity.column_names)
20
22
  # - :identity_required_criteria = Hash of criteria to use to find identities, and also to merge into attributes
21
- # - :unique_identifiers = Array of column names that identify a model uniquely with omniauth data
23
+ # - :identity_identifiers = Array of column names that identify a model uniquely with omniauth data
22
24
  # - :skip_raw_info (default: false) = Boolean whether to exclude OmniAuth "extra" data in identity_attributes[:raw_info]
23
25
  # - :skip_nils (default: true) = Boolean whether to remove attributes with nil values
24
26
  def initialize(provider_attributes, options = {})
@@ -30,28 +32,29 @@ module Trusty
30
32
  :skip_raw_info => false,
31
33
  :skip_nils => true
32
34
  }.merge(options)
33
-
35
+
34
36
  @provider_identity = ModelMapper.new(self,
35
- :model => @options[:identity_model] || Identity,
37
+ :model => @options[:identity_model] || ::Identity,
36
38
  :attributes => @options[:identity_attributes],
37
39
  :attribute_names => @options[:identity_attribute_names],
38
- :unique_identifiers => @options[:unique_identifiers] || [:provider, :uid],
40
+ :unique_identifiers => @options[:identity_identifiers] || [:provider, :uid],
39
41
  :required_criteria => @options[:identity_required_criteria]
40
42
  )
41
43
  @provider_user = ModelMapper.new(self,
42
- :model => @options[:user_model] || User,
44
+ :model => @options[:user_model] || ::User,
43
45
  :attributes => @options[:user_attributes],
44
46
  :attribute_names => @options[:user_attribute_names],
45
- :unique_identifiers => @options[:unique_identifiers] || [:email]
47
+ :unique_identifiers => @options[:user_identifiers] || [:email],
48
+ :required_criteria => @options[:user_required_criteria]
46
49
  )
47
50
  end
48
-
51
+
49
52
  # Query existing
50
53
 
51
54
  def find_identities_for_user(user)
52
55
  @provider_identity.find_records(user_id: user.id)
53
56
  end
54
-
57
+
55
58
  # Matched identities based on omniauth unique identifiers (provider, uid)
56
59
  def matched_identities
57
60
  @matched_identities ||= @provider_identity.find_records
@@ -68,7 +71,7 @@ module Trusty
68
71
  def multiple_identities?
69
72
  matched_identities.size > 1
70
73
  end
71
-
74
+
72
75
  # Matched users based on omniauth unique identifiers (email)
73
76
  def matched_users
74
77
  @matched_users ||= @provider_user.find_records
@@ -85,27 +88,27 @@ module Trusty
85
88
  def multiple_users?
86
89
  matched_users.size > 1
87
90
  end
88
-
91
+
89
92
  def single_user
90
93
  @single_user ||= matched_users.first if single_user?
91
94
  end
92
-
95
+
93
96
  def single_identity
94
97
  @single_identity ||= matched_identities.first if single_identity?
95
98
  end
96
-
99
+
97
100
  # USER
98
-
101
+
99
102
  def build_user(attributes = {})
100
103
  @provider_user.build_record(attributes)
101
104
  end
102
-
105
+
103
106
  # IDENTITY
104
-
107
+
105
108
  def build_identity(attributes = {})
106
109
  @provider_identity.build_record(attributes)
107
110
  end
108
-
111
+
109
112
  def build_identity_for_user(user)
110
113
  # build_identity.tap do |identity|
111
114
  # # this assumes there is an inverse relationship automatically created
@@ -114,23 +117,22 @@ module Trusty
114
117
  # end
115
118
  build_identity(user: user)
116
119
  end
117
-
120
+
118
121
  def update_identity!(identity)
119
122
  @provider_identity.update_record!(identity)
120
123
  end
121
-
124
+
122
125
  ###### General ######
123
-
126
+
124
127
  def attributes(*filter_attribute_names)
125
- puts "provider_attributes: #{provider_attributes.inspect}"
126
128
  unless @attributes
127
129
  info = provider_attributes.fetch('info', {})
128
130
  credentials = provider_attributes['credentials']
129
-
131
+
130
132
  name = clean(info['name']) { [info['first_name'], info['last_name']].join(" ").strip }
131
133
  first_name = clean(info['first_name']) { name.split(/\s+/, 2).first }
132
134
  last_name = clean(info['last_name']) { name.split(/\s+/, 2).last }
133
-
135
+
134
136
  @attributes = {
135
137
  :provider => provider_name,
136
138
  :uid => clean(provider_attributes['uid']),
@@ -156,18 +158,18 @@ module Trusty
156
158
  :time_zone => info['time_zone'] || Time.zone.name,
157
159
  :locale => info['locale'] || I18n.locale
158
160
  }.with_indifferent_access
159
-
161
+
160
162
  @attributes.reject!{|_,value| value.nil?} if options[:skip_nils]
161
163
  @attributes.delete(:raw_info) if options[:skip_raw_info]
162
164
  end
163
-
165
+
164
166
  if filter_attribute_names.any?
165
167
  @attributes.slice(*filter_attribute_names)
166
168
  else
167
169
  @attributes.dup
168
170
  end
169
171
  end
170
-
172
+
171
173
  end
172
174
  end
173
- end
175
+ end
@@ -1,3 +1,3 @@
1
1
  module Trusty
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trusty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Van Horn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-26 00:00:00.000000000 Z
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -206,10 +206,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
206
206
  version: '0'
207
207
  requirements: []
208
208
  rubyforge_project:
209
- rubygems_version: 2.4.5
209
+ rubygems_version: 2.6.11
210
210
  signing_key:
211
211
  specification_version: 4
212
212
  summary: Trusty allows you to manage environment variables and other common configuration
213
213
  challenges.
214
214
  test_files: []
215
- has_rdoc: