Skip to content

Commit 0468bf6

Browse files
committed
Merge pull request #90 from pusher/jf-pusher-api-methods
Add docs on #channel_info and #channels
2 parents c9c70d0 + c1f026c commit 0468bf6

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,21 +125,17 @@ Pusher['a_channel'].trigger('an_event', {:some => 'data'})
125125

126126
This will continue to work, but has been replaced by `Pusher.trigger` which supports one or multiple channels.
127127

128-
### Generic requests to the Pusher REST API
128+
### Using the Pusher REST API
129129

130-
Aside from triggering events, the REST API also supports a number of operations for querying the state of the system. A reference of the available methods is available at <http://pusher.com/docs/rest_api>.
130+
This gem provides methods for accessing information from the [Pusher REST API](https://pusher.com/docs/rest_api). The documentation also shows an example of the responses from each of the API endpionts.
131131

132-
All requests must be signed by using your secret key, which is handled automatically using these methods:
132+
The following methods are provided by the gem.
133133

134-
``` ruby
135-
# using the Pusher class
136-
Pusher.get('url_without_app_id', params)
134+
- `Pusher.channel_info('channel_name')` returns information about that channel.
137135

138-
# using a client
139-
pusher_client.post('url_without_app_id', params)
140-
```
136+
- `Pusher.channel_users('channel_name')` returns a list of all the users subscribed to the channel.
141137

142-
Note that you don't need to specify your app_id in the URL, as this is inferred from your credentials.
138+
- `Pusher.channels` returns information about all the channels in your Pusher application.
143139

144140
### Asynchronous requests
145141

lib/pusher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class << self
3131
def_delegators :default_client, :timeout=, :connect_timeout=, :send_timeout=, :receive_timeout=, :keep_alive_timeout=
3232

3333
def_delegators :default_client, :get, :get_async, :post, :post_async
34-
def_delegators :default_client, :channels, :channel_info, :trigger, :trigger_async
34+
def_delegators :default_client, :channels, :channel_info, :channel_users, :trigger, :trigger_async
3535
def_delegators :default_client, :authenticate, :webhook, :channel, :[]
3636

3737
attr_writer :logger

lib/pusher/client.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ def channel_info(channel_name, params = {})
209209
get("/channels/#{channel_name}", params)
210210
end
211211

212+
# Request info for users of a channel
213+
#
214+
# GET /apps/[id]/channels/[channel_name]/users
215+
#
216+
# @param channel_name [String] Channel name (max 200 characters)
217+
# @param params [Hash] Hash of parameters for the API - see REST API docs
218+
#
219+
# @return [Hash] See Pusher API docs
220+
#
221+
# @raise [Pusher::Error] Unsuccessful response - see the error message
222+
# @raise [Pusher::HTTPError] Error raised inside http client. The original error is wrapped in error.original_error
223+
#
224+
def channel_users(channel_name, params = {})
225+
get("/channels/#{channel_name}/users", params)
226+
end
227+
212228
# Trigger an event on one or more channels
213229
#
214230
# POST /apps/[app_id]/events

spec/client_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@
128128
end
129129
end
130130

131+
describe '#channel_users' do
132+
it "should call correct URL and symbolise response" do
133+
api_path = %r{/apps/20/channels/mychannel/users}
134+
stub_request(:get, api_path).to_return({
135+
:status => 200,
136+
:body => MultiJson.encode({
137+
'users' => [{ 'id' => 1 }]
138+
})
139+
})
140+
expect(@client.channel_users('mychannel')).to eq({
141+
:users => [{ 'id' => 1}]
142+
})
143+
end
144+
end
145+
131146
describe '#authenticate' do
132147
before :each do
133148
@custom_data = {:uid => 123, :info => {:name => 'Foo'}}

0 commit comments

Comments
 (0)