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(channel_type, id, 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(type, id, 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.
29 30 31 32 |
# File 'lib/backchat-client.rb', line 29 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.
22 23 24 |
# File 'lib/backchat-client.rb', line 22 def api_key @api_key end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
23 24 25 |
# File 'lib/backchat-client.rb', line 23 def endpoint @endpoint end |
Instance Method Details
#create_channel(channel_type, id, bql = nil) ⇒ Object
Creates a specific channel
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/backchat-client.rb', line 84 def create_channel(channel_type, id, bql=nil) _channel = channel.create(generate_channel_url(channel_type, id, bql)) if _channel.respond_to?("has_key?") and _channel.has_key?("data") _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
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/backchat-client.rb', line 129 def create_stream(name, description = nil, filters = []) description.nil? and description = "Stream created using backchat-client gem" _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 end |
#delete_user ⇒ Object
Delete user account
57 58 59 |
# File 'lib/backchat-client.rb', line 57 def delete_user user.destroy end |
#destroy_channel(name, force = false) ⇒ Object
Delete a channel
100 101 102 |
# File 'lib/backchat-client.rb', line 100 def destroy_channel(name, force = false) channel.destroy(name, force) end |
#destroy_stream(name) ⇒ Object
Delete a stream
177 178 179 |
# File 'lib/backchat-client.rb', line 177 def destroy_stream(name) stream.destroy(name) end |
#find_channel ⇒ Object
Retrieves a specific channel or all the channels associated to the api_key
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/backchat-client.rb', line 67 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
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/backchat-client.rb', line 112 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(type, id, filter = nil) ⇒ Object
Helper that generates the channel url using Addressable help
184 185 186 187 188 189 190 |
# File 'lib/backchat-client.rb', line 184 def generate_channel_url(type, id, filter = nil) channel_uri = "#{type}://#{id}" 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
50 51 52 |
# File 'lib/backchat-client.rb', line 50 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.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/backchat-client.rb', line 151 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 stream.update(stream_slug, st) true end |
#valid? ⇒ Boolean
Checks if a Token is valid
38 39 40 |
# File 'lib/backchat-client.rb', line 38 def valid? !get_profile.nil? end |