Class: LogCourier::PendingPayload
- Inherits:
-
Object
- Object
- LogCourier::PendingPayload
- Defined in:
- lib/log-courier/client.rb
Overview
Describes a pending payload
Instance Attribute Summary collapse
-
#events ⇒ Object
Returns the value of attribute events.
-
#last_sequence ⇒ Object
Returns the value of attribute last_sequence.
-
#next ⇒ Object
Returns the value of attribute next.
-
#nonce ⇒ Object
Returns the value of attribute nonce.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#sequence_len ⇒ Object
Returns the value of attribute sequence_len.
Class Method Summary collapse
Instance Method Summary collapse
- #ack(sequence) ⇒ Object
- #generate ⇒ Object
-
#initialize(events, nonce) ⇒ PendingPayload
constructor
A new instance of PendingPayload.
Constructor Details
#initialize(events, nonce) ⇒ PendingPayload
Returns a new instance of PendingPayload.
49 50 51 52 53 54 |
# File 'lib/log-courier/client.rb', line 49 def initialize(events, nonce) @events = events @nonce = nonce generate end |
Instance Attribute Details
#events ⇒ Object
Returns the value of attribute events.
44 45 46 |
# File 'lib/log-courier/client.rb', line 44 def events @events end |
#last_sequence ⇒ Object
Returns the value of attribute last_sequence.
45 46 47 |
# File 'lib/log-courier/client.rb', line 45 def last_sequence @last_sequence end |
#next ⇒ Object
Returns the value of attribute next.
42 43 44 |
# File 'lib/log-courier/client.rb', line 42 def next @next end |
#nonce ⇒ Object
Returns the value of attribute nonce.
43 44 45 |
# File 'lib/log-courier/client.rb', line 43 def nonce @nonce end |
#payload ⇒ Object
Returns the value of attribute payload.
47 48 49 |
# File 'lib/log-courier/client.rb', line 47 def payload @payload end |
#sequence_len ⇒ Object
Returns the value of attribute sequence_len.
46 47 48 |
# File 'lib/log-courier/client.rb', line 46 def sequence_len @sequence_len end |
Class Method Details
.get_json_adapter ⇒ Object
36 37 38 39 |
# File 'lib/log-courier/client.rb', line 36 def get_json_adapter @json_adapter = MultiJson.adapter.instance if @json_adapter.nil? return @json_adapter end |
Instance Method Details
#ack(sequence) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/log-courier/client.rb', line 74 def ack(sequence) if sequence <= @last_sequence return 0, false elsif sequence >= @sequence_len lines = @sequence_len - @last_sequence @last_sequence = sequence @payload = nil @events = [] return lines, true end lines = sequence - @last_sequence @last_sequence = sequence @payload = nil @events.shift(lines) return lines, false end |
#generate ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/log-courier/client.rb', line 56 def generate fail ArgumentError, 'Corrupt payload' if @events.length == 0 buffer = Zlib::Deflate.new # Write each event in JSON format events.each do |event| json_data = self.class.get_json_adapter.dump(event) # Add length and then the data buffer << [json_data.length].pack('N') << json_data end # Generate and store the payload @payload = nonce + buffer.flush(Zlib::FINISH) @last_sequence = 0 @sequence_len = @events.length end |