piplrequest 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/piplrequest.rb +114 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3b21fcc4f7921a78f6b406126892148bd11e8b10
|
4
|
+
data.tar.gz: f833372e9913f6b70071958bf36e2c1e7a6d3d94
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2afc16953e9cb0fc8cc58a0dc0d1e4fb7e44acead6643085e04f17fcf9eccd8dd1e023d553dacb912625151d181b74fe35c02a0bf50d5904d348715410012f20
|
7
|
+
data.tar.gz: fc75c71d10bdab306c8d5d6783d0256018dede5ec897f6c4ae1d5918e33bfd0382d6153d2ac4cea56016e4cfeabb9d04b9841ebf517302be30af02a88fe9103a
|
data/lib/piplrequest.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'pipl'
|
2
|
+
require 'pry'
|
3
|
+
require 'geocoder'
|
4
|
+
|
5
|
+
class PiplRequest
|
6
|
+
def initialize(api_key, fields_to_use)
|
7
|
+
@api_key = api_key
|
8
|
+
@fields_to_use = fields_to_use
|
9
|
+
configure_pipl
|
10
|
+
end
|
11
|
+
|
12
|
+
# Sets Pipl API settings globally
|
13
|
+
def configure_pipl
|
14
|
+
Pipl.configure do |c|
|
15
|
+
c.api_key = @api_key
|
16
|
+
c.show_sources = 'all'
|
17
|
+
c.minimum_probability = 0.7
|
18
|
+
c.minimum_match = 0.5
|
19
|
+
c.strict_validation = true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Gets the data
|
24
|
+
def get_data(data_item)
|
25
|
+
return process_output(send_request(build_person(data_item)))
|
26
|
+
end
|
27
|
+
|
28
|
+
# Sends the request
|
29
|
+
def send_request(person)
|
30
|
+
response = Pipl::client.search person: person, pretty: true, hide_sponsored: true, show_sources: "all"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Process the output
|
34
|
+
def process_output(response)
|
35
|
+
personout = Array.new
|
36
|
+
|
37
|
+
# Handle both single persons and possible_persons response
|
38
|
+
if response.person
|
39
|
+
personout.push(response.person.to_hash)
|
40
|
+
elsif response.possible_persons
|
41
|
+
response.possible_persons.each{|r| personout.push(r.to_hash)}
|
42
|
+
end
|
43
|
+
|
44
|
+
return JSON.pretty_generate(personout)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Geocode location to get area in correct format
|
48
|
+
def geocode(location)
|
49
|
+
begin
|
50
|
+
# Catch Washington DC case and similar
|
51
|
+
location = "Washington D.C." if location.include?("Washington D.C.")
|
52
|
+
location = location.gsub("Area", "").gsub("Greater", "").strip.lstrip
|
53
|
+
|
54
|
+
# Geocode and get first part of response
|
55
|
+
response = Geocoder.search(location)
|
56
|
+
address_info = response.first.data["address_components"]
|
57
|
+
|
58
|
+
# Get data for each field
|
59
|
+
country = address_info.select { |i| i["types"].include?("country")}[0]["short_name"]
|
60
|
+
state = address_info.select { |i| i["types"].include?("administrative_area_level_1")}[0]["short_name"]
|
61
|
+
city = address_info.select { |i| i["types"].include?("colloquial_area") || i["types"].include?("locality")}[0]["long_name"]
|
62
|
+
|
63
|
+
return Pipl::Address.new(country: country, state: state, city: city)
|
64
|
+
rescue # Return input location if fails (default to US)
|
65
|
+
return Pipl::Address.new(country: 'US', city: location) if location && location != ","
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Clean name fields to not include extra info
|
70
|
+
def clean_name(name)
|
71
|
+
return name.split("(").first.split("/").first.strip if name
|
72
|
+
end
|
73
|
+
|
74
|
+
# Generate the name
|
75
|
+
def gen_name(data_item)
|
76
|
+
return Pipl::Name.new(first: clean_name(get_field_content(data_item, :name, :first)),
|
77
|
+
last: clean_name(get_field_content(data_item, :name, :last)))
|
78
|
+
end
|
79
|
+
|
80
|
+
# Generate the URL
|
81
|
+
def gen_url(data_item)
|
82
|
+
Pipl::Url.new(url: get_field_content(data_item, :url, :url),
|
83
|
+
domain: @fields_to_use[:url][:domain])
|
84
|
+
end
|
85
|
+
|
86
|
+
# Builds person model
|
87
|
+
def build_person(data_item)
|
88
|
+
# Initial gen and required fields
|
89
|
+
person = Pipl::Person.new
|
90
|
+
person.add_field(gen_name(data_item))
|
91
|
+
|
92
|
+
# Optional fields- only run if there
|
93
|
+
location = geocode(get_field_content(data_item, :address, :city))
|
94
|
+
person.add_field(location) if location
|
95
|
+
|
96
|
+
url = gen_url(data_item)
|
97
|
+
person.add_field(url) if url
|
98
|
+
|
99
|
+
return person
|
100
|
+
end
|
101
|
+
|
102
|
+
# Get content that should be put in field based on fields_to_use mapping
|
103
|
+
def get_field_content(data_item, field_category, field_name)
|
104
|
+
data_field = @fields_to_use[field_category][field_name]
|
105
|
+
|
106
|
+
# Merge multiple fields if provided
|
107
|
+
if data_field.is_a?(Array)
|
108
|
+
data_field.map{|d| data_item[d]}.join(", ")
|
109
|
+
else return data_item[data_field]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# TODO: Test with Indeed too!
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: piplrequest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- M. C. McGrath
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Gets data from Pipl
|
14
|
+
email: shidash@shidash.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/piplrequest.rb
|
20
|
+
homepage: https://github.com/TransparencyToolkit/piplrequest
|
21
|
+
licenses:
|
22
|
+
- GPL
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.8
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Gets data from Pipl
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|