Skip to content

Commit d511fe0

Browse files
committed
phone lookup helper.
1 parent 17b4a11 commit d511fe0

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ person = client.people.find_by_email(person_email)
6060
person_id = person.action_network_id
6161
puts person.email_addresses
6262

63+
# Retrieve a Person's data by their phone_number
64+
person = client.people.find_by_phone_number('+12223334444')
65+
person_id = person.action_network_id
66+
puts person.phone_numbers
67+
6368
# Update a Person
6469
client.people.update(person_id, {custom_fields: {custom_id: "12345"}})
6570

lib/action_network_rest/people.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ def find_by_email(email)
3939
set_action_network_id_on_object(person_object) if person_object.present?
4040
end
4141

42+
def find_by_phone_number(phone_number)
43+
# This works for parsing exactly 1 person's info out of the response.
44+
# The response we get from Action Network is expected to have
45+
#
46+
# "_embedded": {
47+
# "osdi:people": [{
48+
# "identifiers": [
49+
# "action_network:c947bcd0-929e-11e3-a2e9-12313d316c29"
50+
# ....
51+
# ]
52+
# }]
53+
# }
54+
#
55+
url_encoded_filter_string = url_escape("phone_number eq '#{phone_number}'")
56+
response = client.get_request "#{base_path}?filter=#{url_encoded_filter_string}"
57+
person_object = response.body[:_embedded][osdi_key].first
58+
set_action_network_id_on_object(person_object) if person_object.present?
59+
end
60+
4261
def update(id, person_data)
4362
people_path = "#{base_path}#{url_escape(id)}"
4463
response = client.put_request people_path, person_data

spec/people_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,55 @@
258258
end
259259
end
260260

261+
describe '#find_by_phone_number' do
262+
let(:person_phone) { '+12223334444' }
263+
let(:person_id) { 'abc-def-123-456' }
264+
let(:response_body) do
265+
{
266+
_embedded: {
267+
'osdi:people': [
268+
identifiers: ["action_network:#{person_id}"]
269+
]
270+
}
271+
}.to_json
272+
end
273+
let(:person_result) do
274+
{
275+
'action_network_id' => 'abc-def-123-456',
276+
'identifiers' => ['action_network:abc-def-123-456']
277+
}
278+
end
279+
let!(:get_stub) do
280+
url_encoded_filter_string = CGI.escape("phone_number eq '#{person_phone}'")
281+
stub_actionnetwork_request("/people/?filter=#{url_encoded_filter_string}", method: :get)
282+
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
283+
end
284+
285+
let(:other_phone) { '+17776665555' }
286+
let(:other_response_body) do
287+
{
288+
_embedded: {
289+
'osdi:people': []
290+
}
291+
}.to_json
292+
end
293+
let!(:other_get_stub) do
294+
url_encoded_filter_string = CGI.escape("phone_number eq '#{other_phone}'")
295+
stub_actionnetwork_request("/people/?filter=#{url_encoded_filter_string}", method: :get)
296+
.to_return(status: 200, body: other_response_body, headers: { content_type: 'application/json' })
297+
end
298+
299+
it 'should GET /people with filter request and return person object' do
300+
result = subject.people.find_by_phone_number(person_phone)
301+
expect(result).to eq(person_result)
302+
end
303+
304+
it 'should GET /people with filter request and return nil if no person' do
305+
result = subject.people.find_by_phone_number(other_phone)
306+
expect(result).to be_nil
307+
end
308+
end
309+
261310
describe '#update' do
262311
let(:person_data) do
263312
{

0 commit comments

Comments
 (0)