Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: kmamykin/em-http-stream
base: 6b5c2fa4a4
...
head fork: kmamykin/em-http-stream
compare: e0b3b67a9e
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
4 .gitignore
@@ -7,4 +7,6 @@ pkg
Gemfile.lock
coverage/*
.yardoc/*
-doc/*
+doc/*
+.idea/
+.rvmrc
View
16 twitter-stream.gemspec → em-http-stream.gemspec
@@ -1,16 +1,16 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = %q{twitter-stream}
- s.version = "0.1.14"
+ s.name = %q{em-http-stream}
+ s.version = "0.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Vladimir Kolesnikov"]
- s.date = %q{2010-10-05}
- s.description = %q{Simple Ruby client library for twitter streaming API. Uses EventMachine for connection handling. Adheres to twitter's reconnection guidline. JSON format only.}
- s.summary = %q{Twitter realtime API client}
- s.homepage = %q{http://github.com/voloko/twitter-stream}
- s.email = %q{voloko@gmail.com}
+ s.authors = ["Vladimir Kolesnikov", "Kliment Mamykin"]
+ s.date = %q{2011-12-22}
+ s.description = %q{Simple Ruby client library for consuming HTTP streaming API. Uses EventMachine for connection handling. JSON format only.}
+ s.summary = %q{HTTP streaming API client}
+ s.homepage = %q{http://github.com/kmamykin/em-http-stream}
+ s.email = %q{kmamyk@gmail.com}
s.platform = Gem::Platform::RUBY
s.rubygems_version = %q{1.3.6}
View
40 examples/base_reader.rb
@@ -0,0 +1,40 @@
+require 'rubygems'
+lib_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
+
+require 'em-http-stream/json_stream'
+
+class BaseReader
+ def self.run(options)
+ EventMachine::run do
+ stream = EventMachine::JSONStream.connect(options)
+
+ stream.each_item do |item|
+ puts "*" * 80
+ puts item
+ end
+
+ stream.on_error do |message|
+ puts "error: #{message}\n"
+ end
+
+ stream.on_reconnect do |timeout, retries|
+ puts "reconnecting in: #{timeout} seconds\n"
+ end
+
+ stream.on_max_reconnects do |timeout, retries|
+ puts "Failed after #{retries} failed reconnects\n"
+ end
+
+ trap('INT') {
+ stream.stop
+ EventMachine.stop if EventMachine.reactor_running?
+ }
+ trap('TERM') {
+ stream.stop
+ EventMachine.stop if EventMachine.reactor_running?
+ }
+ end
+ end
+end
+
View
9 examples/meetup.rb
@@ -0,0 +1,9 @@
+$LOAD_PATH.unshift './examples'
+
+require 'base_reader'
+
+BaseReader.run(
+ :host => 'stream.meetup.com',
+ :path => '/2/rsvps'
+ #:path => '/2/open_events'
+)
View
40 examples/reader.rb
@@ -1,40 +0,0 @@
-require 'rubygems'
-lib_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
-$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
-
-require 'twitter/json_stream'
-
-EventMachine::run {
- stream = Twitter::JSONStream.connect(
- :path => '/1/statuses/filter.json',
- :auth => 'LOGIN:PASSWORD',
- :method => 'POST',
- :content => 'track=basketball,football,baseball,footy,soccer'
- )
-
- stream.each_item do |item|
- $stdout.print "item: #{item}\n"
- $stdout.flush
- end
-
- stream.on_error do |message|
- $stdout.print "error: #{message}\n"
- $stdout.flush
- end
-
- stream.on_reconnect do |timeout, retries|
- $stdout.print "reconnecting in: #{timeout} seconds\n"
- $stdout.flush
- end
-
- stream.on_max_reconnects do |timeout, retries|
- $stdout.print "Failed after #{retries} failed reconnects\n"
- $stdout.flush
- end
-
- trap('TERM') {
- stream.stop
- EventMachine.stop if EventMachine.reactor_running?
- }
-}
-puts "The event loop has ended"
View
13 examples/twitter_filter.rb
@@ -0,0 +1,13 @@
+$LOAD_PATH.unshift './examples'
+
+require 'base_reader'
+
+fail "Usage: ruby ./examples/twitter_sample.rb <twitter_username> <twitter_password>" unless ARGV[0] && ARGV[1]
+BaseReader.run(
+ :auth =>"#{ARGV[0]}:#{ARGV[1]}",
+ :ssl => true,
+ :host => 'stream.twitter.com',
+ :path => '/1/statuses/filter.json',
+ :method => 'POST',
+ :content => 'track=basketball'
+)
View
11 examples/twitter_sample.rb
@@ -0,0 +1,11 @@
+$LOAD_PATH.unshift './examples'
+
+require 'base_reader'
+
+fail "Usage: ruby ./examples/twitter_sample.rb <twitter_username> <twitter_password>" unless ARGV[0] && ARGV[1]
+BaseReader.run(
+ :auth =>"#{ARGV[0]}:#{ARGV[1]}",
+ :ssl => true,
+ :host => 'stream.twitter.com',
+ :path => '/1/statuses/sample.json'
+)
View
9 lib/twitter/json_stream.rb → lib/em-http-stream/json_stream.rb
@@ -4,7 +4,7 @@
require 'simple_oauth'
require 'http/parser'
-module Twitter
+module EventMachine
class JSONStream < EventMachine::Connection
MAX_LINE_LENGTH = 1024*1024
@@ -25,11 +25,10 @@ class JSONStream < EventMachine::Connection
:path => '/',
:content_type => "application/x-www-form-urlencoded",
:content => '',
- :path => '/1/statuses/filter.json',
- :host => 'stream.twitter.com',
+ :host => 'localhost',
:port => 80,
:ssl => false,
- :user_agent => 'TwitterStream',
+ :user_agent => 'EM-HTTP-Stream',
:timeout => 0,
:proxy => ENV['HTTP_PROXY'],
:auth => nil,
@@ -181,7 +180,7 @@ def reset_state
@code = 0
@headers = {}
@state = :init
- @buffer = BufferedTokenizer.new("\r", MAX_LINE_LENGTH)
+ @buffer = BufferedTokenizer.new("\n", MAX_LINE_LENGTH)
@stream = ''
@parser = Http::Parser.new
View
32 spec/twitter/json_stream_spec.rb → spec/em-http-stream/json_stream_spec.rb
@@ -1,8 +1,8 @@
$:.unshift "."
require File.dirname(__FILE__) + '/../spec_helper.rb'
-require 'twitter/json_stream'
+require 'em-http-stream/json_stream'
-include Twitter
+include EventMachine
Host = "127.0.0.1"
Port = 9550
@@ -48,21 +48,29 @@ def receive_data data
stream.should == 'TEST INSTANCE'
end
- it "should define default properties" do
+ it "should connect to default host, port and path" do
EM.should_receive(:connect).with do |host, port, handler, opts|
- host.should == 'stream.twitter.com'
+ host.should == 'localhost'
port.should == 80
- opts[:path].should == '/1/statuses/filter.json'
- opts[:method].should == 'GET'
+ opts[:path].should == '/'
end
- stream = JSONStream.connect {}
+ stream = JSONStream.connect( {} )
+ end
+
+ it "should connect to provided host, port and path" do
+ EM.should_receive(:connect).with do |host, port, handler, opts|
+ host.should == 'a.host.com'
+ port.should == 99
+ opts[:path].should == '/some/path'
+ end
+ stream = JSONStream.connect( :host => 'a.host.com', :port=>99, :path=>'/some/path' )
end
it "should connect to the proxy if provided" do
EM.should_receive(:connect).with do |host, port, handler, opts|
host.should == 'my-proxy'
port.should == 8080
- opts[:host].should == 'stream.twitter.com'
+ opts[:host].should == 'localhost'
opts[:port].should == 80
opts[:proxy].should == 'http://my-proxy:8080'
end
@@ -75,7 +83,7 @@ def receive_data data
attr_reader :stream
before :each do
$body = File.readlines(fixture_path("twitter/tweets.txt"))
- $body.each {|tweet| tweet.strip!; tweet << "\r" }
+ $body.each {|tweet| tweet.strip!; tweet << "\n" }
$data_to_send = http_response(200,"OK",{},$body)
$recieved_data = ''
$close_connection = false
@@ -83,7 +91,7 @@ def receive_data data
it "should add no params" do
connect_stream
- $recieved_data.should include('/1/statuses/filter.json HTTP')
+ $recieved_data.should include('/ HTTP')
end
it "should add custom params" do
@@ -259,7 +267,7 @@ def connect_stream_without_server(opts={},&block)
end
it "should ignore empty lines" do
- body_chunks = ["{\"screen"+"_name\"",":\"user1\"}\r\r\r{","\"id\":9876}\r\r"]
+ body_chunks = ["{\"screen"+"_name\"",":\"user1\"}\n\n\n{","\"id\":9876}\n\n"]
$data_to_send = http_response(200,"OK",{},body_chunks)
items = []
connect_stream do
@@ -273,7 +281,7 @@ def connect_stream_without_server(opts={},&block)
end
it "should parse full entities even if split" do
- body_chunks = ["{\"id\"",":1234}\r{","\"id\":9876}"]
+ body_chunks = ["{\"id\"",":1234}\n{","\"id\":9876}"]
$data_to_send = http_response(200,"OK",{},body_chunks)
items = []
connect_stream do
View
2  spec/spec_helper.rb
@@ -5,7 +5,7 @@
require 'rspec'
require 'rspec/mocks'
-require 'twitter/json_stream'
+require 'em-http-stream/json_stream'
def fixture_path(path)
File.join(File.dirname(__FILE__), '..', 'fixtures', path)

No commit comments for this range

Something went wrong with that request. Please try again.