Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SubscriptionLists class #131

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/urbanairship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'urbanairship/devices/open_channel'
require 'urbanairship/reports/response_statistics'
require 'urbanairship/devices/static_lists'
require 'urbanairship/devices/subscription_lists'
require 'urbanairship/push/location'
require 'urbanairship/automations/pipeline'
require 'urbanairship/automations/automation'
Expand Down
46 changes: 46 additions & 0 deletions lib/urbanairship/devices/subscription_lists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'urbanairship'


module Urbanairship
module Devices
class SubscriptionLists
include Urbanairship::Common
include Urbanairship::Loggable
SUBSCRIBE = "subscribe"

def initialize(client: required('client'))
@client = client
end

def subscribe(list_id, email_addresses)
subscribe_payload = payload(SUBSCRIBE, list_id, email_addresses)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to raise an error if list_id or email_addresses aren't supplied:

        fail TypeError, 'list_id musy be provided' unless list_id.is_a? String

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I will take care of that!


response = @client.send_request(
method: 'POST',
body: JSON.dump(subscribe_payload),
path: channel_path('subscription_lists'),
content_type: 'application/json'
)
logger.info("Subscribed #{email_addresses.count} users to #{list_id}")

response
end

private

def payload(action, list_id, email_addresses)
{
subscription_lists: [
{
action: action,
list_id: list_id
}
],
audience: {
email_address: email_addresses
}
}
end
end
end
end
51 changes: 51 additions & 0 deletions spec/lib/urbanairship/devices/subscription_lists_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'
require 'urbanairship'

describe Urbanairship::Devices do
UA = Urbanairship
airship = UA::Client.new(key: '123', secret: 'abc')

describe Urbanairship::Devices::SubscriptionLists do
let(:expected_response) do
{
body: {
ok: true
},
code: 202,
}
end
let(:list_id) { "some-list" }
let(:email_addresses) { ["email@example.com"] }

describe '#update_attributes' do
let(:payload) do
{
subscription_lists: [
{
action: "subscribe",
list_id: list_id
}
],
audience: {
email_address: email_addresses
}
}
end

describe 'Request' do
it 'makes the expected request' do
allow(airship).to receive(:send_request) do |arguments|
expect(arguments).to eq(
method: 'POST',
body: payload.to_json,
path: '/channels/subscription_lists',
content_type: 'application/json',
)
expected_response
end
expect(described_class.new(client: airship).subscribe(list_id, email_addresses)).to eq(expected_response)
end
end
end
end
end