simple_stream 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/lib/simple_stream.rb +17 -0
- data/lib/simple_stream/client.rb +64 -0
- data/lib/simple_stream/connection.rb +18 -0
- data/lib/simple_stream/response.rb +30 -0
- data/lib/simple_stream/version.rb +3 -0
- data/simple_stream.gemspec +28 -0
- data/spec/spec_helper.rb +1 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b5376e59aff6cc7c16b2f51ea2f5190892c8e7bf
|
4
|
+
data.tar.gz: 169ea336150d5ab8048668c27e3e0782dc656675
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 066988feb6644300848e02e951bc236608287fe7ac40008df51b3ffaacb2a19fbe100605b60a842e570c72489e86ffa5f55c7c8030850dc37a755dffe15c9b49
|
7
|
+
data.tar.gz: bcad5dbaafd1a26ff25d18ec6acd766b2943abfd4e1923d83486535c374131aacf1ac1cc611b0c1c7d3b26e348d0dc595bb8ac2bfcef1a220a467f0172071d31
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Jason Kriss
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# SimpleStream
|
2
|
+
|
3
|
+
A stripped-down Twitter streaming API client.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_stream'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_stream
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Simply provide your credentials:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
SimpleStream.configure do |config|
|
25
|
+
config.consumer_key = 'YOUR_CONSUMER_KEY'
|
26
|
+
config.consumer_secret = 'YOUR_CONSUMER_SECRET'
|
27
|
+
config.access_token = 'YOUR_ACCESS_TOKEN'
|
28
|
+
config.access_secret = 'YOUR_ACCESS_SECRET'
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
and start streaming:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
client = SimpleStream::Client.new
|
36
|
+
client.user do |message|
|
37
|
+
# Do something with the JSON
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it ( http://github.com/jasonkriss/simple_stream/fork )
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'simple_stream/client'
|
2
|
+
require 'simple_stream/version'
|
3
|
+
|
4
|
+
module SimpleStream
|
5
|
+
def self.configure
|
6
|
+
yield config
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.config
|
10
|
+
@config ||= Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
class Configuration
|
14
|
+
attr_accessor :consumer_key, :consumer_secret,
|
15
|
+
:access_token, :access_secret
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'http/request'
|
2
|
+
require 'addressable/uri'
|
3
|
+
require 'simple_oauth'
|
4
|
+
require 'simple_stream/connection'
|
5
|
+
require 'simple_stream/response'
|
6
|
+
|
7
|
+
module SimpleStream
|
8
|
+
class Client
|
9
|
+
attr_reader :connection
|
10
|
+
|
11
|
+
USER_AGENT = "SimpleStream Ruby Gem #{SimpleStream::VERSION}"
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@connection = SimpleStream::Connection.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def site(user_ids, opts = {}, &block)
|
18
|
+
request(:get, 'https://sitestream.twitter.com:443/1.1/site.json', opts.merge(follow: user_ids.join(',')), &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
def user(opts = {}, &block)
|
22
|
+
request(:get, 'https://userstream.twitter.com:443/1.1/user.json', opts, &block)
|
23
|
+
end
|
24
|
+
|
25
|
+
def filter(opts = {}, &block)
|
26
|
+
request(:post, 'https://stream.twitter.com:443/1.1/statuses/filter.json', opts, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def sample(opts = {}, &block)
|
30
|
+
request(:get, 'https://stream.twitter.com:443/1.1/statuses/sample.json', opts, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
def firehose(opts = {}, &block)
|
34
|
+
request(:get, 'https://stream.twitter.com:443/1.1/statuses/firehose.json', opts, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def request(method, uri, params)
|
39
|
+
headers = default_headers.merge(:authorization => oauth_auth_header(method, uri, params).to_s)
|
40
|
+
request = HTTP::Request.new(method, uri + '?' + to_url_params(params), headers)
|
41
|
+
response = SimpleStream::Response.new { |data| yield(data) }
|
42
|
+
connection.stream(request, response)
|
43
|
+
end
|
44
|
+
|
45
|
+
def oauth_auth_header(method, uri, params = {})
|
46
|
+
uri = Addressable::URI.parse(uri)
|
47
|
+
config = SimpleStream.config
|
48
|
+
SimpleOAuth::Header.new(method, uri, params, {
|
49
|
+
consumer_key: config.consumer_key,
|
50
|
+
consumer_secret: config.consumer_secret,
|
51
|
+
token: config.access_token,
|
52
|
+
token_secret: config.access_secret
|
53
|
+
})
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_url_params(params)
|
57
|
+
params.collect { |k, v| [k, URI.encode(v)].join('=') }.sort.join('&')
|
58
|
+
end
|
59
|
+
|
60
|
+
def default_headers
|
61
|
+
{ accept: '*/*', user_agent: USER_AGENT }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
require 'resolv'
|
3
|
+
|
4
|
+
module SimpleStream
|
5
|
+
class Connection
|
6
|
+
def stream(request, response)
|
7
|
+
client_context = OpenSSL::SSL::SSLContext.new
|
8
|
+
client = TCPSocket.new(Resolv.getaddress(request.uri.host), request.uri.port)
|
9
|
+
ssl_client = OpenSSL::SSL::SSLSocket.new(client, client_context)
|
10
|
+
ssl_client.connect
|
11
|
+
request.stream(ssl_client)
|
12
|
+
|
13
|
+
while body = ssl_client.readpartial(1024)
|
14
|
+
response << body
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'buftok'
|
2
|
+
require 'http/parser'
|
3
|
+
|
4
|
+
module SimpleStream
|
5
|
+
class Response
|
6
|
+
attr_reader :block, :parser, :tokenizer
|
7
|
+
|
8
|
+
def initialize(&block)
|
9
|
+
@block = block
|
10
|
+
@parser = Http::Parser.new(self)
|
11
|
+
@tokenizer = BufferedTokenizer.new("\r\n")
|
12
|
+
end
|
13
|
+
|
14
|
+
def <<(data)
|
15
|
+
parser << data
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_headers_complete(headers)
|
19
|
+
# TODO
|
20
|
+
p(status_code: parser.status_code, header: headers) unless parser.status_code == 200
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_body(data)
|
24
|
+
tokenizer.extract(data).each do |line|
|
25
|
+
next if line.empty?
|
26
|
+
block.call(JSON.parse(line, symbolize_names: true))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_stream/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'simple_stream'
|
8
|
+
spec.version = SimpleStream::VERSION
|
9
|
+
spec.authors = ['Jason Kriss']
|
10
|
+
spec.email = ['jasonkriss@gmail.com']
|
11
|
+
spec.summary = %q{Stripped-down Twitter streaming API client.}
|
12
|
+
spec.homepage = 'https://github.com/jasonkriss/simple_stream'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_dependency 'addressable', '~> 2.3'
|
21
|
+
spec.add_dependency 'http', '~> 0.5.0'
|
22
|
+
spec.add_dependency 'simple_oauth', '~> 0.2.0'
|
23
|
+
spec.add_dependency 'buftok', '~> 0.2.0'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
26
|
+
spec.add_development_dependency 'rake'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'simple_stream'
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_stream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Kriss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: addressable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: http
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.5.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.5.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simple_oauth
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.2.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: buftok
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.2.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.2.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- jasonkriss@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- CHANGELOG.md
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- lib/simple_stream.rb
|
126
|
+
- lib/simple_stream/client.rb
|
127
|
+
- lib/simple_stream/connection.rb
|
128
|
+
- lib/simple_stream/response.rb
|
129
|
+
- lib/simple_stream/version.rb
|
130
|
+
- simple_stream.gemspec
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
homepage: https://github.com/jasonkriss/simple_stream
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
metadata: {}
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 2.1.7
|
153
|
+
signing_key:
|
154
|
+
specification_version: 4
|
155
|
+
summary: Stripped-down Twitter streaming API client.
|
156
|
+
test_files:
|
157
|
+
- spec/spec_helper.rb
|