piplapis-ruby 5.0.5 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,294 +1,301 @@
1
- require_relative 'fields'
2
- require_relative 'utils'
3
-
4
-
5
- module Pipl
6
-
7
- class FieldsContainer
8
-
9
- CLASS_CONTAINER = {
10
- Name: 'names',
11
- Address: 'addresses',
12
- Phone: 'phones',
13
- Email: 'emails',
14
- Job: 'jobs',
15
- Education: 'educations',
16
- Image: 'images',
17
- Username: 'usernames',
18
- UserID: 'user_ids',
19
- Url: 'urls',
20
- Ethnicity: 'ethnicities',
21
- Language: 'languages',
22
- OriginCountry: 'origin_countries',
23
- Relationship: 'relationships',
24
- Tag: 'tags',
25
- }
26
-
27
- attr_reader :names, :addresses, :phones, :emails, :jobs, :educations, :images, :usernames, :user_ids, :urls
28
- attr_reader :relationships, :tags, :ethnicities, :languages, :origin_countries, :dob, :gender
29
-
30
- def initialize(params={})
31
- @names = []
32
- @addresses = []
33
- @phones = []
34
- @emails = []
35
- @jobs = []
36
- @educations = []
37
- @images = []
38
- @usernames = []
39
- @user_ids = []
40
- @urls = []
41
- @ethnicities = []
42
- @languages = []
43
- @origin_countries = []
44
- @relationships = []
45
- @tags = []
46
- @dob = nil
47
- @gender = nil
48
-
49
- add_fields params[:fields] if params.key? :fields
50
- end
51
-
52
- def self.from_hash(h)
53
- raise AbstractMethodInvoked.new
54
- end
55
-
56
- def self.fields_from_hash(h)
57
- fields = self::CLASS_CONTAINER.map do |cls_name, container|
58
- cls = Pipl.const_get cls_name
59
- h[container.to_sym].map { |x| cls.from_hash(x) } if h.key? container.to_sym
60
- end
61
- .flatten.compact
62
- fields << DOB.from_hash(h[:dob]) if h.key? :dob
63
- fields << Gender.from_hash(h[:gender]) if h.key? :gender
64
- fields
65
- end
66
-
67
- def fields_to_hash
68
- h = {}
69
- h[:dob] = @dob.to_hash if @dob
70
- h[:gender] = @gender.to_hash if @gender
71
- self.class::CLASS_CONTAINER.values.each do |container|
72
- fields = instance_variable_get("@#{container}")
73
- h[container.to_sym] = fields.map { |field| field.to_hash }.compact unless fields.empty?
74
- end
75
- h.reject { |_, value| value.nil? or (value.kind_of?(Array) and value.empty?) }
76
- end
77
-
78
- def add_fields(fields)
79
- fields.each { |f| add_field f }
80
- end
81
-
82
- def add_field(field)
83
- cls_sym = field.class.name.split('::').last.to_sym
84
- container = self.class::CLASS_CONTAINER[cls_sym]
85
- if container
86
- instance_variable_get("@#{container}") << field
87
- elsif cls_sym == :DOB
88
- @dob = field
89
- elsif cls_sym == :Gender
90
- @gender = field
91
- else
92
- raise ArgumentError.new("Object of type #{field.class} is an invalid field")
93
- end
94
- end
95
-
96
- def all_fields
97
- fields = self.class::CLASS_CONTAINER.values.map { |container| instance_variable_get("@#{container}") }
98
- .flatten.compact
99
- fields << @dob if @dob
100
- fields << @gender if @gender
101
- fields
102
- end
103
-
104
- def job
105
- @jobs.first unless @jobs.empty?
106
- end
107
-
108
- def address
109
- @addresses.first unless @addresses.empty?
110
- end
111
-
112
- def education
113
- @educations.first unless @educations.empty?
114
- end
115
-
116
- def language
117
- @languages.first unless @languages.empty?
118
- end
119
-
120
- def ethnicity
121
- @ethnicities.first unless @ethnicities.empty?
122
- end
123
-
124
- def origin_country
125
- @origin_countries.first unless @origin_countries.empty?
126
- end
127
-
128
- def phone
129
- @phones.first unless @phones.empty?
130
- end
131
-
132
- def email
133
- @emails.first unless @emails.empty?
134
- end
135
-
136
- def name
137
- @names.first unless @names.empty?
138
- end
139
-
140
- def image
141
- @images.first unless @images.empty?
142
- end
143
-
144
- def url
145
- @urls.first unless @urls.empty?
146
- end
147
-
148
- def username
149
- @usernames.first unless @usernames.empty?
150
- end
151
-
152
- def user_id
153
- @user_ids.first unless @user_ids.empty?
154
- end
155
-
156
- def relationship
157
- @relationships.first unless @relationships.empty?
158
- end
159
-
160
- end
161
-
162
-
163
- class Relationship < FieldsContainer
164
-
165
- CLASS_CONTAINER = FieldsContainer::CLASS_CONTAINER.clone
166
- CLASS_CONTAINER.delete :Relationship
167
-
168
- # @!attribute valid_since
169
- # @see Field
170
- # @!attribute inferred
171
- # @see Field
172
- # @!attribute type
173
- # @return [String] Type of association of this relationship to a person.
174
- # Possible values are:
175
- # friend
176
- # family
177
- # work
178
- # other
179
- # @!attribute subtype
180
- # @return [String] Subtype of association of this relationship to a person. Free text.
181
-
182
- attr_accessor :valid_since, :inferred, :type, :subtype
183
-
184
- def initialize(params={})
185
- super params
186
- @valid_since = params[:valid_since]
187
- @inferred = params[:inferred]
188
- @type = params[:type]
189
- @subtype = params[:subtype]
190
- end
191
-
192
- def self.from_hash(h)
193
- params = Pipl::Field.base_params_from_hash h
194
- params[:subtype] = h[:@subtype]
195
- params[:fields] = self.fields_from_hash(h)
196
- self.new(params)
197
- end
198
-
199
- def to_hash
200
- fields_to_hash
201
- end
202
-
203
- def to_s
204
- @names.first.to_s unless @names.empty?
205
- end
206
-
207
- end
208
-
209
-
210
- class Source < FieldsContainer
211
-
212
- attr_reader :match, :name, :category, :origin_url, :sponsored, :domain, :source_id, :person_id, :premium, :valid_since
213
-
214
- def initialize(params={})
215
- super params
216
- @name = params[:name]
217
- @category = params[:category]
218
- @origin_url = params[:origin_url]
219
- @domain = params[:domain]
220
- @source_id = params[:source_id]
221
- @person_id = params[:person_id]
222
- @sponsored = params[:sponsored]
223
- @premium = params[:premium]
224
- @match = params[:match]
225
- @valid_since = params[:valid_since]
226
- end
227
-
228
- def self.from_hash(h)
229
- params = {
230
- name: h[:@name],
231
- category: h[:@category],
232
- origin_url: h[:@origin_url],
233
- domain: h[:@domain],
234
- source_id: h[:@id],
235
- person_id: h[:@person_id],
236
- match: h[:@match],
237
- sponsored: h[:@sponsored],
238
- premium: h[:@premium],
239
- }
240
- params[:valid_since] = Pipl::Utils.str_to_date(h[:@valid_since]) if h.key? :@valid_since
241
- params[:fields] = self.fields_from_hash(h)
242
- self.new(params)
243
- end
244
-
245
- end
246
-
247
- class Person < FieldsContainer
248
-
249
- attr_reader :id, :match, :search_pointer, :inferred
250
-
251
- def initialize(params={})
252
- super params
253
- @id = params[:id]
254
- @match = params[:match]
255
- @search_pointer = params[:search_pointer]
256
- @inferred = params[:inferred] || false
257
- end
258
-
259
- def self.from_hash(h)
260
- params = {
261
- id: h[:@id],
262
- match: h[:@match],
263
- search_pointer: h[:@search_pointer],
264
- inferred: h[:@inferred],
265
- }
266
- params[:fields] = fields_from_hash(h)
267
- self.new(params)
268
- end
269
-
270
- def to_hash
271
- h = {}
272
- h[:search_pointer] = @search_pointer if @search_pointer and not @search_pointer.empty?
273
- h.update(fields_to_hash)
274
- h
275
- end
276
-
277
- def is_searchable?
278
- not @search_pointer.nil? or
279
- @names.any? { |f| f.is_searchable? } or
280
- @emails.any? { |f| f.is_searchable? } or
281
- @phones.any? { |f| f.is_searchable? } or
282
- @usernames.any? { |f| f.is_searchable? } or
283
- @user_ids.any? { |f| f.is_searchable? } or
284
- @urls.any? { |f| f.is_searchable? } or
285
- @addresses.any? { |f| f.is_sole_searchable? }
286
- end
287
-
288
- def unsearchable_fields
289
- all_fields.reject { |f| f.is_searchable? }
290
- end
291
-
292
- end
293
-
294
- end
1
+ require_relative 'fields'
2
+ require_relative 'utils'
3
+
4
+
5
+ module Pipl
6
+
7
+ class FieldsContainer
8
+
9
+ CLASS_CONTAINER = {
10
+ Name: 'names',
11
+ Address: 'addresses',
12
+ Phone: 'phones',
13
+ Email: 'emails',
14
+ Job: 'jobs',
15
+ Education: 'educations',
16
+ Image: 'images',
17
+ Username: 'usernames',
18
+ UserID: 'user_ids',
19
+ Url: 'urls',
20
+ Ethnicity: 'ethnicities',
21
+ Language: 'languages',
22
+ OriginCountry: 'origin_countries',
23
+ Relationship: 'relationships',
24
+ Tag: 'tags',
25
+ Vehicle: 'vehicles',
26
+ }
27
+
28
+ attr_reader :names, :addresses, :phones, :emails, :jobs, :educations, :images, :usernames, :user_ids, :urls
29
+ attr_reader :relationships, :tags, :ethnicities, :languages, :origin_countries, :dob, :gender, :vehicles
30
+
31
+ def initialize(params={})
32
+ @names = []
33
+ @addresses = []
34
+ @phones = []
35
+ @emails = []
36
+ @jobs = []
37
+ @educations = []
38
+ @images = []
39
+ @usernames = []
40
+ @user_ids = []
41
+ @urls = []
42
+ @ethnicities = []
43
+ @languages = []
44
+ @origin_countries = []
45
+ @relationships = []
46
+ @tags = []
47
+ @dob = nil
48
+ @gender = nil
49
+ @vehicles = []
50
+
51
+ add_fields params[:fields] if params.key? :fields
52
+ end
53
+
54
+ def self.from_hash(h)
55
+ raise AbstractMethodInvoked.new
56
+ end
57
+
58
+ def self.fields_from_hash(h)
59
+ fields = self::CLASS_CONTAINER.map do |cls_name, container|
60
+ cls = Pipl.const_get cls_name
61
+ h[container.to_sym].map { |x| cls.from_hash(x) } if h.key? container.to_sym
62
+ end
63
+ .flatten.compact
64
+ fields << DOB.from_hash(h[:dob]) if h.key? :dob
65
+ fields << Gender.from_hash(h[:gender]) if h.key? :gender
66
+ fields
67
+ end
68
+
69
+ def fields_to_hash
70
+ h = {}
71
+ h[:dob] = @dob.to_hash if @dob
72
+ h[:gender] = @gender.to_hash if @gender
73
+ self.class::CLASS_CONTAINER.values.each do |container|
74
+ fields = instance_variable_get("@#{container}")
75
+ h[container.to_sym] = fields.map { |field| field.to_hash }.compact unless fields.empty?
76
+ end
77
+ h.reject { |_, value| value.nil? || (value.kind_of?(Array) && value.empty?) }
78
+ end
79
+
80
+ def add_fields(fields)
81
+ fields.each { |f| add_field f }
82
+ end
83
+
84
+ def add_field(field)
85
+ cls_sym = field.class.name.split('::').last.to_sym
86
+ container = self.class::CLASS_CONTAINER[cls_sym]
87
+ if container
88
+ instance_variable_get("@#{container}") << field
89
+ elsif cls_sym == :DOB
90
+ @dob = field
91
+ elsif cls_sym == :Gender
92
+ @gender = field
93
+ else
94
+ raise ArgumentError.new("Object of type #{field.class} is an invalid field")
95
+ end
96
+ end
97
+
98
+ def all_fields
99
+ fields = self.class::CLASS_CONTAINER.values.map { |container| instance_variable_get("@#{container}") }
100
+ .flatten.compact
101
+ fields << @dob if @dob
102
+ fields << @gender if @gender
103
+ fields
104
+ end
105
+
106
+ def job
107
+ @jobs.first unless @jobs.empty?
108
+ end
109
+
110
+ def address
111
+ @addresses.first unless @addresses.empty?
112
+ end
113
+
114
+ def education
115
+ @educations.first unless @educations.empty?
116
+ end
117
+
118
+ def language
119
+ @languages.first unless @languages.empty?
120
+ end
121
+
122
+ def ethnicity
123
+ @ethnicities.first unless @ethnicities.empty?
124
+ end
125
+
126
+ def origin_country
127
+ @origin_countries.first unless @origin_countries.empty?
128
+ end
129
+
130
+ def phone
131
+ @phones.first unless @phones.empty?
132
+ end
133
+
134
+ def email
135
+ @emails.first unless @emails.empty?
136
+ end
137
+
138
+ def name
139
+ @names.first unless @names.empty?
140
+ end
141
+
142
+ def image
143
+ @images.first unless @images.empty?
144
+ end
145
+
146
+ def url
147
+ @urls.first unless @urls.empty?
148
+ end
149
+
150
+ def username
151
+ @usernames.first unless @usernames.empty?
152
+ end
153
+
154
+ def user_id
155
+ @user_ids.first unless @user_ids.empty?
156
+ end
157
+
158
+ def relationship
159
+ @relationships.first unless @relationships.empty?
160
+ end
161
+
162
+ def vehicle
163
+ @vehicles.first unless @vehicles.empty?
164
+ end
165
+
166
+ end
167
+
168
+
169
+ class Relationship < FieldsContainer
170
+
171
+ CLASS_CONTAINER = FieldsContainer::CLASS_CONTAINER.clone
172
+ CLASS_CONTAINER.delete :Relationship
173
+
174
+ # @!attribute valid_since
175
+ # @see Field
176
+ # @!attribute inferred
177
+ # @see Field
178
+ # @!attribute type
179
+ # @return [String] Type of association of this relationship to a person.
180
+ # Possible values are:
181
+ # friend
182
+ # family
183
+ # work
184
+ # other
185
+ # @!attribute subtype
186
+ # @return [String] Subtype of association of this relationship to a person. Free text.
187
+
188
+ attr_accessor :valid_since, :inferred, :type, :subtype
189
+
190
+ def initialize(params={})
191
+ super params
192
+ @valid_since = params[:valid_since]
193
+ @inferred = params[:inferred]
194
+ @type = params[:type]
195
+ @subtype = params[:subtype]
196
+ end
197
+
198
+ def self.from_hash(h)
199
+ params = Pipl::Field.base_params_from_hash h
200
+ params[:subtype] = h[:@subtype]
201
+ params[:fields] = self.fields_from_hash(h)
202
+ self.new(params)
203
+ end
204
+
205
+ def to_hash
206
+ fields_to_hash
207
+ end
208
+
209
+ def to_s
210
+ @names.first.to_s unless @names.empty?
211
+ end
212
+
213
+ end
214
+
215
+
216
+ class Source < FieldsContainer
217
+
218
+ attr_reader :match, :name, :category, :origin_url, :sponsored, :domain, :source_id, :person_id, :premium, :valid_since
219
+
220
+ def initialize(params={})
221
+ super params
222
+ @name = params[:name]
223
+ @category = params[:category]
224
+ @origin_url = params[:origin_url]
225
+ @domain = params[:domain]
226
+ @source_id = params[:source_id]
227
+ @person_id = params[:person_id]
228
+ @sponsored = params[:sponsored]
229
+ @premium = params[:premium]
230
+ @match = params[:match]
231
+ @valid_since = params[:valid_since]
232
+ end
233
+
234
+ def self.from_hash(h)
235
+ params = {
236
+ name: h[:@name],
237
+ category: h[:@category],
238
+ origin_url: h[:@origin_url],
239
+ domain: h[:@domain],
240
+ source_id: h[:@id],
241
+ person_id: h[:@person_id],
242
+ match: h[:@match],
243
+ sponsored: h[:@sponsored],
244
+ premium: h[:@premium],
245
+ }
246
+ params[:valid_since] = Pipl::Utils.str_to_date(h[:@valid_since]) if h.key? :@valid_since
247
+ params[:fields] = self.fields_from_hash(h)
248
+ self.new(params)
249
+ end
250
+
251
+ end
252
+
253
+ class Person < FieldsContainer
254
+
255
+ attr_reader :id, :match, :search_pointer, :inferred
256
+
257
+ def initialize(params={})
258
+ super params
259
+ @id = params[:id]
260
+ @match = params[:match]
261
+ @search_pointer = params[:search_pointer]
262
+ @inferred = params[:inferred] || false
263
+ end
264
+
265
+ def self.from_hash(h)
266
+ params = {
267
+ id: h[:@id],
268
+ match: h[:@match],
269
+ search_pointer: h[:@search_pointer],
270
+ inferred: h[:@inferred],
271
+ }
272
+ params[:fields] = fields_from_hash(h)
273
+ self.new(params)
274
+ end
275
+
276
+ def to_hash
277
+ h = {}
278
+ h[:search_pointer] = @search_pointer if @search_pointer && ! @search_pointer.empty?
279
+ h.update(fields_to_hash)
280
+ h
281
+ end
282
+
283
+ def is_searchable?
284
+ !@search_pointer.nil? ||
285
+ @names.any? { |f| f.is_searchable? } ||
286
+ @emails.any? { |f| f.is_searchable? } ||
287
+ @phones.any? { |f| f.is_searchable? } ||
288
+ @usernames.any? { |f| f.is_searchable? } ||
289
+ @user_ids.any? { |f| f.is_searchable? } ||
290
+ @urls.any? { |f| f.is_searchable? } ||
291
+ @addresses.any? { |f| f.is_sole_searchable? } ||
292
+ @vehicles.any? { |f| f.is_searchable? }
293
+ end
294
+
295
+ def unsearchable_fields
296
+ all_fields.reject { |f| f.is_searchable? }
297
+ end
298
+
299
+ end
300
+
301
+ end