Class: Gnip::GnipStream::Replay

Inherits:
Stream
  • Object
show all
Defined in:
lib/gnip/gnip-stream/replay.rb

Instance Attribute Summary

Attributes inherited from Stream

#backfill_client, #url, #version

Instance Method Summary collapse

Methods inherited from Stream

#handle_connection_close, #handle_error, #on_connection_close, #on_error, #on_message, #process_chunk

Constructor Details

#initialize(client) ⇒ Replay

Returns a new instance of Replay.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/gnip/gnip-stream/replay.rb', line 5

def initialize(client)
  super #version is setted in the super
  case self.version
  when '1.0'
    @url = "https://stream.gnip.com:443/accounts/#{client.}/publishers/#{client.publisher}/replay/track/#{client.replay_label}.json"
  when '2.0'
    @url = "https://gnip-stream.gnip.com/replay/powertrack/accounts/#{client.}/publishers/#{client.publisher}/#{client.replay_label}.json"
  else
    raise Exception.new("version #{self.version} is not supported from this gem.")
  end
end

Instance Method Details

#configure_handlersObject



17
18
19
20
# File 'lib/gnip/gnip-stream/replay.rb', line 17

def configure_handlers
  self.on_error { |error| @error_handler.attempt_to_reconnect("Gnip Connection Error. Reason was: #{error.inspect}") }
  self.on_connection_close { puts 'Gnip::GnipStream::Replay -> Connection closed' }
end

#connect(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gnip/gnip-stream/replay.rb', line 28

def connect(options)
  search_options = {}
  search_options[:fromDate]    = Gnip.format_date(options[:date_from])  if options[:date_from]
  search_options[:toDate]      = Gnip.format_date(options[:date_to])    if options[:date_to]
  stream_url = [self.url, search_options.to_query].join('?')
  EM.run do
    http = EM::HttpRequest.new(stream_url, inactivity_timeout: 45, connection_timeout: 75).get(head: @headers)
    http.stream { |chunk| process_chunk(chunk) }
    http.callback { 
      handle_connection_close(http) 
      EM.stop
    }
    http.errback { 
      handle_error(http)
      EM.stop
    }
  end
end

#consume(options = {}, &block) ⇒ Object



22
23
24
25
26
# File 'lib/gnip/gnip-stream/replay.rb', line 22

def consume(options={}, &block)
  @client_callback = block if block
  self.on_message(&@client_callback)
  self.connect(options)
end