Class: BackchatClient::User

Inherits:
Object
  • Object
show all
Includes:
BackchatLogger, HttpClient
Defined in:
lib/backchat_client/user.rb

Overview

A Backchat User is a developer that has an api_key and can create applications using Backchat API

Constant Summary collapse

URI_PATH =

no suffix for user path

nil

Instance Method Summary collapse

Methods included from BackchatLogger

#debug, #error, included, #logger

Methods included from HttpClient

#delete, #get, #post, #put, #response_handler

Constructor Details

#initialize(api_key, endpoint) ⇒ User

Returns a new instance of User.

Parameters:

  • *api_key*

    application identifier

  • *endpoint*

    Backchat endpoint



20
21
22
23
# File 'lib/backchat_client/user.rb', line 20

def initialize(api_key, endpoint)
  @api_key = api_key
  @endpoint = endpoint
end

Instance Method Details

#destroyObject

Delete user account



28
29
30
# File 'lib/backchat_client/user.rb', line 28

def destroy
  delete("")
end

#findObject

this method return the user profile in case that the token provided is valid

Returns:

  • user profile with information about channels, streams and plan

  • nil if token is invalid or an unexpected error takes place



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/backchat_client/user.rb', line 35

def find
  begin
    debug("Fetching user profile")
    data = get("index.json")
    if data.is_a?(String)
      data = ActiveSupport::JSON.decode(data)
      if data.has_key?("data")
        data["data"]
      else
        nil
      end
    end
  rescue BackchatClient::Error::ClientError => ex
    error "Invalid api_key #{@api_key}"
    nil # Invalid token
  rescue Exception => ex
    logger.error " Unexpected error"
    logger.error ex.inspect
    nil # Unexpected error
  end
end