Skip to content

Commit

Permalink
Implement additional Twitter::Streaming::Client methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Aug 26, 2013
1 parent 9c05a32 commit c1d8c43
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 21 deletions.
146 changes: 126 additions & 20 deletions lib/twitter/streaming/client.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'twitter/arguments'
require 'twitter/client'
require 'twitter/streaming/connection'
require 'twitter/streaming/proxy'
Expand All @@ -9,7 +10,7 @@ module Streaming
class Client < Twitter::Client
attr_writer :connection

def initialize
def initialize(options={}, &block)
super
@connection = Twitter::Streaming::Connection.new
@request_options = {
Expand All @@ -24,40 +25,145 @@ def initialize
}
end

def user(&block)
user!(&block).value
def firehose(options={}, &block)
firehose!(options, &block).value
end

def user!(&block)
def firehose!(options={}, &block)
request({
:method => 'GET',
:host => 'userstream.twitter.com',
:path => '/1.1/user.json',
:params => {},
:method => 'GET',
:host => 'stream.twitter.com',
:path => '/1.1/statuses/firehose.json',
:params => options,
}) do |data|
begin
block.call(Tweet.new(data))
block.call(Tweet.new(data)) if data[:id]
rescue StandardError => error
p(data)
p(error)
end
end
end

def track(*keywords, &block)
track!(*keywords, &block).value
def follow(*args, &block)
follow!(*args, &block).value
end

def track!(*keywords, &block)
options = {
:method => 'POST',
:host => 'stream.twitter.com',
:path => '/1.1/statuses/filter.json',
:params => {'track' => keywords.join(',')},
}
request(options) do |data|
def follow!(*args, &block)
arguments = Twitter::Arguments.new(args)
request({
:method => 'POST',
:host => 'stream.twitter.com',
:path => '/1.1/statuses/filter.json',
:params => arguments.options.merge(:follow => arguments.join(',')),
}) do |data|
begin
block.call(Tweet.new(data)) if data[:id]
rescue StandardError => error
p(data)
p(error)
end
end
end

def locations(*args, &block)
locations!(*args, &block).value
end

def locations!(*args, &block)
arguments = Twitter::Arguments.new(args)
request({
:method => 'POST',
:host => 'stream.twitter.com',
:path => '/1.1/statuses/filter.json',
:params => arguments.options.merge(:locations => arguments.join(',')),
}) do |data|
begin
block.call(Tweet.new(data)) if data[:id]
rescue StandardError => error
p(data)
p(error)
end
end
end

def sample(options={}, &block)
sample!(options, &block).value
end

def sample!(options={}, &block)
request({
:method => 'GET',
:host => 'stream.twitter.com',
:path => '/1.1/statuses/sample.json',
:params => options,
}) do |data|
begin
block.call(Tweet.new(data)) if data[:id]
rescue StandardError => error
p(data)
p(error)
end
end
end

def site(*args, &block)
site!(*args, &block).value
end

def site!(*args, &block)
arguments = Twitter::Arguments.new(args)
request({
:method => 'POST',
:host => 'sitestream.twitter.com',
:path => '/1.1/site.json',
:params => arguments.options.merge(:follow => arguments.join(',')),
}) do |data|
begin
block.call(Tweet.new(data)) if data[:id]
rescue StandardError => error
p(data)
p(error)
end
end
end

def track(*args, &block)
track!(*args, &block).value
end

def track!(*args, &block)
arguments = Twitter::Arguments.new(args)
request({
:method => 'POST',
:host => 'stream.twitter.com',
:path => '/1.1/statuses/filter.json',
:params => arguments.options.merge(:track => arguments.join(',')),
}) do |data|
begin
block.call(Tweet.new(data)) if data[:id]
rescue StandardError => error
p(data)
p(error)
end
end
end

def user(options={}, &block)
user!(options, &block).value
end

def user!(options={}, &block)
request({
:method => 'GET',
:host => 'userstream.twitter.com',
:path => '/1.1/user.json',
:params => options,
}) do |data|
begin
block.call(Tweet.new(data))
block.call(Tweet.new(data)) if data[:id]
rescue StandardError => error
p(data)
p(error)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/twitter/streaming/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Streaming
class Request
attr_reader :proxy, :options

def initialize(options = {})
def initialize(options={})
@options = options
@proxy = Proxy.new(@options.delete(:proxy)) if @options[:proxy]
end
Expand Down
1 change: 1 addition & 0 deletions lib/twitter/streaming/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(block)

def on_headers_complete(headers)
# TODO: handle response codes
p(headers)
end

def on_body(data)
Expand Down

0 comments on commit c1d8c43

Please sign in to comment.