twitterstream 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/History.txt +3 -0
  2. data/lib/twitterstream.rb +104 -84
  3. metadata +39 -15
@@ -1,3 +1,6 @@
1
+ === 0.2.0 2011-01-08
2
+ * Support Production UserStreams
3
+
1
4
  === 0.1.1 2010-01-01
2
5
  * Fixed require 'twitter_stream' => 'twitterstream'
3
6
  * Fixed couldn't get deletion/limitation notice
@@ -5,102 +5,122 @@ require 'net/http'
5
5
  require 'uri'
6
6
  require 'rubygems'
7
7
  require 'json'
8
+ require 'oauth'
8
9
 
9
10
  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
11
+ class HTTPResponse
12
+ def each_line(rs = "\n")
13
+ stream_check
14
+ while line = @socket.readuntil(rs)
15
+ yield line
16
+ end
17
+ self
18
18
  end
19
+ end
19
20
  end
20
21
 
21
22
  class TwitterStream
22
- VERSION = '0.1.1'
23
- @@urls = {
24
- 'sample' => URI.parse("http://stream.twitter.com/1/statuses/sample.json"),
25
- 'filter' => URI.parse("http://stream.twitter.com/1/statuses/filter.json")
26
- }
27
-
28
- def initialize(username, password)
29
- @username = username
30
- @password = password
31
- self
23
+ VERSION = '0.2.0'
24
+ @@urls = {
25
+ 'sample' => URI.parse("http://stream.twitter.com/1/statuses/sample.json"),
26
+ 'filter' => URI.parse("http://stream.twitter.com/1/statuses/filter.json"),
27
+ 'userstreams' => URI.parse('https://userstream.twitter.com/2/user.json?replies=all'),
28
+ }
29
+
30
+ def initialize(params={ })
31
+ if params[:username] && params[:password]
32
+ @username = params[:username]
33
+ @password = params[:password]
34
+ else
35
+ @consumer = OAuth::Consumer.new(params[:consumer_token], params[:consumer_secret])
36
+ @access = OAuth::Token.new(params[:access_token], params[:access_secret])
32
37
  end
33
-
34
- def sample(params=nil)
35
- raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
36
- start_stream('sample', params) do |status|
37
- yield status
38
- end
38
+ self
39
+ end
40
+
41
+ def sample(params=nil)
42
+ raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
43
+ start_stream('sample', params) do |status|
44
+ yield status
39
45
  end
40
-
41
- def filter(params=nil)
42
- raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
43
- start_stream('filter', params) do |status|
44
- yield status
45
- end
46
+ end
47
+
48
+ def filter(params=nil)
49
+ raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
50
+ start_stream('filter', params) do |status|
51
+ yield status
46
52
  end
47
-
48
- def track(track, params=nil)
49
- raise ArgumentError, "track is not array or string" unless track.kind_of?(Array) || track.kind_of?(String)
50
- raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
51
-
52
- p = { 'track' => track.kind_of?(Array) ? track.map{|x| raise ArgumentError, "track item is not string or integer!" unless x.kind_of?(String) || x.kind_of?(Integer); x.kind_of?(Integer) ? x.to_s : x }.join(",") : track }
53
-
54
- p.merge!('filter',params) if params
55
- start_stream('filter', p) do |status|
56
- yield status
57
- end
53
+ end
54
+
55
+ def track(track, params=nil)
56
+ raise ArgumentError, "track is not array or string" unless track.kind_of?(Array) || track.kind_of?(String)
57
+ raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
58
+
59
+ p = { 'track' => track.kind_of?(Array) ? track.map{|x| raise ArgumentError, "track item is not string or integer!" unless x.kind_of?(String) || x.kind_of?(Integer); x.kind_of?(Integer) ? x.to_s : x }.join(",") : track }
60
+
61
+ p.merge!('filter',params) if params
62
+ start_stream('filter', p) do |status|
63
+ yield status
58
64
  end
