Skip to content

Commit 5d3248d

Browse files
committed
Merge pull request #41 from pusher/channels_can_be_list_or_string
Channel(s) parameter can now be a collection or a string
2 parents 218f299 + 4d7346a commit 5d3248d

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pusher/pusher.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ def trigger(self, channels, event_name, data, socket_id=None):
6666
6767
http://pusher.com/docs/rest_api#method-post-event
6868
'''
69-
if isinstance(channels, six.string_types) or not isinstance(channels, (collections.Sized, collections.Iterable)):
70-
raise TypeError("Expected a collection of channels (each channel should be %s)" % text)
69+
70+
if isinstance(channels, dict) or not (isinstance(channels, six.string_types) or isinstance(channels, (collections.Sized, collections.Iterable))):
71+
raise TypeError("Expected a single string or collection of channels (each channel should be %s)" % text)
72+
73+
if isinstance(channels, six.string_types):
74+
channels = [channels]
7175

7276
if len(channels) > 10:
7377
raise ValueError("Too many channels")

pusher_tests/test_pusher.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestPusher(unittest.TestCase):
1616
def setUp(self):
1717
self.pusher = Pusher(config=Config.from_url(u'http://key:secret@somehost/apps/4'))
1818

19-
def test_trigger_success_case(self):
19+
def test_trigger_with_channels_list_success_case(self):
2020
json_dumped = u'{"message": "hello world"}'
2121

2222
with mock.patch('json.dumps', return_value=json_dumped) as json_dumps_mock:
@@ -35,9 +35,24 @@ def test_trigger_success_case(self):
3535

3636
json_dumps_mock.assert_called_once({u'message': u'hello world'})
3737

38-
def test_trigger_disallow_single_channel(self):
38+
def test_trigger_with_channel_string_success_case(self):
39+
json_dumped = u'{"message": "hello world"}'
40+
41+
with mock.patch('json.dumps', return_value=json_dumped) as json_dumps_mock:
42+
43+
request = self.pusher.trigger.make_request(u'some_channel', u'some_event', {u'message': u'hello world'})
44+
45+
expected_params = {
46+
u'channels': [u'some_channel'],
47+
u'data': json_dumped,
48+
u'name': u'some_event'
49+
}
50+
51+
self.assertEqual(request.params, expected_params)
52+
53+
def test_trigger_disallow_non_string_or_list_channels(self):
3954
self.assertRaises(TypeError, lambda:
40-
self.pusher.trigger.make_request(u'some_channel', u'some_event', {u'message': u'hello world'}))
55+
self.pusher.trigger.make_request({u'channels': u'test_channel'}, u'some_event', {u'message': u'hello world'}))
4156

4257
def test_trigger_disallow_invalid_channels(self):
4358
self.assertRaises(ValueError, lambda:

0 commit comments

Comments
 (0)