Skip to content

Commit

Permalink
Add associate_by_email_address method
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-wojtowicz committed Jun 22, 2022
1 parent a024f60 commit e80de55
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/urbanairship/devices/named_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ def associate(channel_id: required('channel_id'), device_type: nil)
response
end

def associate_by_email_address(email_address: required('email_address'))
fail ArgumentError,
'named_user_id is required for association' if @named_user_id.nil?

payload = {}
payload['email_address'] = email_address
payload['named_user_id'] = @named_user_id.to_s

response = @client.send_request(
method: 'POST',
body: JSON.dump(payload),
path: named_users_path('associate'),
content_type: CONTENT_TYPE
)
logger.info { "Associated email_address #{email_address} with named_user #{@named_user_id}" }
response
end

def disassociate(channel_id: required('channel_id'), device_type: nil)
payload = {}
payload['channel_id'] = channel_id
Expand Down
43 changes: 43 additions & 0 deletions spec/lib/urbanairship/devices/named_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
let(:channel_id) { '123' }
let(:device_type) { 'android' }
let(:named_user_id) { 'user' }
let(:email_address) { 'whales@example.com' }

named_user = nil

Expand Down Expand Up @@ -112,6 +113,48 @@
end
end

describe '#associate_by_email_address' do
describe 'Request' do
after(:each) { named_user.associate_by_email_address(email_address: email_address) }

it 'makes the expected request' do
allow(airship).to receive(:send_request) do |arguments|
expect(arguments).to eq(
method: 'POST',
body: { email_address: email_address, named_user_id: named_user_id }.to_json,
path: "/named_users/associate",
content_type: described_class::CONTENT_TYPE,
)
expected_response
end
end

context 'Named user ID is an integer' do
let(:named_user_id) { 1985 }

it 'converts named user ID to a string' do
allow(airship).to receive(:send_request) do |arguments|
expect(JSON.parse(arguments[:body], symbolize_names: true)[:named_user_id]).to eq named_user_id.to_s
expected_response
end
end
end
end

it 'associates a email_address with a named_user' do
allow(airship).to receive(:send_request).and_return(expected_response)
actual_resp = named_user.associate_by_email_address(email_address: email_address)
expect(actual_resp).to eq(expected_response)
end

it 'fails when the user_id is not set' do
named_user_without_id = UA::NamedUser.new(client: airship)
expect {
named_user_without_id.associate_by_email_address(email_address: email_address)
}.to raise_error(ArgumentError)
end
end

describe '#disassociate' do
it 'disassociates a channel from a named_user' do
allow(airship).to receive(:send_request).and_return(expected_response)
Expand Down

0 comments on commit e80de55

Please sign in to comment.