Class: PromptScrub::StreamRehydrator
- Inherits:
-
Object
- Object
- PromptScrub::StreamRehydrator
- Defined in:
- lib/promptscrub/stream_rehydrator.rb
Constant Summary collapse
- PARTIAL_TOKEN =
/<[A-Z_\d]*\z/
Instance Method Summary collapse
- #call(chunk) ⇒ Object
- #flush ⇒ Object
-
#initialize(vault, &callback) ⇒ StreamRehydrator
constructor
A new instance of StreamRehydrator.
Constructor Details
#initialize(vault, &callback) ⇒ StreamRehydrator
Returns a new instance of StreamRehydrator.
7 8 9 10 11 |
# File 'lib/promptscrub/stream_rehydrator.rb', line 7 def initialize(vault, &callback) @vault = vault @callback = callback @buffer = '' end |
Instance Method Details
#call(chunk) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/promptscrub/stream_rehydrator.rb', line 13 def call(chunk) combined = @buffer + chunk @buffer = '' if (match = combined.match(PARTIAL_TOKEN)) @buffer = match[0] combined = combined[0, match.begin(0)] end @callback.call(rehydrate(combined)) unless combined.empty? end |
#flush ⇒ Object
25 26 27 28 29 |
# File 'lib/promptscrub/stream_rehydrator.rb', line 25 def flush result = rehydrate(@buffer) @buffer = '' @callback.call(result) unless result.empty? end |