Class: BackchatClient::Stream

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

Overview

A Backchat Stream allows to define a set of channels with filters

Constant Summary collapse

URI_PATH =

http uri to handle streams

"streams"

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) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • *api_key*

    application identifier

  • *endpoint*

    Backchat endpoint



19
20
21
22
# File 'lib/backchat_client/stream.rb', line 19

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

Instance Method Details

#create(name, description, filters = []) ⇒ Object

This method POST a request to create a new stream on behalf of the authenticated application

Parameters:

  • name
  • description
  • filters (defaults to: [])

    one or more valid channel URIs

  • filter_enabled

    one or more boolean values enabling|disabling the filters

Returns:

  • decoded response body



31
32
33
34
35
36
37
38
39
# File 'lib/backchat_client/stream.rb', line 31

def create(name, description, filters = [])
  begin
    data = post("", {:name => name, :description => description, :channel_filters => filters})
    ActiveSupport::JSON.decode(data)
  rescue Error::ClientError => ex
    logger.error ActiveSupport::JSON.decode(ex.response.body)
    raise ex
  end
end

#destroy(name) ⇒ Object

Delete a defined stream

Parameters:

  • *name*

    valid stream name

Returns:

  • true|false if deleted or not



61
62
63
64
65
66
67
68
69
# File 'lib/backchat_client/stream.rb', line 61

def destroy(name)
  begin
    delete(name)
    true
  rescue Error::ClientError => ex
    logger.error ex.errors
    false
  end
end

#find(name) ⇒ Object

Retrieves a stream

Parameters:

  • *name*

    get a stream

Returns:

  • stream data or nil if not found



44
45
46
47
48
49
50
51
52
# File 'lib/backchat_client/stream.rb', line 44

def find(name)
  name.nil? and name = "index.json"
  begin
    ActiveSupport::JSON.decode(get(name))
  rescue Error::ClientError => ex
    logger.error(ex.errors)
    return nil
  end
end

#update(slug, params) ⇒ Object



54
55
56
# File 'lib/backchat_client/stream.rb', line 54

def update(slug, params)
  ActiveSupport::JSON.decode(put(slug, params))
end