Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace buftok due to incorrect delimiter handling #484

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/twitter/streaming/buffered_tokenizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Twitter
module Streaming
class BufferedTokenizer
def initialize(delimiter)
@delimiter = delimiter
@buffer = ""
end

def extract(data)
@buffer << data
items = @buffer.split(@delimiter)
if @buffer.end_with?(@delimiter)
@buffer = ''
items
else
@buffer = items.pop
items
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/twitter/streaming/response.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'buftok'
require 'twitter/streaming/buffered_tokenizer'

module Twitter
module Streaming
Expand Down
25 changes: 25 additions & 0 deletions spec/twitter/streaming/buffered_tokenizer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'helper'

describe Twitter::Streaming::BufferedTokenizer do
let(:tokenizer) { described_class.new("\r\n") }

describe '#extract' do
it 'returns an empty array when no delimiter is given' do
expect(tokenizer.extract("foo bar")).to be_empty
end

it 'returns a token' do
expect(tokenizer.extract("foo\r\n")).to eq ["foo"]
end

it 'returns multiple tokens' do
expect(tokenizer.extract("foo")).to be_empty
expect(tokenizer.extract("bar\r\nbaz\r\n")).to eq ["foobar", "baz"]
end

it 'handles splitted delimiter' do
expect(tokenizer.extract("foo\r")).to be_empty
expect(tokenizer.extract("\n")).to eq ["foo"]
end
end
end
1 change: 0 additions & 1 deletion twitter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'twitter/version'

Gem::Specification.new do |spec|
spec.add_dependency 'buftok', '~> 0.1.0'
spec.add_dependency 'descendants_tracker', '~> 0.0.1'
spec.add_dependency 'equalizer', '~> 0.0.7'
spec.add_dependency 'faraday', ['>= 0.8', '< 0.10']
Expand Down