Class: ESParrot

Inherits:
Object
  • Object
show all
Includes:
Neighborparrot
Defined in:
lib/neighborparrot/esparrot.rb

Instance Method Summary collapse

Methods included from Neighborparrot

configuration, configure, #post, send

Instance Method Details

#closeObject

close the active connection



38
39
40
41
42
43
# File 'lib/neighborparrot/esparrot.rb', line 38

def close
  return unless connected?
  @connection.finish()
  @current_thread.kill
  @current_thread = nil
end

#connected?Boolean

Returns true if a connection exists and is started.

Returns:

  • (Boolean)

    true if a connection exists and is started



33
34
35
# File 'lib/neighborparrot/esparrot.rb', line 33

def connected?
  @connection && @connection.started?
end

#on_close(&block) ⇒ Object

Define a block called on connection closed



58
59
60
# File 'lib/neighborparrot/esparrot.rb', line 58

def on_close(&block)
  @on_close_blk = block
end

#on_connect(&block) ⇒ Object

Define a block called on connect



63
64
65
# File 'lib/neighborparrot/esparrot.rb', line 63

def on_connect(&block)
  @on_connect_blk = block
end

#on_error(&block) ⇒ Object

Define a block called on error An optional param with the error should be pass if present



53
54
55
# File 'lib/neighborparrot/esparrot.rb', line 53

def on_error(&block)
  @on_error_blk = block
end

#on_message(&block) ⇒ Object

Define a block called on message received The received message is passed to the block as a var



47
48
49
# File 'lib/neighborparrot/esparrot.rb', line 47

def on_message(&block)
  @on_message_blk = block
end

#open(params = {}) ⇒ Object

Open a persistent connection to the Neighbor in a new thread and return true if all works unless :foreground options is true. Current options to the connectio are:

  • :foreground [Boolean] run the connection in the foreground

    stoping the clode flow until the connection is closed by server or
    another thread call close
    
  • :api_id => Your api ID in neighborparrot.com

  • :api_key => Your api key

  • :server => Server to connect (Only for development)

Parameters:

  • channel (String)

    to connect

  • Params (Hash)

    for the connection. this params can be:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/neighborparrot/esparrot.rb', line 17

def open(params={})
  params = Neighborparrot.configuration.merge(params)
  Neighborparrot.check_params params, :open
  return false if connected?
  if ! params[:foreground] == true
    close if @current_thread # If previus thread but closed connection, kill it
    @current_thread = Thread.new(params) do | params|
      open_connection params
    end
    return true
  else
    open_connection params
  end
end