Class: Pandapush::Client
- Inherits:
-
Object
- Object
- Pandapush::Client
- Defined in:
- lib/pandapush/client.rb
Instance Attribute Summary collapse
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#key ⇒ Object
Returns the value of attribute key.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#generate_token(channel:, channel_type: 'private', pub: true, sub: true, presence: nil) ⇒ Object
generate_token - returns a JWT token for client authentication channel - The channel name the token works for (required) channel_type - ‘public’ or ‘private’.
-
#initialize(app_id: nil, key: nil, secret: nil, url: 'https://pp-beta.instructure.com') ⇒ Client
constructor
A new instance of Client.
-
#publish(channel:, message:, channel_type: 'private') ⇒ Object
publish - sends a message to a Pandapush channel channel - The name of the channel to push to (required) message - hash that will be JSON encoded (required) channel_type - ‘public’ or ‘private’.
Constructor Details
#initialize(app_id: nil, key: nil, secret: nil, url: 'https://pp-beta.instructure.com') ⇒ Client
Returns a new instance of Client.
9 10 11 |
# File 'lib/pandapush/client.rb', line 9 def initialize(app_id: nil, key: nil, secret: nil, url: 'https://pp-beta.instructure.com') @app_id, @key, @secret, @url = app_id, key, secret, url end |
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
7 8 9 |
# File 'lib/pandapush/client.rb', line 7 def app_id @app_id end |
#key ⇒ Object
Returns the value of attribute key.
7 8 9 |
# File 'lib/pandapush/client.rb', line 7 def key @key end |
#secret ⇒ Object
Returns the value of attribute secret.
7 8 9 |
# File 'lib/pandapush/client.rb', line 7 def secret @secret end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/pandapush/client.rb', line 7 def url @url end |
Instance Method Details
#generate_token(channel:, channel_type: 'private', pub: true, sub: true, presence: nil) ⇒ Object
generate_token - returns a JWT token for client authentication
channel - The channel name the token works for (required)
channel_type - 'public' or 'private'. Defaults to private
pub - true if this token allows publishing. Defaults to true
sub - true if this token allows subscribing. Defaults to true
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/pandapush/client.rb', line 35 def generate_token(channel:, channel_type: 'private', pub: true, sub: true, presence: nil) raise ConfigurationError, 'Missing client configuration: please check that key, secret and app_id are configured.' unless configured? payload = { keyId: key, channel: channel_uri(channel, channel_type), pub: pub, sub: sub } payload[:presence] = presence if presence JWT.encode(payload, secret) end |
#publish(channel:, message:, channel_type: 'private') ⇒ Object
publish - sends a message to a Pandapush channel
channel - The name of the channel to push to (required)
message - hash that will be JSON encoded (required)
channel_type - 'public' or 'private'. Defaults to private
20 21 22 23 24 25 |
# File 'lib/pandapush/client.rb', line 20 def publish(channel:, message:, channel_type: 'private') raise ConfigurationError, 'Missing client configuration: please check that key, secret and app_id are configured.' unless configured? RestClient.post channel_uri(channel, channel_type, include_base_url: true), .to_json, {:content_type => :json, :Authorization => "Token #{generate_token(channel: channel, channel_type: channel_type)}"} end |