Skip to content

Commit f5bada5

Browse files
committed
Channel: cleanup, remove unnecessary duplication
1 parent c4bab8c commit f5bada5

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

lib/pusher/channel.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
require 'multi_json'
33

44
module Pusher
5-
# Trigger events on Channels
5+
# Delegates operations for a specific channel from a client
66
class Channel
77
attr_reader :name
88
INVALID_CHANNEL_REGEX = /[^A-Za-z0-9_\-=@,.;]/
9-
def initialize(base_url, name, client = Pusher)
10-
@uri = base_url.dup
9+
10+
def initialize(_, name, client = Pusher)
1111
if Pusher::Channel::INVALID_CHANNEL_REGEX.match(name)
1212
raise Pusher::Error, "Illegal channel name '#{name}'"
13-
elsif name.length > 164
13+
elsif name.length > 200
1414
raise Pusher::Error, "Channel name too long (limit 164 characters) '#{name}'"
1515
end
16-
@uri.path = @uri.path + "/channels/#{name}/"
1716
@name = name
1817
@client = client
1918
end
@@ -93,7 +92,7 @@ def trigger(event_name, data, socket_id = nil)
9392
# @raise [Pusher::HTTPError] on any error raised inside http client - the original error is available in the original_error attribute
9493
#
9594
def info(attributes = [])
96-
@client.get("/channels/#{name}", :info => attributes.join(','))
95+
@client.channel_info(name, :info => attributes.join(','))
9796
end
9897

9998
# Request users for a presence channel
@@ -102,12 +101,13 @@ def info(attributes = [])
102101
# @example Response
103102
# [{"id"=>"4"}]
104103
#
104+
# @param params [Hash] Hash of parameters for the API - see REST API docs
105105
# @return [Hash] Array of user hashes for this channel
106106
# @raise [Pusher::Error] on invalid Pusher response - see the error message for more details
107107
# @raise [Pusher::HTTPError] on any error raised inside Net::HTTP - the original error is available in the original_error attribute
108108
#
109-
def users
110-
@client.get("/channels/#{name}/users")[:users]
109+
def users(params = {})
110+
@client.channel_users(name, params)[:users]
111111
end
112112

113113
# Compute authentication string required as part of the authentication

lib/pusher/client.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,18 @@ def webhook(request)
187187
WebHook.new(request, self)
188188
end
189189

190-
# Return a convenience channel object by name. No API request is made.
190+
# Return a convenience channel object by name that delegates operations
191+
# on a channel. No API request is made.
191192
#
192193
# @example
193194
# Pusher['my-channel']
194195
# @return [Channel]
195-
# @raise [ConfigurationError] unless key, secret and app_id have been
196-
# configured. Channel names should be less than 200 characters, and
196+
# @raise [Pusher::Error] if the channel name is invalid.
197+
# Channel names should be less than 200 characters, and
197198
# should not contain anything other than letters, numbers, or the
198199
# characters "_\-=@,.;"
199200
def channel(channel_name)
200-
Channel.new(url, channel_name, self)
201+
Channel.new(nil, channel_name, self)
201202
end
202203

203204
alias :[] :channel
@@ -233,7 +234,7 @@ def channel_info(channel_name, params = {})
233234
get("/channels/#{channel_name}", params)
234235
end
235236

236-
# Request info for users of a channel
237+
# Request info for users of a presence channel
237238
#
238239
# GET /apps/[id]/channels/[channel_name]/users
239240
#

spec/channel_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def stub_post_to_raise(e)
8787

8888
describe '#users' do
8989
it "should call the Client#channel_users" do
90-
expect(@client).to receive(:get).with("/channels/presence-mychannel/users").and_return({:users => {'id' => '4'}})
90+
expect(@client).to receive(:get).with("/channels/presence-mychannel/users", {}).and_return({:users => {'id' => '4'}})
9191
@channel = @client['presence-mychannel']
9292
@channel.users
9393
end

spec/client_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
it "should raise exception if app_id is not configured" do
142142
@client.app_id = nil
143143
expect {
144-
@client['test_channel']
144+
@channel.trigger!('foo', 'bar')
145145
}.to raise_error(Pusher::ConfigurationError)
146146
end
147147
end
@@ -260,6 +260,18 @@
260260
expect(MultiJson.decode(req.body)["channels"]).to eq(['mychannel'])
261261
}
262262
end
263+
264+
%w[app_id key secret].each do |key|
265+
it "should fail in missing #{key}" do
266+
@client.public_send("#{key}=", nil)
267+
expect {
268+
@client.trigger('mychannel', 'event', {'some' => 'data'})
269+
}.to raise_error(Pusher::ConfigurationError)
270+
expect(WebMock).not_to have_requested(:post, @api_path).with { |req|
271+
expect(MultiJson.decode(req.body)["channels"]).to eq(['mychannel'])
272+
}
273+
end
274+
end
263275
end
264276

265277
describe '#trigger_async' do

0 commit comments

Comments
 (0)