Class: Flamingo::Wader

Inherits:
Object
  • Object
show all
Defined in:
lib/flamingo/wader.rb

Defined Under Namespace

Classes: AuthenticationError, HttpStatusError, InvalidParametersError, MaxReconnectsExceededError, ServerUnavailableError, UnknownStreamError, WaderError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(screen_name, password, stream) ⇒ Wader

Returns a new instance of Wader.



33
34
35
36
37
38
39
# File 'lib/flamingo/wader.rb', line 33

def initialize(screen_name,password,stream)
  self.screen_name = screen_name
  self.password = password
  self.stream = stream
  self.server_unavailable_max_retries = 5
  self.server_unavailable_wait = 60
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



28
29
30
# File 'lib/flamingo/wader.rb', line 28

def connection
  @connection
end

#passwordObject

Returns the value of attribute password.



28
29
30
# File 'lib/flamingo/wader.rb', line 28

def password
  @password
end

#screen_nameObject

Returns the value of attribute screen_name.



28
29
30
# File 'lib/flamingo/wader.rb', line 28

def screen_name
  @screen_name
end

#server_unavailable_max_retriesObject

Returns the value of attribute server_unavailable_max_retries.



28
29
30
# File 'lib/flamingo/wader.rb', line 28

def server_unavailable_max_retries
  @server_unavailable_max_retries
end

#server_unavailable_retriesObject

Returns the value of attribute server_unavailable_retries.



28
29
30
# File 'lib/flamingo/wader.rb', line 28

def server_unavailable_retries
  @server_unavailable_retries
end

#server_unavailable_waitObject

Returns the value of attribute server_unavailable_wait.



28
29
30
# File 'lib/flamingo/wader.rb', line 28

def server_unavailable_wait
  @server_unavailable_wait
end

#streamObject

Returns the value of attribute stream.



28
29
30
# File 'lib/flamingo/wader.rb', line 28

def stream
  @stream
end

Instance Method Details

#retriesObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/flamingo/wader.rb', line 70

def retries
  if connection
    # This is weird but necessary because twitter-stream increments the 
    # reconnect_retries a bit oddly. They are incremented prior to the 
    # actual reconnect which means that the last reconnect_retries value 
    # is 1 more than the real value.
    rs = connection.reconnect_retries
    rs == 0 ? 0 : rs - 1
  else
    0
  end
end

#runObject

The main EventMachine run loop

Start the stream listener (using twitter-stream, github.com/voloko/twitter-stream) Listen for responses and errors;

dispatch each for later handling

Raises:

  • (@error)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/flamingo/wader.rb', line 48

def run
  self.server_unavailable_retries = 0
  begin
    connect_and_run
  rescue => e
    # This is largely to get around a bug in Twitter-Stream that should 
    # be fixed in the next release. If the server is just not there on 
    # the first try, it blows up. Hopefully this code can be removed after 
    # that release.
    Flamingo.logger.warn "Failure initiating connection. Most likely "+
      "because server is unavailable.\n#{e}\n#{e.backtrace.join("\n\t")}"
    if server_unavailable_retries < server_unavailable_max_retries
      sleep(server_unavailable_wait)
      self.server_unavailable_retries += 1
      retry
    else
      raise ServerUnavailableError.new
    end
  end
  raise @error if @error
end

#stopObject



83
84
85
86
87
88
# File 'lib/flamingo/wader.rb', line 83

def stop
  if connection
    connection.stop
  end
  EM.stop
end