Sha256: d6fced562daaf11cc744c5decb0b798878e6c7cce95dbc2d0f37ff0fffd70e5a

Contents?: true

Size: 895 Bytes

Versions: 4

Compression:

Stored size: 895 Bytes

Contents

require_relative './page'

module EsHttpClient

  class StreamEvents

    def initialize(name, connection)
      @connection = connection
      @latest_ref = Ref.head_of(name)
    end

    def each(&block)
      page = read_stream_page(@latest_ref.uri)
      last = page.last
      page = read_stream_page(last) if last
      loop do
        break unless page.has_entries?
        page.each_event(&block)
        next_page = page.previous
        break unless next_page
        page = read_stream_page(next_page)
      end
      return @latest_ref
    end

    private

    def read_stream_page(uri)
      response = @connection.get(uri, @latest_ref.etag)
      @latest_ref = Ref.new(uri, response.headers['etag'])
      Page.new(response.body)
    end

    def find_link(links, rel)
      link = links.detect { |l| l['relation'] == rel }
      link.nil? ? nil : link['uri']
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
es-http-client-0.2.0 lib/es_http_client/stream_events.rb
es-http-client-0.1.2 lib/es_http_client/stream_events.rb
es-http-client-0.1.1 lib/es_http_client/stream_events.rb
es-http-client-0.1.0 lib/es_http_client/stream_events.rb