http_streaming_client 0.8.1

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.
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+ require 'zlib'
4
+ require 'uri'
5
+
6
+ require 'http_streaming_client/credentials/twitter'
7
+ include HttpStreamingClient::Credentials::Twitter
8
+
9
+ describe HttpStreamingClient do
10
+
11
+ describe "twitter firehose streaming get test, no compression" do
12
+ url = "https://stream.twitter.com/1.1/statuses/sample.json"
13
+ authorization = HttpStreamingClient::Oauth::Twitter.generate_authorization(url, "get", {}, OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
14
+
15
+ subject { authorization }
16
+ it { should_not be_nil}
17
+ it { should be_instance_of(String) }
18
+
19
+ count = 0
20
+
21
+ it "should successfully retrieve JSON records from the firehose" do
22
+ expect {
23
+ client = HttpStreamingClient::Client.new(compression: false)
24
+ response = client.get(url, {:headers => {'Authorization' => "#{authorization}" }}) { |line|
25
+ count = count + 1
26
+ if count > NUM_JSON_RECORDS_TO_RECEIVE then
27
+ client.interrupt
28
+ next
29
+ end
30
+ logger.debug "json: #{JSON.parse(line).to_s}" }
31
+ }.to_not raise_error
32
+ end
33
+
34
+ end
35
+
36
+ describe "twitter firehose streaming get test, GZIP compression" do
37
+ url = "https://stream.twitter.com/1.1/statuses/sample.json"
38
+ authorization = HttpStreamingClient::Oauth::Twitter.generate_authorization(url, "get", {}, OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
39
+
40
+ subject { authorization }
41
+ it { should_not be_nil}
42
+ it { should be_instance_of(String) }
43
+
44
+ count = 0
45
+
46
+ it "should successfully retrieve decompressed JSON records from the firehose" do
47
+ expect {
48
+ client = HttpStreamingClient::Client.new(compression: true)
49
+ response = client.get(url, {:headers => {'Authorization' => "#{authorization}" }}) { |line|
50
+ count = count + 1
51
+ if count > NUM_JSON_RECORDS_TO_RECEIVE then
52
+ client.interrupt
53
+ next
54
+ end
55
+ logger.debug "json: #{JSON.parse(line).to_s}" }
56
+ }.to_not raise_error
57
+ end
58
+ end
59
+
60
+ describe "twitter firehose streaming get unauthorized failure" do
61
+ it "should fail if authorization not provided" do
62
+ expect { HttpStreamingClient::Client.get "https://stream.twitter.com/1.1/statuses/sample.json" }.to raise_error(HttpStreamingClient::HttpError)
63
+ end
64
+ end
65
+
66
+ end
@@ -0,0 +1,40 @@
1
+ require 'bundler/setup'
2
+ require 'logger'
3
+ require 'json'
4
+
5
+ require 'http_streaming_client'
6
+
7
+ HttpStreamingClient.logger.console = false
8
+ HttpStreamingClient.logger.logfile = false
9
+
10
+ require 'http_streaming_client/credentials/adobe'
11
+ include HttpStreamingClient::Credentials::Adobe
12
+
13
+ url = TOKENAPIHOST
14
+ authorization = HttpStreamingClient::Oauth::Adobe.generate_authorization(url, USERNAME, PASSWORD, CLIENTID, CLIENTSECRET)
15
+ puts "#{TOKENAPIHOST}:access token: #{authorization}"
16
+ client = HttpStreamingClient::Client.new(compression: false)
17
+ response = client.get(STREAMURL, {:headers => {'Authorization' => "Bearer #{authorization}" }}) { |line|
18
+
19
+ if line.nil? then
20
+ puts "error:nil line received"
21
+ next
22
+ end
23
+
24
+ if line.size == 0 then
25
+ puts "error:zero length line received"
26
+ next
27
+ end
28
+
29
+ if line.eql? "\r\n" then
30
+ puts "Server ping received"
31
+ next
32
+ end
33
+
34
+ begin
35
+ json = JSON.parse(line)
36
+ puts json.to_s
37
+ rescue Exception => e
38
+ puts "exception:#{e.message}:line-->#{line}<--"
39
+ end
40
+ }
@@ -0,0 +1,83 @@
1
+ require 'bundler/setup'
2
+ require 'logger'
3
+ require 'json'
4
+
5
+ require 'http_streaming_client'
6
+
7
+ HttpStreamingClient.logger.console = false
8
+ HttpStreamingClient.logger.logfile = false
9
+
10
+ require 'http_streaming_client/credentials/adobe'
11
+ include HttpStreamingClient::Credentials::Adobe
12
+
13
+ url = TOKENAPIHOST
14
+ authorization = HttpStreamingClient::Oauth::Adobe.generate_authorization(url, USERNAME, PASSWORD, CLIENTID, CLIENTSECRET)
15
+ puts "#{TOKENAPIHOST}:access token: #{authorization}"
16
+ client = HttpStreamingClient::Client.new(compression: false)
17
+
18
+ NUM_RECORDS_PER_BATCH = 1000
19
+ MAX_RECORDS = 50000
20
+
21
+ count = 0
22
+ totalSize = 0
23
+ intervalSize = 0
24
+ startTime = nil
25
+ lastTime = nil
26
+
27
+ puts "starting performance test run: #{Time.new.to_s}"
28
+
29
+ startTime = lastTime = Time.new
30
+
31
+ response = client.get(STREAMURL, {:headers => {'Authorization' => "Bearer #{authorization}" }}) { |line|
32
+
33
+ if line.nil? then
34
+ puts "error:nil line received"
35
+ next
36
+ end
37
+
38
+ if line.size == 0 then
39
+ puts "error:zero length line received"
40
+ next
41
+ end
42
+
43
+ if line.eql? "\r\n" then
44
+ puts "Server ping received"
45
+ next
46
+ end
47
+
48
+ count = count + 1
49
+ intervalSize = intervalSize + line.size
50
+
51
+ if count % NUM_RECORDS_PER_BATCH == 0 then
52
+
53
+ now = Time.new
54
+ intervalElapsedTime = now - lastTime
55
+ totalElapsedTime = now - startTime
56
+
57
+ totalSize = totalSize + intervalSize
58
+
59
+ stats = Hash.new
60
+
61
+ stats['total_records_received'] = count.to_s
62
+ stats['total_elapsed_time_sec'] = totalElapsedTime.round(2).to_s
63
+ stats['total_records_per_sec'] = (count / totalElapsedTime).round(2).to_s
64
+ stats['total_kbytes_per_sec'] = (totalSize / totalElapsedTime / 1024).round(2).to_s
65
+
66
+ stats['interval_records_received'] = NUM_RECORDS_PER_BATCH
67
+ stats['interval_elapsed_time_sec'] = intervalElapsedTime.round(2).to_s
68
+ stats['interval_records_per_sec'] = (NUM_RECORDS_PER_BATCH / intervalElapsedTime).round(2).to_s
69
+ stats['interval_kbytes_per_sec'] = (intervalSize / intervalElapsedTime / 1024).round(2).to_s
70
+
71
+ puts stats.to_json
72
+
73
+ lastTime = now
74
+ intervalSize = 0
75
+ end
76
+
77
+ if count == MAX_RECORDS then
78
+ puts "finished performance test run: #{Time.new.to_s}"
79
+ puts "total elapsed time: #{(Time.new - startTime).round(2).to_s} seconds"
80
+ exit 0
81
+ end
82
+
83
+ }
@@ -0,0 +1,31 @@
1
+ require 'bundler/setup'
2
+ require 'logger'
3
+
4
+ require 'http/oauth'
5
+ require 'http/streaming/client'
6
+ require 'http/custom_logger'
7
+
8
+ include Http::Oauth
9
+ include Http::Streaming::Client
10
+ include Http::CustomLogger
11
+
12
+ logger.console = true
13
+ logger.level = Logger::DEBUG
14
+ logger.logfile = false
15
+
16
+ if ARGV.size != 7 then
17
+ puts "Usage generate_twitter_authorization_header.rb method url params_string consumer_key consumer_secret oauth_token oauth_token_secret\n"
18
+ exit(1)
19
+ end
20
+
21
+ method = ARGV[0]
22
+ url = ARGV[1]
23
+ params_hash = Hash.new
24
+ ARGV[2].split("&").each { |p| params_hash[p.split("=")[0]] = p.split("=")[1] } unless ARGV[2].empty?
25
+ oauth_consumer_key = ARGV[3]
26
+ oauth_consumer_secret = ARGV[4]
27
+ oauth_token = ARGV[5]
28
+ oauth_token_secret = ARGV[6]
29
+
30
+ authorization = generate_twitter_authorization(url, method, params_hash, oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret)
31
+ puts "Authorization: #{authorization}"
@@ -0,0 +1,26 @@
1
+ require 'bundler/setup'
2
+ require 'logger'
3
+ require 'base64'
4
+
5
+ require 'http/streaming/client'
6
+ require 'http/custom_logger'
7
+
8
+ include Http::Streaming::Client
9
+ include Http::CustomLogger
10
+
11
+ logger.console = true
12
+ logger.level = Logger::DEBUG
13
+ logger.logfile = false
14
+
15
+ if ARGV.size != 2 then
16
+ puts "Usage generate_twitter_bearer_token.rb app_key app_secret\n"
17
+ exit(1)
18
+ end
19
+
20
+ key = URI::encode(ARGV[0])
21
+ secret = URI::encode(ARGV[1])
22
+ encoded = Base64.strict_encode64("#{key}:#{secret}")
23
+
24
+ # API spec states that grant_type should be in the post body...but api.twitter.com seems to see it only if it is a URL parameter...
25
+ response = post("https://api.twitter.com/oauth2/token?grant_type=client_credentials", "grant_type=client_credentials", headers: {'Authorization' => "Basic #{encoded}" })
26
+ puts response
@@ -0,0 +1,17 @@
1
+ require 'bundler/setup'
2
+ require 'logger'
3
+ require 'json'
4
+
5
+ require 'http_streaming_client'
6
+
7
+ require 'http_streaming_client/credentials/twitter'
8
+ include HttpStreamingClient::Credentials::Twitter
9
+
10
+ HttpStreamingClient.logger.console = false
11
+ HttpStreamingClient.logger.logfile = false
12
+
13
+ url = "https://stream.twitter.com/1.1/statuses/sample.json"
14
+ authorization = HttpStreamingClient::Oauth::Twitter.generate_authorization(url, "get", {}, OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
15
+ puts "authorization: #{authorization}"
16
+ client = HttpStreamingClient::Client.new(compression: true)
17
+ response = client.get(url, {:headers => {'Authorization' => "#{authorization}" }}) { |line| puts "#{JSON.parse(line).to_s}/n" }
@@ -0,0 +1,78 @@
1
+ require 'bundler/setup'
2
+ require 'logger'
3
+ require 'json'
4
+
5
+ require 'http_streaming_client'
6
+
7
+ require 'http_streaming_client/credentials/twitter'
8
+ include HttpStreamingClient::Credentials::Twitter
9
+
10
+ HttpStreamingClient.logger.console = false
11
+ HttpStreamingClient.logger.logfile = false
12
+
13
+ url = "https://stream.twitter.com/1.1/statuses/sample.json"
14
+ authorization = HttpStreamingClient::Oauth::Twitter.generate_authorization(url, "get", {}, OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
15
+ puts "authorization: #{authorization}"
16
+
17
+ NUM_RECORDS_PER_BATCH = 1000
18
+ MAX_RECORDS = 50000
19
+
20
+ count = 0
21
+ totalSize = 0
22
+ intervalSize = 0
23
+ startTime = nil
24
+ lastTime = nil
25
+
26
+ puts "starting performance test run: #{Time.new.to_s}"
27
+
28
+ startTime = lastTime = Time.new
29
+
30
+ client = HttpStreamingClient::Client.new(compression: true)
31
+ response = client.get(url, {:headers => {'Authorization' => "#{authorization}" }}) { |line|
32
+
33
+ if line.nil? then
34
+ puts "error:nil line received"
35
+ next
36
+ end
37
+
38
+ if line.size == 0 then
39
+ puts "error:zero length line received"
40
+ next
41
+ end
42
+
43
+ count = count + 1
44
+ intervalSize = intervalSize + line.size
45
+
46
+ if count % NUM_RECORDS_PER_BATCH == 0 then
47
+
48
+ now = Time.new
49
+ intervalElapsedTime = now - lastTime
50
+ totalElapsedTime = now - startTime
51
+
52
+ totalSize = totalSize + intervalSize
53
+
54
+ stats = Hash.new
55
+
56
+ stats['total_records_received'] = count.to_s
57
+ stats['total_elapsed_time_sec'] = totalElapsedTime.round(2).to_s
58
+ stats['total_records_per_sec'] = (count / totalElapsedTime).round(2).to_s
59
+ stats['total_kbytes_per_sec'] = (totalSize / totalElapsedTime / 1024).round(2).to_s
60
+
61
+ stats['interval_records_received'] = NUM_RECORDS_PER_BATCH
62
+ stats['interval_elapsed_time_sec'] = intervalElapsedTime.round(2).to_s
63
+ stats['interval_records_per_sec'] = (NUM_RECORDS_PER_BATCH / intervalElapsedTime).round(2).to_s
64
+ stats['interval_kbytes_per_sec'] = (intervalSize / intervalElapsedTime / 1024).round(2).to_s
65
+
66
+ puts stats.to_json
67
+
68
+ lastTime = now
69
+ intervalSize = 0
70
+ end
71
+
72
+ if count == MAX_RECORDS then
73
+ puts "finished performance test run: #{Time.new.to_s}"
74
+ puts "total elapsed time: #{(Time.new - startTime).round(2).to_s} seconds"
75
+ exit 0
76
+ end
77
+
78
+ }
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_streaming_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.1
5
+ platform: ruby
6
+ authors:
7
+ - David Tompkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Ruby HTTP client with streaming support for GZIP compressed streams and
98
+ chunked transfer encoding. Also includes extensible OAuth support for Adobe and
99
+ Twitter streaming APIs.
100
+ email:
101
+ - tompkins@adobe.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - AUTHORS
108
+ - CHANGELOG
109
+ - Gemfile
110
+ - LICENSE
111
+ - README.md
112
+ - Rakefile
113
+ - http_streaming_client.gemspec
114
+ - lib/http_streaming_client.rb
115
+ - lib/http_streaming_client/client.rb
116
+ - lib/http_streaming_client/credentials/.gitignore
117
+ - lib/http_streaming_client/credentials/adobe.rb.sample
118
+ - lib/http_streaming_client/credentials/twitter.rb.sample
119
+ - lib/http_streaming_client/custom_logger.rb
120
+ - lib/http_streaming_client/decoders/gzip.rb
121
+ - lib/http_streaming_client/errors.rb
122
+ - lib/http_streaming_client/oauth.rb
123
+ - lib/http_streaming_client/oauth/adobe.rb
124
+ - lib/http_streaming_client/oauth/base.rb
125
+ - lib/http_streaming_client/oauth/twitter.rb
126
+ - lib/http_streaming_client/railtie.rb
127
+ - lib/http_streaming_client/version.rb
128
+ - spec/adobe_spec.rb
129
+ - spec/client_spec.rb
130
+ - spec/oauth_spec.rb
131
+ - spec/spec_helper.rb
132
+ - spec/twitter_spec.rb
133
+ - tools/adobe_firehose.rb
134
+ - tools/adobe_firehose_performance_test.rb
135
+ - tools/generate_twitter_authorization_header.rb
136
+ - tools/generate_twitter_bearer_token.rb
137
+ - tools/twitter_firehose.rb
138
+ - tools/twitter_firehose_performance_test.rb
139
+ homepage: https://git.corp.adobe.com/tompkins/http_streaming_client
140
+ licenses:
141
+ - Apache 2.0
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - '>='
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.0.3
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: a streaming HTTP protocol client
163
+ test_files:
164
+ - spec/adobe_spec.rb
165
+ - spec/client_spec.rb
166
+ - spec/oauth_spec.rb
167
+ - spec/spec_helper.rb
168
+ - spec/twitter_spec.rb