Class: Backchat::Client
- Inherits:
-
Object
- Object
- Backchat::Client
- Includes:
- BackchatClient::BackchatLogger
- Defined in:
- lib/backchat-client.rb
Overview
This class is the main entry point to use the backchat-client gem. It creates a client with a specific api_key that connects to Backchat and processes any request.
Constant Summary collapse
- DEFAULT_ENDPOINT =
default endpoint where Backchat is deployed
"https://api.backchat.io/1"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Instance Method Summary collapse
-
#create_channel(uri, bql = nil) ⇒ Object
Creates a specific channel.
-
#create_stream(name, description = nil, filters = []) ⇒ Object
Create a specific stream.
-
#delete_user ⇒ Object
Delete user account.
-
#destroy_channel(name, force = false) ⇒ Object
Delete a channel.
-
#destroy_stream(name) ⇒ Object
Delete a stream.
-
#find_channel ⇒ Object
Retrieves a specific channel or all the channels associated to the api_key.
-
#find_stream(name = nil) ⇒ Object
Retrieves a specific stream.
-
#generate_channel_url(channel_uri, filter = nil) ⇒ Object
Helper that generates the channel url using Addressable help.
-
#get_profile ⇒ Object
get user profile.
-
#initialize(api_key, endpoint = nil) ⇒ Client
constructor
A new instance of Client.
-
#set_channels(stream_slug, channels = [], reset = false, filter = nil) ⇒ Object
This method updates the stream, assigning the new channels array to it.
-
#valid? ⇒ Boolean
Checks if a Token is valid.
Methods included from BackchatClient::BackchatLogger
#debug, #error, included, #logger
Constructor Details
#initialize(api_key, endpoint = nil) ⇒ Client
Returns a new instance of Client.
32 33 34 35 |
# File 'lib/backchat-client.rb', line 32 def initialize(api_key, endpoint = nil) @api_key = api_key @endpoint = endpoint.nil? ? DEFAULT_ENDPOINT : endpoint end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
25 26 27 |
# File 'lib/backchat-client.rb', line 25 def api_key @api_key end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
26 27 28 |
# File 'lib/backchat-client.rb', line 26 def endpoint @endpoint end |
Instance Method Details
#create_channel(uri, bql = nil) ⇒ Object
Creates a specific channel
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/backchat-client.rb', line 85 def create_channel(uri, bql=nil) _channel = channel.create(generate_channel_url(uri, bql)) if _channel.respond_to?("has_key?") and _channel.has_key?("data") logger.debug("Channel created in Backchat #{_channel}") _channel["data"] else logger.error("Invalid data received while creating channel #{_channel}") raise "No data received from Backchat while creating channel" end end |
#create_stream(name, description = nil, filters = []) ⇒ Object
Create a specific stream
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/backchat-client.rb', line 127 def create_stream(name, description = nil, filters = []) description.nil? and description = "Stream created using backchat-client gem" begin _stream = stream.create(name, description, filters) if _stream.respond_to?("has_key?") and _stream.has_key?("data") _stream["data"] else logger.error("Invalid data received while creating stream: #{_stream}") raise "No data received from Backchat while creating stream" end rescue BackchatClient::Error::ClientError => ex logger.error("There was an error creating the stream: #{ex.errors}") nil end end |
#delete_user ⇒ Object
Delete user account
60 61 62 |
# File 'lib/backchat-client.rb', line 60 def delete_user user.destroy end |
#destroy_channel(name, force = false) ⇒ Object
Delete a channel
101 102 103 |
# File 'lib/backchat-client.rb', line 101 def destroy_channel(name, force = false) channel.destroy(name, force) end |
#destroy_stream(name) ⇒ Object
Delete a stream
186 187 188 |
# File 'lib/backchat-client.rb', line 186 def destroy_stream(name) stream.destroy(name) end |
#find_channel ⇒ Object
Retrieves a specific channel or all the channels associated to the api_key
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/backchat-client.rb', line 69 def find_channel channels = channel.find # check that the data has the expected format if channels.respond_to?("has_key?") and channels.has_key?("data") channels["data"] else logger.info("Unable to find channels") nil end end |
#find_stream(name = nil) ⇒ Object
Retrieves a specific stream
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/backchat-client.rb', line 111 def find_stream(name = nil) streams = stream.find(name) if !streams.nil? and streams.respond_to?("has_key?") and streams.has_key?("data") streams["data"] else logger.debug "Stream with name #{name} not found in backchat" nil end end |
#generate_channel_url(channel_uri, filter = nil) ⇒ Object
Helper that generates the channel url using Addressable help
193 194 195 196 197 198 |
# File 'lib/backchat-client.rb', line 193 def generate_channel_url(channel_uri, filter = nil) if filter channel_uri += "?q=#{filter}" end Addressable::URI.parse(channel_uri).normalize.to_s end |
#get_profile ⇒ Object
get user profile. The api_key used should be associated to a user
53 54 55 |
# File 'lib/backchat-client.rb', line 53 def get_profile user.find end |
#set_channels(stream_slug, channels = [], reset = false, filter = nil) ⇒ Object
This method updates the stream, assigning the new channels array to it. In order to simplify, all the channels received will be automatically enabled.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/backchat-client.rb', line 152 def set_channels(stream_slug, channels = [], reset = false, filter = nil) st = stream.find(stream_slug) or raise "stream does not exist" st = st["data"] # format the channels array channels.map { |channel| channel[:enabled] = true channel[:channel]+="?q=#{filter}" unless filter.nil? channel[:channel] = Addressable::URI.parse(channel[:channel]).normalize.to_s } if reset # reset the stream channels st["channel_filters"] = channels else # add the new channels to the existing ones st["channel_filters"] |= channels end begin logger.debug("Updating stream channels to #{st}") stream.update(stream_slug, st) true rescue Exception => ex logger.error("Error while updating stream channels : #{ex.}") logger.error(ex) false end end |
#valid? ⇒ Boolean
Checks if a Token is valid
41 42 43 |
# File 'lib/backchat-client.rb', line 41 def valid? !get_profile.nil? end |