Skip to content

Commit

Permalink
phone lookup helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
woodhull committed May 11, 2023
1 parent 17b4a11 commit d511fe0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ person = client.people.find_by_email(person_email)
person_id = person.action_network_id
puts person.email_addresses

# Retrieve a Person's data by their phone_number
person = client.people.find_by_phone_number('+12223334444')
person_id = person.action_network_id
puts person.phone_numbers

# Update a Person
client.people.update(person_id, {custom_fields: {custom_id: "12345"}})

Expand Down
19 changes: 19 additions & 0 deletions lib/action_network_rest/people.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ def find_by_email(email)
set_action_network_id_on_object(person_object) if person_object.present?
end

def find_by_phone_number(phone_number)
# This works for parsing exactly 1 person's info out of the response.
# The response we get from Action Network is expected to have
#
# "_embedded": {
# "osdi:people": [{
# "identifiers": [
# "action_network:c947bcd0-929e-11e3-a2e9-12313d316c29"
# ....
# ]
# }]
# }
#
url_encoded_filter_string = url_escape("phone_number eq '#{phone_number}'")
response = client.get_request "#{base_path}?filter=#{url_encoded_filter_string}"
person_object = response.body[:_embedded][osdi_key].first
set_action_network_id_on_object(person_object) if person_object.present?
end

def update(id, person_data)
people_path = "#{base_path}#{url_escape(id)}"
response = client.put_request people_path, person_data
Expand Down
49 changes: 49 additions & 0 deletions spec/people_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,55 @@
end
end

describe '#find_by_phone_number' do
let(:person_phone) { '+12223334444' }
let(:person_id) { 'abc-def-123-456' }
let(:response_body) do
{
_embedded: {
'osdi:people': [
identifiers: ["action_network:#{person_id}"]
]
}
}.to_json
end
let(:person_result) do
{
'action_network_id' => 'abc-def-123-456',
'identifiers' => ['action_network:abc-def-123-456']
}
end
let!(:get_stub) do
url_encoded_filter_string = CGI.escape("phone_number eq '#{person_phone}'")
stub_actionnetwork_request("/people/?filter=#{url_encoded_filter_string}", method: :get)
.to_return(status: 200, body: response_body, headers: { content_type: 'application/json' })
end

let(:other_phone) { '+17776665555' }
let(:other_response_body) do
{
_embedded: {
'osdi:people': []
}
}.to_json
end
let!(:other_get_stub) do
url_encoded_filter_string = CGI.escape("phone_number eq '#{other_phone}'")
stub_actionnetwork_request("/people/?filter=#{url_encoded_filter_string}", method: :get)
.to_return(status: 200, body: other_response_body, headers: { content_type: 'application/json' })
end

it 'should GET /people with filter request and return person object' do
result = subject.people.find_by_phone_number(person_phone)
expect(result).to eq(person_result)
end

it 'should GET /people with filter request and return nil if no person' do
result = subject.people.find_by_phone_number(other_phone)
expect(result).to be_nil
end
end

describe '#update' do
let(:person_data) do
{
Expand Down

0 comments on commit d511fe0

Please sign in to comment.