Class: HttpEventStore::Api::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/http_event_store/api/client.rb

Constant Summary collapse

VDN_EVENTSTORE_EVENTS_JSON =
'application/vnd.eventstore.events+json'.freeze
JSON =
'application/json'.freeze
VDN_EVENTSTORE_EVENTS_JSON_HEADERS =
{ "accept" => VDN_EVENTSTORE_EVENTS_JSON, "content-type" => VDN_EVENTSTORE_EVENTS_JSON }.freeze
JSON_HEADERS =
{ "accept" => JSON, "content-type" => JSON }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, port, page_size) ⇒ Client

Returns a new instance of Client.


10
11
12
13
# File 'lib/http_event_store/api/client.rb', line 10

def initialize(endpoint, port, page_size)
  @endpoint = Endpoint.new(endpoint, port)
  @page_size = page_size
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint


14
15
16
# File 'lib/http_event_store/api/client.rb', line 14

def endpoint
  @endpoint
end

#page_sizeObject (readonly)

Returns the value of attribute page_size


14
15
16
# File 'lib/http_event_store/api/client.rb', line 14

def page_size
  @page_size
end

Instance Method Details

#append_to_stream(stream_name, event_data, expected_version = nil) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/http_event_store/api/client.rb', line 16

def append_to_stream(stream_name, event_data, expected_version = nil)
  headers =  VDN_EVENTSTORE_EVENTS_JSON_HEADERS.merge({"ES-ExpectedVersion" => "#{expected_version}"}.reject { |key, val| val.empty? })

  data = [event_data].flatten.map do |event|
    {
      eventId:   event.event_id,
      eventType: event.type,
      data:      event.data
    }
  end

  make_request(:post, "/streams/#{stream_name}", data, headers)
end

#delete_stream(stream_name, hard_delete) ⇒ Object


30
31
32
33
# File 'lib/http_event_store/api/client.rb', line 30

def delete_stream(stream_name, hard_delete)
  headers = JSON_HEADERS.merge({"ES-HardDelete" => "#{hard_delete}"})
  make_request(:delete, "/streams/#{stream_name}", {}, headers)
end

#read_stream_backward(stream_name, start, count) ⇒ Object


35
36
37
# File 'lib/http_event_store/api/client.rb', line 35

def read_stream_backward(stream_name, start, count)
  make_request(:get, "/streams/#{stream_name}/#{start}/backward/#{count}", {}, JSON_HEADERS)
end

#read_stream_forward(stream_name, start, count, long_pool = 0) ⇒ Object


39
40
41
42
# File 'lib/http_event_store/api/client.rb', line 39

def read_stream_forward(stream_name, start, count, long_pool = 0)
  headers = long_pool > 0 ? JSON_HEADERS.merge({"ES-LongPoll" => "#{long_pool}"}) : JSON_HEADERS
  make_request(:get, "/streams/#{stream_name}/#{start}/forward/#{count}", {}, headers)
end

#read_stream_page(uri) ⇒ Object Also known as: read_projection_page


44
45
46
# File 'lib/http_event_store/api/client.rb', line 44

def read_stream_page(uri)
  make_request(:get, uri, {}, JSON_HEADERS)
end