piplapis-ruby 5.0.4 → 5.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a9ec065bf1afe0724361330927ccb35dc910cf36
4
- data.tar.gz: 756f3b62d694449389e39b7c1a148df89d531d69
3
+ metadata.gz: 91ab0c6412cf37cc084601eb3dc9fafb2f531b61
4
+ data.tar.gz: 9de85f2eec73a3370ad39af6d3237b40744d1716
5
5
  SHA512:
6
- metadata.gz: db49fce2bcbe5f2b9e6307c0a125c2f61f5c1f10fa4b59e1dd805600facfefdacd53258e61bff9f716c4ceedc169d411787a7d4d971e3c63a894b9c46ba0daab
7
- data.tar.gz: 178270c9e6675ca1c7f16472620d6342780261d0108e12ccbf2fe3e9e04aa7ce2b6bc42c46d0910d0acc2ef2d5a8804b724c362dac75f79cab9ac3db3ae6e92f
6
+ metadata.gz: f119adef4b2ee343cbcb6b08145b930659f5eaa48a8be30215a47d11d6ef5babcf768f5d3e0fcb8197764f5b0d0e99f3a0afd8b8e03e65e37f59ef3f43316abc
7
+ data.tar.gz: 8a2bdecd2d313df8f1014163a33ac6973a5c6eee2022dd37d8bada6d392131fc022104da3b58c74d1b6f6a48b22c98005e4fbfc217beee3b75f855fa60907762
data/LICENSE CHANGED
@@ -1,11 +1,11 @@
1
- Licensed under the Apache License, Version 2.0 (the "License");
2
- you may not use this file except in compliance with the License.
3
- You may obtain a copy of the License at
4
-
5
- http://www.apache.org/licenses/LICENSE-2.0
6
-
7
- Unless required by applicable law or agreed to in writing, software
8
- distributed under the License is distributed on an "AS IS" BASIS,
9
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- See the License for the specific language governing permissions and
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
11
  limitations under the License.
data/README.md CHANGED
@@ -1,25 +1,25 @@
1
- Pipl API Ruby Library
2
- ===========================
3
-
4
- This is the official Ruby client library for easily integrating Pipl's APIs into your application.
5
-
6
- * Full details about Pipl's API - [https://pipl.com/dev/](https://pipl.com/dev/)
7
- * This library is available in other languages - [https://pipl.com/dev/guide/code_libraries](https://pipl.com/dev/guide/code_libraries)
8
-
9
- Library Requirements
10
- --------------------
11
-
12
- * Ruby 1.9 and above.
13
-
14
- Getting Started & Code Snippets
15
- -------------------------------
16
-
17
- **Pipl's Search API**
18
- * Code snippets - [https://pipl.com/dev/guide/code_snippets](https://pipl.com/dev/guide/code_snippets)
19
- * Full reference - [https://pipl.com/dev/reference/](https://pipl.com/dev/reference/)
20
-
21
-
22
- Credits
23
- -------
24
-
25
- Thanks to [srochy](https://github.com/srochy) for writing the first version this library!
1
+ Pipl API Ruby Library
2
+ ===========================
3
+
4
+ This is the official Ruby client library for easily integrating Pipl's APIs into your application.
5
+
6
+ * Full details about Pipl's API - [https://pipl.com/dev/](https://pipl.com/dev/)
7
+ * This library is available in other languages - [https://pipl.com/dev/guide/code_libraries](https://pipl.com/dev/guide/code_libraries)
8
+
9
+ Library Requirements
10
+ --------------------
11
+
12
+ * Ruby 1.9 and above.
13
+
14
+ Getting Started & Code Snippets
15
+ -------------------------------
16
+
17
+ **Pipl's Search API**
18
+ * Code snippets - [https://pipl.com/dev/guide/code_snippets](https://pipl.com/dev/guide/code_snippets)
19
+ * Full reference - [https://pipl.com/dev/reference/](https://pipl.com/dev/reference/)
20
+
21
+
22
+ Credits
23
+ -------
24
+
25
+ Thanks to [srochy](https://github.com/srochy) for writing the first version this library!
@@ -1,35 +1,35 @@
1
- require_relative 'pipl/client'
2
- require_relative 'pipl/default'
3
-
4
- module Pipl
5
-
6
- class << self
7
- include Pipl::Configurable
8
-
9
- attr_accessor :logger
10
-
11
- def client
12
- @client = Client.new(options) unless defined?(@client) && @client.same_options?(options)
13
- @client
14
- end
15
-
16
-
17
- def respond_to_missing?(method_name, include_private=false)
18
- ; client.respond_to?(method_name, include_private);
19
- end if RUBY_VERSION >= '1.9'
20
-
21
- def respond_to?(method_name, include_private=false)
22
- ; client.respond_to?(method_name, include_private) || super;
23
- end if RUBY_VERSION < '1.9'
24
-
25
- private
26
-
27
- def method_missing(method_name, *args, &block)
28
- return super unless client.respond_to?(method_name)
29
- client.send(method_name, *args, &block)
30
- end
31
-
32
- end
33
- end
34
-
35
- Pipl.setup
1
+ require_relative 'pipl/client'
2
+ require_relative 'pipl/default'
3
+
4
+ module Pipl
5
+
6
+ class << self
7
+ include Pipl::Configurable
8
+
9
+ attr_accessor :logger
10
+
11
+ def client
12
+ @client = Client.new(options) unless defined?(@client) && @client.same_options?(options)
13
+ @client
14
+ end
15
+
16
+
17
+ def respond_to_missing?(method_name, include_private=false)
18
+ ; client.respond_to?(method_name, include_private);
19
+ end if RUBY_VERSION >= '1.9'
20
+
21
+ def respond_to?(method_name, include_private=false)
22
+ ; client.respond_to?(method_name, include_private) || super;
23
+ end if RUBY_VERSION < '1.9'
24
+
25
+ private
26
+
27
+ def method_missing(method_name, *args, &block)
28
+ return super unless client.respond_to?(method_name)
29
+ client.send(method_name, *args, &block)
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ Pipl.setup
@@ -1,177 +1,177 @@
1
- require 'uri'
2
- require 'net/http'
3
-
4
- require_relative 'configurable'
5
- require_relative 'containers'
6
- require_relative 'errors'
7
- require_relative 'fields'
8
- require_relative 'response'
9
-
10
-
11
- module Pipl
12
-
13
- class Client
14
-
15
- include Pipl::Configurable
16
-
17
- QUERY_PARAMS = %w(minimum_probability minimum_match hide_sponsored live_feeds show_sources match_requirements
18
- source_category_requirements infer_persons)
19
-
20
- def initialize(options = {})
21
- Pipl::Configurable.keys.each do |key|
22
- instance_variable_set(:"@#{key}", options[key] || Pipl.instance_variable_get(:"@#{key}"))
23
- end
24
-
25
- end
26
-
27
- def same_options?(opts)
28
- opts.hash == options.hash
29
- end
30
-
31
- def search(params={})
32
- opts = options.merge params
33
- create_search_person(opts)
34
- validate_search_params(opts)
35
- http, req = create_http_request(opts)
36
- if opts.key? :callback
37
- do_send_async http, req, opts[:callback]
38
- elsif opts[:async] and block_given?
39
- do_send_async http, req, Proc.new
40
- else
41
- do_send http, req
42
- end
43
- end
44
-
45
- private
46
-
47
- def create_search_person(opts)
48
- return if opts.key? :search_pointer
49
-
50
- person = opts[:person] || Pipl::Person.new
51
- person.add_field Pipl::Name.new(raw: opts[:raw_name]) if opts[:raw_name]
52
- person.add_field Pipl::Email.new(address: opts[:email]) if opts[:email]
53
- person.add_field Pipl::Username.new(content: opts[:username]) if opts[:username]
54
- person.add_field Pipl::Address.new(raw: opts[:raw_address]) if opts[:raw_address]
55
-
56
- if opts[:first_name] || opts[:middle_name] || opts[:last_name]
57
- person.add_field Pipl::Name.new(first: opts[:first_name], middle: opts[:middle_name], last: opts[:last_name])
58
- end
59
-
60
- if opts[:country] || opts[:state] || opts[:city]
61
- person.add_field Pipl::Address.new(country: opts[:country], state: opts[:state], city: opts[:city])
62
- end
63
-
64
- if opts[:phone]
65
- if opts[:phone].is_a? String
66
- person.add_field Pipl::Phone.new(raw: opts[:phone])
67
- else
68
- person.add_field Pipl::Phone.new(number: opts[:phone])
69
- end
70
- end
71
-
72
- if opts[:from_age] || opts[:to_age]
73
- person.add_field Pipl::DOB.from_age_range((opts[:from_age] || 0).to_i, (opts[:to_age].to_i || 1000).to_i)
74
- end
75
-
76
- opts[:person] = person
77
- end
78
-
79
- def validate_search_params(opts)
80
- unless opts[:api_key] and not opts[:api_key].empty?
81
- raise ArgumentError.new('API key is missing')
82
- end
83
-
84
- if opts[:search_pointer] and opts[:search_pointer].empty?
85
- raise ArgumentError.new('Given search pointer is empty')
86
- end
87
-
88
- unless opts.key? :search_pointer
89
- unless opts[:person] and opts[:person].is_searchable?
90
- raise ArgumentError.new('No valid name/username/user_id/phone/email/address or search pointer in request')
91
- end
92
- end
93
-
94
- if opts[:strict_validation]
95
- if opts[:minimum_probability] and not (0..1).include? opts[:minimum_probability]
96
- raise ArgumentError.new('minimum_probability must be a float in 0..1')
97
- end
98
-
99
- if opts[:minimum_match] and not (0..1).include? opts[:minimum_match]
100
- raise ArgumentError.new('minimum_match must be a float in 0..1')
101
- end
102
-
103
- unless [Pipl::Configurable::SHOW_SOURCES_ALL,
104
- Pipl::Configurable::SHOW_SOURCES_MATCHING,
105
- Pipl::Configurable::SHOW_SOURCES_NONE, nil, true, false].include? opts[:show_sources]
106
- raise ArgumentError.new('show_sources must be one of all, matching, false or a boolean value')
107
- end
108
-
109
- if opts[:match_requirements] and not opts[:match_requirements].is_a? String
110
- raise ArgumentError.new('match_requirements must be a String')
111
- end
112
-
113
- if opts[:source_category_requirements] and not opts[:source_category_requirements].is_a? String
114
- raise ArgumentError.new('source_category_requirements must be a String')
115
- end
116
-
117
- if opts[:infer_persons] and not [nil, false, true].include? opts[:infer_persons]
118
- raise ArgumentError.new('infer_persons must be true, false or nil')
119
- end
120
-
121
- unless opts.key? :search_pointer
122
- unsearchable = opts[:person].unsearchable_fields
123
- if unsearchable and not unsearchable.empty?
124
- raise ArgumentError.new("Some fields are unsearchable: #{unsearchable}")
125
- end
126
- end
127
- end
128
- end
129
-
130
- def create_http_request(opts)
131
- uri = URI(opts[:api_endpoint])
132
- query_params = ["key=#{opts[:api_key]}"] +
133
- QUERY_PARAMS.map { |k| "#{k}=#{opts[k.to_sym]}" unless opts[k.to_sym].nil? }
134
- query_params << opts[:extra] or []
135
- query_params << uri.query
136
- uri.query = URI.escape(query_params.compact.join('&'))
137
-
138
- req = Net::HTTP::Post.new(uri.request_uri)
139
- req['User-Agent'] = opts[:user_agent]
140
- if opts.key? :search_pointer
141
- req.set_form_data search_pointer: opts[:search_pointer]
142
- else
143
- h = opts[:person].to_hash
144
- req.set_form_data person: h.reject { |_, value| value.nil? }.to_json
145
- end
146
-
147
- http = Net::HTTP.new(uri.host, uri.port)
148
- http.use_ssl = uri.scheme == 'https'
149
- http.open_timeout = opts[:http_timeout]
150
-
151
- [http, req]
152
- end
153
-
154
- def do_send(http, req)
155
- response = http.request(req)
156
- if response.is_a? Net::HTTPSuccess
157
- SearchResponse.from_http_response(response)
158
- else
159
- raise Pipl::Client::APIError.from_http_response(response)
160
- end
161
- end
162
-
163
- def do_send_async(http, req, callback)
164
- Thread.new do
165
- begin
166
- response = do_send http, req
167
- callback.call response: response
168
- rescue Pipl::Client::APIError => err
169
- callback.call error: err
170
- rescue Exception => msg
171
- callback.call error: msg
172
- end
173
- end
174
- end
175
-
176
- end
177
- end
1
+ require 'uri'
2
+ require 'net/http'
3
+
4
+ require_relative 'configurable'
5
+ require_relative 'containers'
6
+ require_relative 'errors'
7
+ require_relative 'fields'
8
+ require_relative 'response'
9
+
10
+
11
+ module Pipl
12
+
13
+ class Client
14
+
15
+ include Pipl::Configurable
16
+
17
+ QUERY_PARAMS = %w(minimum_probability minimum_match hide_sponsored live_feeds show_sources match_requirements
18
+ source_category_requirements infer_persons)
19
+
20
+ def initialize(options = {})
21
+ Pipl::Configurable.keys.each do |key|
22
+ instance_variable_set(:"@#{key}", options[key] || Pipl.instance_variable_get(:"@#{key}"))
23
+ end
24
+
25
+ end
26
+
27
+ def same_options?(opts)
28
+ opts.hash == options.hash
29
+ end
30
+
31
+ def search(params={})
32
+ opts = options.merge params
33
+ create_search_person(opts)
34
+ validate_search_params(opts)
35
+ http, req = create_http_request(opts)
36
+ if opts.key? :callback
37
+ do_send_async http, req, opts[:callback]
38
+ elsif opts[:async] and block_given?
39
+ do_send_async http, req, Proc.new
40
+ else
41
+ do_send http, req
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def create_search_person(opts)
48
+ return if opts.key? :search_pointer
49
+
50
+ person = opts[:person] || Pipl::Person.new
51
+ person.add_field Pipl::Name.new(raw: opts[:raw_name]) if opts[:raw_name]
52
+ person.add_field Pipl::Email.new(address: opts[:email]) if opts[:email]
53
+ person.add_field Pipl::Username.new(content: opts[:username]) if opts[:username]
54
+ person.add_field Pipl::Address.new(raw: opts[:raw_address]) if opts[:raw_address]
55
+
56
+ if opts[:first_name] || opts[:middle_name] || opts[:last_name]
57
+ person.add_field Pipl::Name.new(first: opts[:first_name], middle: opts[:middle_name], last: opts[:last_name])
58
+ end
59
+
60
+ if opts[:country] || opts[:state] || opts[:city]
61
+ person.add_field Pipl::Address.new(country: opts[:country], state: opts[:state], city: opts[:city])
62
+ end
63
+
64
+ if opts[:phone]
65
+ if opts[:phone].is_a? String
66
+ person.add_field Pipl::Phone.new(raw: opts[:phone])
67
+ else
68
+ person.add_field Pipl::Phone.new(number: opts[:phone])
69
+ end
70
+ end
71
+
72
+ if opts[:from_age] || opts[:to_age]
73
+ person.add_field Pipl::DOB.from_age_range((opts[:from_age] || 0).to_i, (opts[:to_age].to_i || 1000).to_i)
74
+ end
75
+
76
+ opts[:person] = person
77
+ end
78
+
79
+ def validate_search_params(opts)
80
+ unless opts[:api_key] and not opts[:api_key].empty?
81
+ raise ArgumentError.new('API key is missing')
82
+ end
83
+
84
+ if opts[:search_pointer] and opts[:search_pointer].empty?
85
+ raise ArgumentError.new('Given search pointer is empty')
86
+ end
87
+
88
+ unless opts.key? :search_pointer
89
+ unless opts[:person] and opts[:person].is_searchable?
90
+ raise ArgumentError.new('No valid name/username/user_id/phone/email/address or search pointer in request')
91
+ end
92
+ end
93
+
94
+ if opts[:strict_validation]
95
+ if opts[:minimum_probability] and not (0..1).include? opts[:minimum_probability]
96
+ raise ArgumentError.new('minimum_probability must be a float in 0..1')
97
+ end
98
+
99
+ if opts[:minimum_match] and not (0..1).include? opts[:minimum_match]
100
+ raise ArgumentError.new('minimum_match must be a float in 0..1')
101
+ end
102
+
103
+ unless [Pipl::Configurable::SHOW_SOURCES_ALL,
104
+ Pipl::Configurable::SHOW_SOURCES_MATCHING,
105
+ Pipl::Configurable::SHOW_SOURCES_NONE, nil, true, false].include? opts[:show_sources]
106
+ raise ArgumentError.new('show_sources must be one of all, matching, false or a boolean value')
107
+ end
108
+
109
+ if opts[:match_requirements] and not opts[:match_requirements].is_a? String
110
+ raise ArgumentError.new('match_requirements must be a String')
111
+ end
112
+
113
+ if opts[:source_category_requirements] and not opts[:source_category_requirements].is_a? String
114
+ raise ArgumentError.new('source_category_requirements must be a String')
115
+ end
116
+
117
+ if opts[:infer_persons] and not [nil, false, true].include? opts[:infer_persons]
118
+ raise ArgumentError.new('infer_persons must be true, false or nil')
119
+ end
120
+
121
+ unless opts.key? :search_pointer
122
+ unsearchable = opts[:person].unsearchable_fields
123
+ if unsearchable and not unsearchable.empty?
124
+ raise ArgumentError.new("Some fields are unsearchable: #{unsearchable}")
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ def create_http_request(opts)
131
+ uri = URI(opts[:api_endpoint])
132
+ query_params = ["key=#{opts[:api_key]}"] +
133
+ QUERY_PARAMS.map { |k| "#{k}=#{opts[k.to_sym]}" unless opts[k.to_sym].nil? }
134
+ query_params << opts[:extra] or []
135
+ query_params << uri.query
136
+ uri.query = URI.escape(query_params.compact.join('&'))
137
+
138
+ req = Net::HTTP::Post.new(uri.request_uri)
139
+ req['User-Agent'] = opts[:user_agent]
140
+ if opts.key? :search_pointer
141
+ req.set_form_data search_pointer: opts[:search_pointer]
142
+ else
143
+ h = opts[:person].to_hash
144
+ req.set_form_data person: h.reject { |_, value| value.nil? }.to_json
145
+ end
146
+
147
+ http = Net::HTTP.new(uri.host, uri.port)
148
+ http.use_ssl = uri.scheme == 'https'
149
+ http.open_timeout = opts[:http_timeout]
150
+
151
+ [http, req]
152
+ end
153
+
154
+ def do_send(http, req)
155
+ response = http.request(req)
156
+ if response.is_a? Net::HTTPSuccess
157
+ SearchResponse.from_http_response(response)
158
+ else
159
+ raise Pipl::Client::APIError.from_http_response(response)
160
+ end
161
+ end
162
+
163
+ def do_send_async(http, req, callback)
164
+ Thread.new do
165
+ begin
166
+ response = do_send http, req
167
+ callback.call response: response
168
+ rescue Pipl::Client::APIError => err
169
+ callback.call error: err
170
+ rescue Exception => msg
171
+ callback.call error: msg
172
+ end
173
+ end
174
+ end
175
+
176
+ end
177
+ end