Class: Pipl::Lite

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/pipl/lite.rb

Defined Under Namespace

Classes: NotConfiguredError, ResponseError

Constant Summary collapse

@@configured =
false

Class Method Summary collapse

Class Method Details

.configure(key:) ⇒ Object



15
16
17
18
# File 'lib/pipl/lite.rb', line 15

def self.configure(key:)
  @@pipl_key = key
  @@configured = true
end

.keyObject



20
21
22
# File 'lib/pipl/lite.rb', line 20

def self.key
  @@pipl_key
end

.reset_key!Object



24
25
26
27
# File 'lib/pipl/lite.rb', line 24

def self.reset_key!
  @@pipl_key = nil
  @@configured = false
end

.search(args = {}) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pipl/lite.rb', line 29

def self.search(args={})
  raise NotConfiguredError unless @@configured
  args[:key] = key
  uri = URI(base_uri + '/search')
  uri.query = URI.encode_www_form(args)
  response = get(uri.to_s)
  case response.code
  when 200..299
    # Return the top 0-5 matches, sorted by score in descending order:
    json = JSON.parse(response.body, symbolize_names: true)
    (json.key?(:person) ? [json[:person]] : json[:possible_persons].select{ |person| person[:@match] > 0 })
      .sort_by{ |x| x[:@match] }
      .reverse[0..4] rescue []
  else
    raise ResponseError.new(response)
  end
end