Class: PiplRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/piplrequest.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, fields_to_use) ⇒ PiplRequest

Returns a new instance of PiplRequest.



6
7
8
9
10
# File 'lib/piplrequest.rb', line 6

def initialize(api_key, fields_to_use)
  @api_key = api_key
  @fields_to_use = fields_to_use
  configure_pipl
end

Instance Method Details

#build_person(data_item) ⇒ Object

Builds person model



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/piplrequest.rb', line 97

def build_person(data_item)
  # Initial gen and required fields
  person = Pipl::Person.new
  person.add_field(gen_name(data_item))

  # Optional fields- only run if there
  location = geocode(get_field_content(data_item, :address, :city))
  person.add_field(location) if location
  
  url = gen_url(data_item)
  person.add_field(url) if url
  
  return person
end

#clean_name(name) ⇒ Object

Clean name fields to not include extra info



70
71
72
73
74
# File 'lib/piplrequest.rb', line 70

def clean_name(name)
  without_parens = name.gsub(/\((?:[^()]+)\)/, "").strip.lstrip
  without_slash = without_parens.split("/").first.strip
  without_numerals = without_slash.gsub(/\s(?:I|V)+(?:\s|$)/, "").strip.lstrip
end

#configure_piplObject

Sets Pipl API settings globally



13
14
15
16
17
18
19
20
21
# File 'lib/piplrequest.rb', line 13

def configure_pipl
  Pipl.configure do |c|
    c.api_key = @api_key
    c.show_sources = 'all'
    c.minimum_probability = 0.7
    c.minimum_match = 0.5
    c.strict_validation = true
  end
end

#gen_name(data_item) ⇒ Object

Generate the name



83
84
85
86
87
88
# File 'lib/piplrequest.rb', line 83

def gen_name(data_item)
  return Pipl::Name.new(first: get_clean_name_content(data_item, :first),
                        last: get_clean_name_content(data_item, :last),
                        raw: get_clean_name_content(data_item, :raw)
                       )
end

#gen_url(data_item) ⇒ Object

Generate the URL



91
92
93
94
# File 'lib/piplrequest.rb', line 91

def gen_url(data_item)
  Pipl::Url.new(url: get_field_content(data_item, :url, :url),
                domain: @fields_to_use[:url][:domain])
end

#geocode(location) ⇒ Object

Geocode location to get area in correct format



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/piplrequest.rb', line 48

def geocode(location)
  begin
    # Catch Washington DC case and similar
    location = "Washington D.C." if location.include?("Washington D.C.")
    location = location.gsub("Area", "").gsub("Greater", "").strip.lstrip
  
    # Geocode and get first part of response
    response = Geocoder.search(location)
    address_info = response.first.data["address_components"]
  
    # Get data for each field
    country = address_info.select { |i| i["types"].include?("country")}[0]["short_name"]
    state = address_info.select { |i| i["types"].include?("administrative_area_level_1")}[0]["short_name"]
    city = address_info.select { |i| i["types"].include?("colloquial_area") || i["types"].include?("locality")}[0]["long_name"]
    
    return Pipl::Address.new(country: country, state: state, city: city)
  rescue # Return input location if fails (default to US)
    return Pipl::Address.new(country: 'US', city: location) if location && location != ","
  end
end

#get_clean_name_content(data_item, type) ⇒ Object

Get the name content and clean it if it exists



77
78
79
80
# File 'lib/piplrequest.rb', line 77

def get_clean_name_content(data_item, type)
  name = get_field_content(data_item, :name, type)
  return clean_name(name) if name
end

#get_data(data_item) ⇒ Object

Gets the data



24
25
26
# File 'lib/piplrequest.rb', line 24

def get_data(data_item)
  return process_output(send_request(build_person(data_item)))
end

#get_field_content(data_item, field_category, field_name) ⇒ Object

Get content that should be put in field based on fields_to_use mapping



113
114
115
116
117
118
119
120
121
# File 'lib/piplrequest.rb', line 113

def get_field_content(data_item, field_category, field_name)
  data_field = @fields_to_use[field_category][field_name]
  
  # Merge multiple fields if provided
  if data_field.is_a?(Array)
    data_field.map{|d| data_item[d]}.join(", ")
  else return data_item[data_field]
  end
end

#process_output(response) ⇒ Object

Process the output



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/piplrequest.rb', line 34

def process_output(response)
  personout = Array.new

  # Handle both single persons and possible_persons response
  if response.person
    personout.push(response.person.to_hash)
  elsif response.possible_persons
    response.possible_persons.each{|r| personout.push(r.to_hash)}
  end
  
  return JSON.pretty_generate(personout)
end

#send_request(person) ⇒ Object

Sends the request



29
30
31
# File 'lib/piplrequest.rb', line 29

def send_request(person)
  response = Pipl::client.search person: person, pretty: true, hide_sponsored: true, show_sources: "all"
end