-
Notifications
You must be signed in to change notification settings - Fork 123
/
pusher.rb
77 lines (64 loc) · 2.04 KB
/
pusher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
autoload 'Logger', 'logger'
require 'uri'
require 'forwardable'
require 'pusher/client'
# Used for configuring API credentials and creating Channel objects
#
module Pusher
# All errors descend from this class so they can be easily rescued
#
# @example
# begin
# Pusher.trigger('channel_name', 'event_name, {:some => 'data'})
# rescue Pusher::Error => e
# # Do something on error
# end
class Error < RuntimeError; end
class AuthenticationError < Error; end
class ConfigurationError < Error; end
class HTTPError < Error; attr_accessor :original_error; end
class << self
extend Forwardable
def_delegators :default_client, :scheme, :host, :port, :app_id, :key, :secret, :http_proxy
def_delegators :default_client, :scheme=, :host=, :port=, :app_id=, :key=, :secret=, :http_proxy=
def_delegators :default_client, :authentication_token, :url
def_delegators :default_client, :encrypted=, :url=
def_delegators :default_client, :get, :get_async, :post, :post_async
def_delegators :default_client, :channels, :channel_info, :trigger, :trigger_async
def_delegators :default_client, :webhook, :channel
attr_writer :logger
# Return a channel by name
#
# @example
# Pusher['my-channel']
# @return [Channel]
# @raise [ConfigurationError] unless key, secret and app_id have been
# configured
def [](channel_name)
begin
default_client[channel_name]
rescue ConfigurationError
raise ConfigurationError, 'Missing configuration: please check that Pusher.key, Pusher.secret and Pusher.app_id are configured.'
end
end
def logger
@logger ||= begin
log = Logger.new($stdout)
log.level = Logger::INFO
log
end
end
private
def default_client
@default_client ||= Pusher::Client.new
end
end
if ENV['PUSHER_URL']
self.url = ENV['PUSHER_URL']
end
end
require 'pusher/channel'
require 'pusher/query_encoder'
require 'pusher/request'
require 'pusher/resource'
require 'pusher/webhook'