piplapis-ruby 5.0.2 → 5.0.3

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.
data/lib/pipl/client.rb CHANGED
@@ -14,6 +14,9 @@ module Pipl
14
14
 
15
15
  include Pipl::Configurable
16
16
 
17
+ QUERY_PARAMS = %w(minimum_probability minimum_match hide_sponsored live_feeds show_sources match_requirements
18
+ source_category_requirements infer_persons)
19
+
17
20
  def initialize(options = {})
18
21
  Pipl::Configurable.keys.each do |key|
19
22
  instance_variable_set(:"@#{key}", options[key] || Pipl.instance_variable_get(:"@#{key}"))
@@ -111,6 +114,10 @@ module Pipl
111
114
  raise ArgumentError.new('source_category_requirements must be a String')
112
115
  end
113
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
+
114
121
  unless opts.key? :search_pointer
115
122
  unsearchable = opts[:person].unsearchable_fields
116
123
  if unsearchable and not unsearchable.empty?
@@ -122,8 +129,8 @@ module Pipl
122
129
 
123
130
  def create_http_request(opts)
124
131
  uri = URI(opts[:api_endpoint])
125
- keys = %w(minimum_probability minimum_match hide_sponsored live_feeds show_sources match_requirements source_category_requirements)
126
- query_params = ["key=#{opts[:api_key]}"] + keys.map { |k| "#{k}=#{opts[k.to_sym]}" unless opts[k.to_sym].nil? }
132
+ query_params = ["key=#{opts[:api_key]}"] +
133
+ QUERY_PARAMS.map { |k| "#{k}=#{opts[k.to_sym]}" unless opts[k.to_sym].nil? }
127
134
  query_params << opts[:extra] or []
128
135
  query_params << uri.query
129
136
  uri.query = URI.escape(query_params.compact.join('&'))
@@ -147,14 +154,9 @@ module Pipl
147
154
  def do_send(http, req)
148
155
  response = http.request(req)
149
156
  if response.is_a? Net::HTTPSuccess
150
- SearchResponse.from_json(response.body)
157
+ SearchResponse.from_http_response(response)
151
158
  else
152
- begin
153
- err = Pipl::Client::APIError.from_json(response.body)
154
- rescue
155
- err = Pipl::Client::APIError.new response.message, response.code
156
- end
157
- raise err
159
+ raise Pipl::Client::APIError.from_http_response(response)
158
160
  end
159
161
  end
160
162
 
@@ -7,7 +7,7 @@ module Pipl
7
7
  SHOW_SOURCES_NONE = 'false'
8
8
 
9
9
  attr_accessor :api_key, :minimum_probability, :minimum_match, :hide_sponsored, :live_feeds, :show_sources
10
- attr_accessor :match_requirements, :source_category_requirements, :strict_validation, :user_agent
10
+ attr_accessor :match_requirements, :source_category_requirements, :infer_persons, :strict_validation, :user_agent
11
11
  attr_writer :api_endpoint
12
12
 
13
13
  class << self
@@ -22,6 +22,7 @@ module Pipl
22
22
  :show_sources,
23
23
  :match_requirements,
24
24
  :source_category_requirements,
25
+ :infer_persons,
25
26
  :strict_validation,
26
27
  :api_endpoint,
27
28
  :user_agent
data/lib/pipl/default.rb CHANGED
@@ -46,6 +46,10 @@ module Pipl
46
46
  ENV['PIPL_SOURCE_CATEGORY_REQUIREMENTS']
47
47
  end
48
48
 
49
+ def infer_persons
50
+ ENV['PIPL_INFER_PERSONS']
51
+ end
52
+
49
53
  def strict_validation
50
54
  ENV['PIPL_USER_STRICT_VALIDATION']
51
55
  end
data/lib/pipl/errors.rb CHANGED
@@ -7,10 +7,16 @@
7
7
 
8
8
  class APIError < Exception
9
9
  attr_reader :status_code
10
+ attr_reader :qps_allotted, :qps_current, :quota_allotted, :quota_current, :quota_reset
10
11
 
11
- def initialize(message, status_code)
12
+ def initialize(message, status_code, params={})
12
13
  super message
13
14
  @status_code = status_code
15
+ @qps_allotted = params[:qps_allotted]
16
+ @qps_current = params[:qps_current]
17
+ @quota_allotted = params[:quota_allotted]
18
+ @quota_current = params[:quota_current]
19
+ @quota_reset = params[:quota_reset]
14
20
  end
15
21
 
16
22
  def is_user_error?
@@ -21,9 +27,31 @@
21
27
  not is_user_error?
22
28
  end
23
29
 
24
- def self.from_json(json_str)
30
+ def self.deserialize(json_str, headers={})
25
31
  h = JSON.parse(json_str, symbolize_names: true)
