Class: Backchat::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from BackchatClient::BackchatLogger

#debug, #error, included, #logger

Constructor Details

#initialize(api_key, endpoint = nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • *api_key*

    application identifier

  • *endpoint*

    Backchat endpoint



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_keyObject

Returns the value of attribute api_key.



22
23
24
# File 'lib/backchat-client.rb', line 22

def api_key
  @api_key
end

#endpointObject (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

Parameters:

  • channel_type

    kind of channel: check Backchat documentation about supported channel types

  • id

    channel unique identifier (in user scope)



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

Parameters:

  • *name*

    unique stream identifier

  • *description* (optional)
  • *filters* (optional)

    array of filters



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_userObject

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

Parameters:

  • *name*
  • *force*

    true|false if channel should be deleted even if being used in a stream



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_channelObject

Retrieves a specific channel or all the channels associated to the api_key

Returns:

  • one or more channel data



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

Parameters:

  • *name* (optional)

    stream name. If undefined, all the user streams are retrieved

Returns:

  • stream data hash



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_profileObject

get user profile. The api_key used should be associated to a user

Returns:

  • user profile or nil if invalid api_key



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.

Parameters:

  • stream_slug

    stream name

  • channels (defaults to: [])

    array of channels to be included in the stream

  • reset (defaults to: false)

    the channels are added (false) to the current channels or remove (true) the previous ones

  • filter (defaults to: nil)

    valid Lucene syntax to filter the channels

Returns:

  • true if the stream was successfully updated

Raises:

  • exception if stream is not found



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

Returns:

  • (Boolean)

    true|false



38
39
40
# File 'lib/backchat-client.rb', line 38

def valid?
  !get_profile.nil?
end