class EventStore::HTTP::Write
Constants
- Error
- ExpectedVersionError
Public Class Methods
stream_path(stream)
click to toggle source
# File lib/event_store/http/write.rb, line 70 def self.stream_path(stream) "/streams/#{stream}" end
Public Instance Methods
build_request(batch, stream, expected_version: nil)
click to toggle source
# File lib/event_store/http/write.rb, line 57 def build_request(batch, stream, expected_version: nil) path = self.class.stream_path stream request = Net::HTTP::Post.new path request['Content-Type'] = MediaTypes::Events.mime request['ES-ExpectedVersion'] = expected_version.to_s if expected_version request.body = Transform::Write.(batch, :json) request end
call(batch, stream, expected_version: nil)
click to toggle source
# File lib/event_store/http/write.rb, line 19 def call(batch, stream, expected_version: nil) logger.trace { "Writing events (#{LogText.attributes batch, stream, expected_version})" } if batch.is_a? Array batch = MediaTypes::Events::Batch.build :events => batch end request = build_request batch, stream, expected_version: expected_version response = nil self.retry.() do |_retry| response = connection.request request if write_timeout? response logger.warn { "Write timeout (#{LogText.attributes batch, stream, expected_version, response: response})" } _retry.failed end end case response when Net::HTTPCreated then location = response['location'] logger.info { "Events written (#{LogText.attributes batch, stream, expected_version, response: response}, Location: #{location})" } URI.parse location when proc { wrong_expected_version? response } error_message = "Wrong expected version (#{LogText.attributes batch, stream, expected_version, response: response})" logger.error { error_message } raise ExpectedVersionError, error_message else error_message = "Request failed (#{LogText.attributes batch, stream, expected_version, response: response})" logger.error { error_message } raise Error, error_message end end
configure(session: nil)
click to toggle source
# File lib/event_store/http/write.rb, line 11 def configure(session: nil) if session.nil? Retry.configure self else session.configure_retry self end end
write_timeout?(response)
click to toggle source
# File lib/event_store/http/write.rb, line 78 def write_timeout?(response) Net::HTTPBadRequest === response && response.message == "Write timeout" end
wrong_expected_version?(response)
click to toggle source
# File lib/event_store/http/write.rb, line 74 def wrong_expected_version?(response) Net::HTTPBadRequest === response && response.message == "Wrong expected EventNumber" end