26
- self.new(h[:error], h[:@http_status_code])
32
+
33
+ # Quota and Throttle headers
34
+ params = {}
35
+ params[:qps_allotted] = headers['X-APIKey-QPS-Allotted'].to_i if headers.key? 'X-APIKey-QPS-Allotted'
36
+ params[:qps_current] = headers['X-APIKey-QPS-Current'].to_i if headers.key? 'X-APIKey-QPS-Current'
37
+ params[:quota_allotted] = headers['X-APIKey-Quota-Allotted'].to_i if headers.key? 'X-APIKey-Quota-Allotted'
38
+ params[:quota_current] = headers['X-APIKey-Quota-Current'].to_i if headers.key? 'X-APIKey-Quota-Current'
39
+ params[:quota_reset] = DateTime.strptime(headers['X-Quota-Reset'], '%A, %B %d, %Y %I:%M:%S %p %Z') if headers.key? 'X-Quota-Reset'
40
+
41
+ self.new(h[:error], h[:@http_status_code], params)
42
+ end
43
+
44
+ def self.from_http_response(resp)
45
+ begin
46
+ self.deserialize(resp.body, resp)
47
+ rescue
48
+ Pipl::Client::APIError.new resp.message, resp.code
49
+ end
50
+ end
51
+
52
+ # Here for backward compatibility
53
+ def self.from_json(json_str)
54
+ self.deserialize(json_str)
27
55
  end
28
56
 
29
57
  end
data/lib/pipl/fields.rb CHANGED
@@ -253,7 +253,7 @@ module Pipl
253
253
 
254
254
  class Email < Field
255
255
 
256
- RE_EMAIL = Regexp.new('^[\w.%\-+]+@[\w.%\-]+\.[a-zA-Z]{2,6}$')
256
+ RE_EMAIL = Regexp.new('^[a-zA-Z0-9\'._%\-+]+@[a-zA-Z0-9._%\-]+\.[a-zA-Z]{2,24}$')
257
257
 
258
258
  # @!attribute address
259
259
  # @return [String] Plain email address
data/lib/pipl/response.rb CHANGED
@@ -10,7 +10,8 @@ module Pipl
10
10
 
11
11
  attr_reader :query, :person, :sources, :possible_persons, :warnings, :visible_sources, :available_sources
12
12
  attr_reader :search_id, :http_status_code, :raw_response, :available_data, :match_requirements
13
- attr_reader :source_category_requirements
13
+ attr_reader :source_category_requirements, :person_count
14
+ attr_reader :qps_allotted, :qps_current, :quota_allotted, :quota_current, :quota_reset
14
15
 
15
16
  def initialize(params={})
16
17
  @query = params[:query]
@@ -26,9 +27,15 @@ module Pipl
26
27
  @available_data = params[:available_data]
27
28
  @match_requirements = params[:match_requirements]
28
29
  @source_category_requirements = params[:source_category_requirements]
30
+ @person_count = params[:person_count]
31
+ @qps_allotted = params[:qps_allotted]
32
+ @qps_current = params[:qps_current]
33
+ @quota_allotted = params[:quota_allotted]
34
+ @quota_current = params[:quota_current]
35
+ @quota_reset = params[:quota_reset]
29
36
  end
30
37
 
31
- def self.from_json(json_str)
38
+ def self.deserialize(json_str, headers={})
32
39
  h = JSON.parse(json_str, symbolize_names: true)
33
40
 
34
41
  params = {}
@@ -46,9 +53,36 @@ module Pipl
46
53
  params[:source_category_requirements] = h[:source_category_requirements]
47
54
  params[:available_data] = AvailableData.from_hash(h[:available_data]) if h.key? :available_data
48
55
 
56
+ # person_count: API v4 doesn't send this in the response so we compute it here
57
+ if h.key? :@person_count
58
+ params[:person_count] = h[:@person_count]
59
+ elsif h.key?(:person)
60
+ params[:person_count] = 1
61
+ elsif h.key?(:possible_persons)
62
+ params[:person_count] = h[:possible_persons].length
63
+ else
64
+ params[:person_count] = 0
65
+ end
66
+
67
+ # Quota and Throttle headers
68
+ params[:qps_allotted] = headers['X-APIKey-QPS-Allotted'].to_i if headers.key? 'X-APIKey-QPS-Allotted'
69
+ params[:qps_current] = headers['X-APIKey-QPS-Current'].to_i if headers.key? 'X-APIKey-QPS-Current'
70
+ params[:quota_allotted] = headers['X-APIKey-Quota-Allotted'].to_i if headers.key? 'X-APIKey-Quota-Allotted'
71
+ params[:quota_current] = headers['X-APIKey-Quota-Current'].to_i if headers.key? 'X-APIKey-Quota-Current'
72
+ params[:quota_reset] = DateTime.strptime(headers['X-Quota-Reset'], '%A, %B %d, %Y %I:%M:%S %p %Z') if headers.key? 'X-Quota-Reset'
73
+
49
74
  self.new(params)
50
75
  end
51
76
 
77
+ def self.from_http_response(resp)
78
+ self.deserialize(resp.body, resp)
79
+ end
80
+
81
+ # Here for backward compatibility
82
+ def self.from_json(json_str)
83
+ self.deserialize(json_str)
84
+ end
85
+
52
86
  def matching_sources
53
87
  @sources.select { |s| s.match == 1.0 } if @sources
54
88
  end
data/lib/pipl/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pipl
2
- VERSION = '5.0.2'.freeze
2
+ VERSION = '5.0.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piplapis-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.2
4
+ version: 5.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-03-17 00:00:00.000000000 Z
12
+ date: 2016-07-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler