Class: Gnip::GnipStream::Replay
- Defined in:
- lib/gnip/gnip-stream/replay.rb
Instance Attribute Summary
Attributes inherited from Stream
#backfill_client, #url, #version
Instance Method Summary collapse
- #configure_handlers ⇒ Object
- #connect(options) ⇒ Object
- #consume(options = {}, &block) ⇒ Object
-
#initialize(client) ⇒ Replay
constructor
A new instance of Replay.
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.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/gnip/gnip-stream/replay.rb', line 4 def initialize(client) super # version is setted in the super case version when '1.0' @url = "https://stream.gnip.com:443/accounts/#{client.account}/publishers/#{client.publisher}/replay/track/#{client.replay_label}.json" when '2.0' @url = "https://gnip-stream.gnip.com/replay/powertrack/accounts/#{client.account}/publishers/#{client.publisher}/#{client.replay_label}.json" else raise Exception.new("version #{version} is not supported from this gem.") end end |
Instance Method Details
#configure_handlers ⇒ Object
16 17 18 19 |
# File 'lib/gnip/gnip-stream/replay.rb', line 16 def configure_handlers on_error { |error| @error_handler.attempt_to_reconnect("Gnip Connection Error. Reason was: #{error.inspect}") } on_connection_close { puts 'Gnip::GnipStream::Replay -> Connection closed' } end |
#connect(options) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/gnip/gnip-stream/replay.rb', line 27 def connect() = {} [:fromDate] = Gnip.format_date([:date_from]) if [:date_from] [:toDate] = Gnip.format_date([:date_to]) if [:date_to] stream_url = [url, .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
21 22 23 24 25 |
# File 'lib/gnip/gnip-stream/replay.rb', line 21 def consume( = {}, &block) @client_callback = block if block (&@client_callback) connect() end |