twistream 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = twistream
2
+
3
+ == what?
4
+
5
+ twistream is an simple client for hooking up with the twitter streaming api
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "twistream"
8
+ gem.summary = %Q{a simple streaming api client for twitter}
9
+ gem.description = %Q{a simple streaming api client for twitter}
10
+ gem.email = "swessel@gr4yweb.de"
11
+ gem.homepage = "http://github.com/gr4y/twistream"
12
+ gem.authors = ["Sascha Wessel"]
13
+ gem.require_path = 'lib'
14
+ gem.files = %w( Rakefile ) + Dir.glob("{lib}/**/*") + %w( VERSION )
15
+ gem.add_runtime_dependency "yajl-ruby", ">= 0.7.8"
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ desc "generate rdoc"
22
+ require 'rake/rdoctask'
23
+ Rake::RDocTask.new do |rdoc|
24
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
25
+ rdoc.rdoc_dir = 'rdoc'
26
+ rdoc.title = "twistream #{version}"
27
+ rdoc.rdoc_files.include('README*')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
30
+
31
+ desc "run all specs"
32
+ begin
33
+ gem 'rspec', '1.3.0'
34
+ require 'spec/rake/spectask'
35
+ Spec::Rake::SpecTask.new do |t|
36
+ t.spec_files = FileList['spec/**/*_spec.rb']
37
+ end
38
+ rescue LoadError
39
+ warn "[twistream] rspec is not installed. install it with: gem install rspec --version 1.3.0"
40
+ end
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "twistream #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
50
+
51
+ require 'reek/rake/task'
52
+ Reek::Rake::Task.new do |t|
53
+ t.fail_on_error = false
54
+ t.verbose = true
55
+ end
56
+
57
+ task :test => [:check_dependencies, :spec]
58
+ task :default => [:test, :build]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,60 @@
1
+ module TwiStream
2
+ class Client
3
+ extend TwiStream::EventHandler
4
+ attr_accessor :realm
5
+ events :on_error
6
+
7
+ def initialize(auth)
8
+ self.realm = "#{auth[:user]}:#{auth[:pass]}".chomp
9
+ end
10
+
11
+ def sample(params = {}, & block)
12
+ start(:sample, params, & block)
13
+ end
14
+
15
+ def retweet(params = {}, & block)
16
+ start(:retweet, params, & block)
17
+ end
18
+
19
+ def firehose(params = {}, & block)
20
+ start(:firehose, params, & block)
21
+ end
22
+
23
+ def filter_by_keywords(params = {}, & block)
24
+ start(:filter, :track => params.join(','), & block)
25
+ end
26
+
27
+ def filter_by_users(params = {}, & block)
28
+ start(:filter, :follow => params.join(','), & block)
29
+ end
30
+
31
+ def filter_by_locations(params = {}, & block)
32
+ start(:filter, :locations => params.join(','), & block)
33
+ end
34
+
35
+ def stop
36
+ if @thread.alive?
37
+ @thread.kill
38
+ end
39
+ end
40
+
41
+ private
42
+ def start(api_method, params = {}, & block)
43
+ url = build_uri(api_method)
44
+ @thread = Thread.new do
45
+ begin
46
+ ::Yajl::HttpStream.post(url, params) do |status|
47
+ puts status
48
+ block.call(status)
49
+ end
50
+ rescue
51
+ on_error.trigger($!, $@)
52
+ end
53
+ end
54
+ end
55
+
56
+ def build_uri(api_method)
57
+ URI.parse("http://#{self.realm}@stream.twitter.com/1/statuses/#{api_method.to_s}.json")
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,18 @@
1
+ module TwiStream
2
+ class Event
3
+ attr_reader :name
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ def handle(method=nil, & block)
10
+ @handler = method if method
11
+ @handler = block if block
12
+ end
13
+
14
+ def trigger(* args)
15
+ @handler.call(* args) if !@handler.nil?
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ module TwiStream
2
+ module EventHandler
3
+
4
+ def event(name)
5
+ register_event(name)
6
+ end
7
+
8
+ def events(* names)
9
+ names.each do |name|
10
+ event(name)
11
+ end
12
+ end
13
+
14
+ private
15
+ def register_event(name)
16
+ class_eval do
17
+ variable =:"@#{name}"
18
+ define_method(name) do
19
+ event = instance_variable_get(variable)
20
+ if event == nil
21
+ event = TwiStream::Event.new(name)
22
+ instance_variable_set(variable, event)
23
+ end
24
+ event
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/twistream.rb ADDED
@@ -0,0 +1,17 @@
1
+ # external require
2
+ require 'uri'
3
+ require 'logger'
4
+ require 'yajl/http_stream'
5
+
6
+ # gem files
7
+ require 'twistream/event'
8
+ require 'twistream/event_handler'
9
+ require 'twistream/client'
10
+
11
+ module TwiStream
12
+ class AuthenticationError < ::StandardError; end
13
+ LOG = ::Logger.new(STDOUT)
14
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
15
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
16
+ VERSION = ::File.exist?(PATH + 'VERSION') ? ::File.read(PATH+'VERSION') : '0'
17
+ end
@@ -0,0 +1,47 @@
1
+ require "spec_helper"
2
+
3
+ describe TwiStream::Client do
4
+
5
+ before :all do
6
+ @client = TwiStream::Client.new({:user => 'gr4y', :pass => 'bUx1kkkd'})
7
+ @client.on_error.handle do |msg,tr|
8
+ puts msg
9
+ end
10
+ end
11
+
12
+ context "start the filter stream, wait 10 seconds and stop" do
13
+ it "should start the filter stream" do
14
+ keywords = "nowplaying", "nowwatching"
15
+ @client.filter_by_keywords(keywords) do |status|
16
+ puts status
17
+ status.should_not == be_nil
18
+ end
19
+ end
20
+
21
+ it "should wait 10 seconds" do
22
+ sleep 10
23
+ end
24
+
25
+ it "should stop" do
26
+ @client.stop
27
+ end
28
+ end
29
+
30
+ context "start the sample stream, wait 10 seconds and stop" do
31
+ it "should start the sample stream" do
32
+ @client.sample do |status|
33
+ puts status
34
+ status.should_not == be_nil
35
+ end
36
+ end
37
+
38
+ it "should wait 10 seconds" do
39
+ sleep 10
40
+ end
41
+
42
+ it "should stop" do
43
+ @client.stop
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1 @@
1
+ require 'twistream'
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twistream
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Sascha Wessel
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-11 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: yajl-ruby
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 7
31
+ - 8
32
+ version: 0.7.8
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: a simple streaming api client for twitter
36
+ email: swessel@gr4yweb.de
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ files:
44
+ - Rakefile
45
+ - VERSION
46
+ - lib/twistream.rb
47
+ - lib/twistream/client.rb
48
+ - lib/twistream/event.rb
49
+ - lib/twistream/event_handler.rb
50
+ - README.rdoc
51
+ - spec/client_spec.rb
52
+ - spec/spec_helper.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/gr4y/twistream
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: a simple streaming api client for twitter
85
+ test_files:
86
+ - spec/client_spec.rb
87
+ - spec/spec_helper.rb