59
-
60
- def follow(follow, params=nil)
61
- raise ArgumentError, "follow is not array or string" unless follow.kind_of?(Array) || follow.kind_of?(String)
62
- raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
63
-
64
- p = { 'follow' => follow.kind_of?(Array) ? follow.join(",") : follow }
65
- p.merge!(params) if params
66
-
67
- start_stream('filter', p) do |status|
68
- yield status
69
- end
65
+ end
66
+
67
+ def follow(follow, params=nil)
68
+ raise ArgumentError, "follow is not array or string" unless follow.kind_of?(Array) || follow.kind_of?(String)
69
+ raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
70
+
71
+ p = { 'follow' => follow.kind_of?(Array) ? follow.join(",") : follow }
72
+ p.merge!(params) if params
73
+
74
+ start_stream('filter', p) do |status|
75
+ yield status
76
+ end
77
+ end
78
+
79
+ def userstreams(params=nil)
80
+ raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
81
+ start_stream('userstreams', params) do |status|
82
+ yield status
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def start_stream(url, params=nil)
89
+ raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
90
+ raise ArgumentError, "url is not String or URI!" unless url.kind_of?(URI) || url.kind_of?(String)
91
+
92
+ if url.kind_of?(URI)
93
+ uri = url
94
+ elsif url.kind_of?(String)
95
+ if @@urls[url]
96
+ uri = @@urls[url]
97
+ elsif /^https?:/ =~ url
98
+ uri = URI.parse(url)
99
+ else
100
+ raise ArgumentError, "@@urls['#{url}'] not found"
101
+ end
102
+ end
103
+
104
+ http = Net::HTTP.new(uri.host, uri.port)
105
+ http.use_ssl = true if uri.host == "userstream.twitter.com"
106
+ http.start
107
+ request = Net::HTTP::Post.new(uri.request_uri)
108
+ request.set_form_data(params) if params
109
+ if @username && @password
110
+ request.basic_auth(@username, @password)
111
+ else
112
+ request.oauth!(http, @consumer, @access)
70
113
  end
71
114
 
72
- private
73
-
74
- def start_stream(url, params=nil)
75
- raise ArgumentError, "params is not hash" unless params.nil? || params.kind_of?(Hash)
76
- raise ArgumentError, "url is not String or URI!" unless url.kind_of?(URI) || url.kind_of?(String)
77
-
78
- if url.kind_of?(URI)
79
- uri = url
80
- elsif url.kind_of?(String)
81
- if @@urls[url]
82
- uri = @@urls[url]
83
- elsif /^https?:/ =~ url
84
- uri = URI.parse(url)
85
- else
86
- raise ArgumentError, "@@urls['#{url}'] not found"
87
- end
88
- end
89
-
90
- http = Net::HTTP.start(uri.host, uri.port)
91
- request = Net::HTTP::Post.new(uri.request_uri)
92
- request.set_form_data(params) if params
93
- request.basic_auth(@username, @password)
94
-
95
- begin
96
- http.request(request) do |response|
97
- response.each_line("\r\n") do |line|
98
- j = JSON.parse(line) rescue next
99
- yield j
100
- end
101
- end
102
- ensure
103
- http.finish
115
+ begin
116
+ http.request(request) do |response|
117
+ response.each_line("\r\n") do |line|
118
+ j = JSON.parse(line) rescue next
119
+ yield j
104
120
  end
121
+ end
122
+ ensure
123
+ http.finish
105
124
  end
125
+ end
106
126
  end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitterstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - yayugu
@@ -9,29 +15,41 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2010-01-01 00:00:00 +09:00
18
+ date: 2011-01-08 00:00:00 +09:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: json
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 31
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 0
23
34
  version: 1.2.0
24
- version:
35
+ type: :runtime
36
+ version_requirements: *id001
25
37
  - !ruby/object:Gem::Dependency
26
38
  name: hoe
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
30
42
  requirements:
31
43
  - - ">="
32
44
  - !ruby/object:Gem::Version
33
- version: 2.4.0
34
- version:
45
+ hash: 47
46
+ segments:
47
+ - 2
48
+ - 8
49
+ - 0
50
+ version: 2.8.0
51
+ type: :development
52
+ version_requirements: *id002
35
53
  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
54
  email:
37
55
  - yayugu@gmail.com
@@ -65,24 +83,30 @@ rdoc_options:
65
83
  require_paths:
66
84
  - lib
67
85
  required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
68
87
  requirements:
69
88
  - - ">="
70
89
  - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
71
93
  version: "0"
72
- version:
73
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
74
96
  requirements:
75
97
  - - ">="
76
98
  - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
77
102
  version: "0"
78
- version:
79
103
  requirements: []
80
104
 
81
105
  rubyforge_project: twitterstream
82
- rubygems_version: 1.3.5
106
+ rubygems_version: 1.4.2
83
107
  signing_key:
84
108
  specification_version: 3
85
109
  summary: It is the simple library to access the Twitter Streaming API( http://apiwiki.twitter.com/Streaming-API-Documentation )
86
110
  test_files:
87
- - test/test_twitterstream.rb
88
111
  - test/test_helper.rb
112
+ - test/test_twitterstream.rb