Class: EventStore::HTTPClient::Events::Write

Inherits:
Object
  • Object
show all
Defined in:
lib/event_store_http_client/events/write.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data, stream_name, version) ⇒ Write

Returns a new instance of Write.



37
38
39
40
41
42
# File 'lib/event_store_http_client/events/write.rb', line 37

def initialize(type, data, stream_name, version)
  @type = type
  @data = data
  @stream_name = stream_name
  @version = version
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/event_store_http_client/events/write.rb', line 6

def data
  @data
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/event_store_http_client/events/write.rb', line 7

def id
  @id
end

#locationObject

Returns the value of attribute location.



9
10
11
# File 'lib/event_store_http_client/events/write.rb', line 9

def location
  @location
end

#stream_nameObject

Returns the value of attribute stream_name.



8
9
10
# File 'lib/event_store_http_client/events/write.rb', line 8

def stream_name
  @stream_name
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/event_store_http_client/events/write.rb', line 11

def type
  @type
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/event_store_http_client/events/write.rb', line 10

def version
  @version
end

Class Method Details

.!(params) ⇒ Object



17
18
19
20
21
22
# File 'lib/event_store_http_client/events/write.rb', line 17

def self.!(params)
  instance = build(params)
  instance.! do |result|
    yield result if block_given?
  end
end

.build(params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/event_store_http_client/events/write.rb', line 24

def self.build(params)
  type = params[:type]
  version = params[:version]
  data = params[:data].to_json
  stream_name = params[:stream_name]

  new(type, data, stream_name, version).tap do |instance|
    Logger.configure instance
    EventStore::HTTPClient::Client::Builder.configure instance
    instance.id = UUID::Random.get
  end
end

Instance Method Details

#!Object



44
45
46
47
48
# File 'lib/event_store_http_client/events/write.rb', line 44

def !
  make_request do |result|
    yield result
  end
end

#make_requestObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/event_store_http_client/events/write.rb', line 50

def make_request
  logger.debug "Making request to #{stream_name}"
  request = client.post("/streams/#{stream_name}") do |resp|
    logger.debug "Response #{resp.status_code}"

    if resp.status_code == 201
      yield :success if block_given?
    else
      yield resp.status_code if block_given?
    end
    resp.body_handler do |body|
      # puts "The total body received was #{body.length} bytes"
      # puts body
    end
  end

  request.put_header('ES-EventType', type)
  request.put_header("ES-EventId", id)
  request.put_header("ES-ExpectedVersion", version) if version
  request.put_header('Accept', 'application/vnd.eventstore.atom+json')
  request.put_header('Content-Length', data.length)
  request.put_header('Content-Type', 'application/json')

  request.exception_handler { |e|
    logger.error "Event #{id} failed to write, trying again"
    logger.error e
    Vertx.set_timer(rand(1000)+10) do
      make_request do |result|
        yield result
      end
    end
  }

  request.write_str(data)

  request.end
end