class EventStore::HTTP::ReadStream

Constants

Error
StreamNotFoundError

Attributes

embed[RW]
long_poll_duration[RW]
output_schema[W]

Public Class Methods

directions() click to toggle source
# File lib/event_store/http/read_stream.rb, line 94
def self.directions
  [:forward, :backward]
end

Public Instance Methods

call(stream, position: nil, batch_size: nil, direction: nil) click to toggle source
# File lib/event_store/http/read_stream.rb, line 18
def call(stream, position: nil, batch_size: nil, direction: nil)
  batch_size ||= Defaults.batch_size
  position ||= Defaults.position
  direction ||= Defaults.direction

  logger.trace { "Reading stream (#{LogText.attributes stream, position, batch_size, direction})" }

  unless self.class.directions.include? direction
    error_message = "Invalid direction; not `forward' or `backward' (#{LogText.attributes stream, position, batch_size, direction})"
    logger.trace { error_message }
    raise ArgumentError, error_message
  end

  slice_path = self.slice_path stream, position, batch_size, direction

  request = Net::HTTP::Get.new slice_path
  request['Accept'] = MediaTypes::Atom.mime
  request['ES-LongPoll'] = long_poll_duration.to_s if long_poll_duration

  response = connection.request request

  case response
  when Net::HTTPSuccess
    page = Transform::Read.(response.body, :json, output_schema)

    logger.info { "Stream read (#{LogText.attributes stream, position, batch_size, direction, response: response}, OutputSchema: #{output_schema})" }

    page

  when Net::HTTPNotFound
    error_message = "Stream not found (#{LogText.attributes stream, position, batch_size, direction, response: response})"
    logger.error error_message
    raise StreamNotFoundError, error_message

  else
    error_message = "Client error (#{LogText.attributes stream, position, batch_size, direction, response: response})"
    logger.error error_message
    raise Error, error_message
  end
end
default_output_schema() click to toggle source
# File lib/event_store/http/read_stream.rb, line 69
def default_output_schema
  case embed
  when :body
    MediaTypes::Atom::Page::Embed::Body
  when :rich
    MediaTypes::Atom::Page::Embed::Rich
  else
    MediaTypes::Atom::Page::Embed::None
  end
end
embed_body() click to toggle source
# File lib/event_store/http/read_stream.rb, line 90
def embed_body
  self.embed = :body
end
embed_rich() click to toggle source
# File lib/event_store/http/read_stream.rb, line 86
def embed_rich
  self.embed = :rich
end
enable_long_poll(duration=nil) click to toggle source
# File lib/event_store/http/read_stream.rb, line 80
def enable_long_poll(duration=nil)
  duration ||= Defaults.long_poll_duration

  self.long_poll_duration = duration
end
output_schema() click to toggle source
# File lib/event_store/http/read_stream.rb, line 14
def output_schema
  @output_schema ||= default_output_schema
end
slice_path(stream, position, batch_size, direction) click to toggle source
# File lib/event_store/http/read_stream.rb, line 59
def slice_path(stream, position, batch_size, direction)
  path = "/streams/#{stream}/#{position}/#{direction}/#{batch_size}"

  if embed
    path << "?embed=#{embed}"
  end

  path
end