Class: PachubeStream::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
RequestMethods
Defined in:
lib/pachube-stream/connection.rb

Constant Summary collapse

NF_RECONNECT_START =
0.25
NF_RECONNECT_ADD =
0.25
NF_RECONNECT_MAX =
16
RECONNECT_MAX =
320
RETRIES_MAX =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestMethods

#delete, #get, #post, #put, #subscribe, #unsubsribe

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



28
29
30
31
32
33
34
# File 'lib/pachube-stream/connection.rb', line 28

def initialize(options)
  @options = options
  @api_key = options[:api_key]
  @timeout = options[:timeout] || 0
  @reconnect_retries = 0
  @immediate_reconnect = false
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



11
12
13
# File 'lib/pachube-stream/connection.rb', line 11

def api_key
  @api_key
end

#max_reconnects_callbackObject

Returns the value of attribute max_reconnects_callback.



12
13
14
# File 'lib/pachube-stream/connection.rb', line 12

def max_reconnects_callback
  @max_reconnects_callback
end

#nf_last_reconnectObject

Returns the value of attribute nf_last_reconnect.



12
13
14
# File 'lib/pachube-stream/connection.rb', line 12

def nf_last_reconnect
  @nf_last_reconnect
end

#on_error_block(&block) ⇒ Object

Returns the value of attribute on_error_block.



11
12
13
# File 'lib/pachube-stream/connection.rb', line 11

def on_error_block
  @on_error_block
end

#on_init_callbackObject

Returns the value of attribute on_init_callback.



11
12
13
# File 'lib/pachube-stream/connection.rb', line 11

def on_init_callback
  @on_init_callback
end

#on_response_block(&block) ⇒ Object

Returns the value of attribute on_response_block.



11
12
13
# File 'lib/pachube-stream/connection.rb', line 11

def on_response_block
  @on_response_block
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/pachube-stream/connection.rb', line 11

def options
  @options
end

#reconnect_callbackObject

Returns the value of attribute reconnect_callback.



12
13
14
# File 'lib/pachube-stream/connection.rb', line 12

def reconnect_callback
  @reconnect_callback
end

#reconnect_retriesObject

Returns the value of attribute reconnect_retries.



12
13
14
# File 'lib/pachube-stream/connection.rb', line 12

def reconnect_retries
  @reconnect_retries
end

Class Method Details

.connect(options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pachube-stream/connection.rb', line 15

def self.connect(options = {})
  api_key = options[:api_key]
  raise ArgumentError.new("You need to supply an API Key") unless api_key
  host = options[:host] || "beta.pachube.com"
  port = options[:port] || 8081
  EventMachine.connect host, port, self, options
  rescue EventMachine::ConnectionError => e
    conn = EventMachine::FailedConnection.new(req)
    conn.error = e.message
    conn.fail
    conn
end

Instance Method Details

#clientObject



36
37
38
# File 'lib/pachube-stream/connection.rb', line 36

def client
  @client ||= PachubeStream::Client.new(self, @api_key, @options)
end

#immediate_reconnectObject



66
67
68
69
70
# File 'lib/pachube-stream/connection.rb', line 66

def immediate_reconnect
  @immediate_reconnect = true
  @gracefully_closed = false
  close_connection
end

#on_max_reconnects(&block) ⇒ Object



49
50
51
# File 'lib/pachube-stream/connection.rb', line 49

def on_max_reconnects(&block)
  @max_reconnects_callback = block
end

#on_reconnect(&block) ⇒ Object



45
46
47
# File 'lib/pachube-stream/connection.rb', line 45

def on_reconnect(&block)
  @reconnect_callback = block
end

#post_initObject



40
41
42
43
# File 'lib/pachube-stream/connection.rb', line 40

def post_init
  set_comm_inactivity_timeout @timeout if @timeout > 0
  @on_inited_callback.call if @on_inited_callback
end

#receive_data(response) ⇒ Object



76
77
78
# File 'lib/pachube-stream/connection.rb', line 76

def receive_data(response)
  client.process_data(response)
end

#stopObject



61
62
63
64
# File 'lib/pachube-stream/connection.rb', line 61

def stop
  @gracefully_closed = true
  close_connection
end

#unbindObject



72
73
74
# File 'lib/pachube-stream/connection.rb', line 72

def unbind
  schedule_reconnect unless @gracefully_closed
end