twitterstream 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.rdoc +18 -4
  2. data/lib/twitterstream.rb +74 -3
  3. metadata +3 -3
data/README.rdoc CHANGED
@@ -4,19 +4,33 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- FIX (describe your package)
7
+ It is the simple library to access the Twitter Streaming API( http://apiwiki.twitter.com/Streaming-API-Documentation ). It works with pure-ruby(don't need C compiler) and rubygems.
8
8
 
9
9
  == FEATURES/PROBLEMS:
10
10
 
11
- * FIX (list of features or problems)
11
+
12
12
 
13
13
  == SYNOPSIS:
14
14
 
15
- FIX (code sample of usage)
15
+ require "twitterStream"
16
+ TwitterStream::Client.new("username", "password").sample do |status|
17
+ next unless status['text']
18
+ user = status['user']
19
+ puts "#{user['screen_name']}: #{status['text']}"
20
+ end
21
+
22
+ or
23
+
24
+ require "twitterStream"
25
+ TwitterStream::Client.new("username", "password").track("bit,ly") do |status|
26
+ next unless status['text']
27
+ user = status['user']
28
+ puts "#{user['screen_name']}: #{status['text']}"
29
+ end
16
30
 
17
31
  == REQUIREMENTS:
18
32
 
19
- * FIX (list of requirements)
33
+ * json
20
34
 
21
35
  == INSTALL:
22
36
 
data/lib/twitterstream.rb CHANGED
@@ -1,6 +1,77 @@
1
1
  $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
- module Twitterstream
5
- VERSION = '0.0.1'
6
- end
4
+ require 'net/http'
5
+ require 'uri'
6
+ require 'rubygems'
7
+ require 'json'
8
+
9
+ module Net
10
+ class HTTPResponse
11
+ def each_line(rs = "\n")
12
+ stream_check
13
+ while line = @socket.readuntil(rs)
14
+ yield line
15
+ end
16
+ self
17
+ end
18
+ end
19
+ end
20
+
21
+ module TwitterStream
22
+ VERSION = '0.0.2'
23
+
24
+ class Client
25
+ def initialize(username, password)
26
+ @username = username
27
+ @password = password
28
+ self
29
+ end
30
+
31
+ def start_stream(uri, params=nil)
32
+ Net::HTTP.start(uri.host, uri.port) do |http|
33
+ request = Net::HTTP::Post.new(uri.request_uri)
34
+ request.set_form_data(params) if params
35
+ request.basic_auth(@username, @password)
36
+ http.request(request) do |response|
37
+ response.each_line("\r\n") do |line|
38
+ status = JSON.parse(line) rescue next
39
+ yield status
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def sample(params=nil)
46
+ uri = URI.parse("http://stream.twitter.com/1/statuses/sample.json")
47
+ start_stream(uri, params) do |status|
48
+ yield status
49
+ end
50
+ end
51
+
52
+ def filter(params=nil)
53
+ uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
54
+ start_stream(uri, params) do |status|
55
+ yield status
56
+ end
57
+ end
58
+
59
+ def track(track, params=nil)
60
+ uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
61
+ p = {'track'=>track}
62
+ p.merge!(params) if params
63
+ start_stream(uri, p) do |status|
64
+ yield status
65
+ end
66
+ end
67
+
68
+ def follow(follow, params=nil)
69
+ uri = URI.parse("http://stream.twitter.com/1/statuses/filter.json")
70
+ p = {'track'=>track}
71
+ p.merge!(params) if params
72
+ start_stream(uri, p) do |status|
73
+ yield status
74
+ end
75
+ end
76
+ end
77
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitterstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - yayugu
@@ -32,7 +32,7 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 2.4.0
34
34
  version:
35
- description: FIX (describe your package)
35
+ description: It is the simple library to access the Twitter Streaming API( http://apiwiki.twitter.com/Streaming-API-Documentation ). It works with pure-ruby(don't need C compiler) and rubygems.
36
36
  email:
37
37
  - yayugu@gmail.com
38
38
  executables: []
@@ -83,7 +83,7 @@ rubyforge_project: twitterstream
83
83
  rubygems_version: 1.3.5
84
84
  signing_key:
85
85
  specification_version: 3
86
- summary: FIX (describe your package)
86
+ summary: It is the simple library to access the Twitter Streaming API( http://apiwiki.twitter.com/Streaming-API-Documentation )
87
87
  test_files:
88
88
  - test/test_twitterstream.rb
89
89
  - test/test_helper.rb