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 all commits
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
16 changes: 16 additions & 0 deletions docs/devices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,19 @@ that have been iterated over by using the ``count`` method.
puts(apid)
end
puts(apid_list.count)


Subscription Lists
---------

Subscribe or Unsubscribe Channels to/from Subscription Lists.

.. code-block:: ruby

require 'urbanairship'

UA = Urbanairship
airship = UA::Client.new(key:'application_key', secret:'master_secret')
subscription_lists = UA::SubscriptionLists.new(client: airship)
response = subscription_lists.subscribe(list_id: "some-list", email_addresses: ["test1@example.com", "test2@example.com"])
puts(response)
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
50 changes: 50 additions & 0 deletions lib/urbanairship/devices/subscription_lists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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)
fail TypeError, 'list_id string must be privided' unless list_id.is_a? String
fail TypeError, 'email_addresses array must be privided' unless email_addresses.is_a? Array
fail TypeError, 'each email address must be a string' unless email_addresses&.all? { |email| email.is_a? String }

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
75 changes: 75 additions & 0 deletions spec/lib/urbanairship/devices/subscription_lists_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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

it 'fails and raises TypeError list_id is not a String' do
list_id = nil

expect{
described_class.new(client: airship).subscribe(list_id, email_addresses)
}.to raise_error(TypeError)
end

it 'fails and raises TypeError email_addresses is not an Array' do
email_addresses = nil

expect{
described_class.new(client: airship).subscribe(list_id, email_addresses)
}.to raise_error(TypeError)
end

it 'fails and raises TypeError an email address is not a String' do
email_addresses.push(123)

expect{
described_class.new(client: airship).subscribe(list_id, email_addresses)
}.to raise_error(TypeError)
end
end
end
end
end