Permalink
Browse files

Merge pull request #3 from eriwen/master

Update to new em-http-request, fixes #1 and #2
  • Loading branch information...
2 parents 489e329 + df92476 commit 0f62753d4f76610b744ebdeb138895b899da51ab @rweald committed Sep 18, 2012
View
@@ -3,3 +3,4 @@
Gemfile.lock
pkg/*
tags
+.idea
View
@@ -0,0 +1,3 @@
+[submodule "vendor/em-http-request"]
+ path = vendor/em-http-request
+ url = git://github.com/eriwen/em-http-request.git
View
@@ -14,12 +14,6 @@ Installing gnip-stream is easy. Simply add the following line to your
gem 'gnip-stream', :git => "https://github.com/rweald/gnip-stream"
```
-##Warning
-This gem currently only supports the old version of the gnip powertrack stream.
-Work is in progress to update this gem to support the new gzipped powertrack stream. However,
-due to limitations in Ruby's gzip support the core is having to be re-written to use
-[curb](https://github.com/taf2/curb) rather than eventmachine
-
##Simple Usage
```ruby
@@ -43,6 +37,7 @@ end
* [Ryan Weald](https://github.com/rweald)
* [Sharethrough Team](https://github.com/sharethrough)
+* [Eric Wendelin](http://eriwen.com)
##License
MIT. See [LICENSE](https://github.com/rweald/gnip-stream/blob/master/LICENSE) file for more details.
View
@@ -8,17 +8,17 @@ Gem::Specification.new do |s|
s.authors = ["Ryan Weald"]
s.email = ["ryan@weald.com"]
s.homepage = "https://github.com/rweald/gnip-stream"
- s.summary = %q{}
+ s.summary = "A library to connect and stream data from the GNIP streaming API"
s.description = %q{}
s.rubyforge_project = "gnip-stream"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
- s.require_paths = ["lib"]
+ s.require_paths = ["lib", "vendor"]
s.add_development_dependency "rspec"
- s.add_dependency "em-http-request", ">= 1.0.0"
+ s.add_dependency "em-http-request", ">= 1.0.3"
end
@@ -1,29 +0,0 @@
-module GnipStream
- class PowertrackAuthentication
- attr_accessor :cookies, :location
- def initialize(url, username, password)
- @url = url
- @username = username
- @password = password
- end
-
- def authenticate
- EM.run do
- http = EM::HttpRequest.new(@url).get(
- :head => {"authorization" => [@username, @password]})
- http.headers { |head| parse_response_header(head) }
- http.callback { EM.stop }
- http.error do
- raise "Gnip Authentication Failed. Reason was #{http.error}"
- end
- end
- end
-
- def parse_response_header(header)
- @location = header["LOCATION"]
- @cookies = (header["SET_COOKIE"].first.split(";")[0..2].join(";"))
- rescue => e
- raise "Gnip Authentication Failed. Reason was #{e.inspect}"
- end
- end
-end
@@ -1,9 +1,7 @@
-require 'gnip-stream/powertrack_authentication'
module GnipStream
class PowertrackClient
def initialize(url, username, password)
- @authentication = PowertrackAuthentication.new(url, username, password)
- @stream = JsonStream.new(url)
+ @stream = JsonStream.new(url, {'authorization' => [username, password], 'accept-encoding' => 'gzip, compressed'})
@error_handler = ErrorReconnect.new(self, :consume)
@connection_close_handler = ErrorReconnect.new(self, :consume)
configure_handlers
@@ -17,15 +15,7 @@ def configure_handlers
def consume(&block)
@client_callback = block if block
@stream.on_message(&@client_callback)
- authenticate
@stream.connect
end
-
- def authenticate
- @authentication.authenticate
- @stream.url = @authentication.location
- @stream.headers = {"cookie" => @authentication.cookies}
- end
-
end
end
@@ -1,37 +0,0 @@
-require 'spec_helper'
-require 'gnip-stream/powertrack_authentication'
-
-describe GnipStream::PowertrackAuthentication do
- subject { GnipStream::PowertrackAuthentication.new("http://example.com",
- "user", "password") }
- describe "#authenticate" do
- let(:fake_request) { double("fake request") }
- it "should make a request to the GNIP API to receive access cookie" do
- EM::HttpRequest.should_receive(:new).with("http://example.com").and_return(fake_request)
- fake_request.should_receive(:get) do |args|
- args[:head].should == {'authorization' => ["user", "password"]}
- EM.stop
- double.as_null_object
- end
- subject.authenticate
- end
- end
-
- describe "#parse_response_header" do
- it "should set the stream url based on 'LOCATION' header" do
- subject.parse_response_header({"SET_COOKIE" => ["session_token=foobar; domain=.gnip.com; path=/;
- expires=Wed, 13-Jul-2011 22:10:10 GMT _base_session=hello; path=/; HttpOnly"]})
- subject.cookies.should == "session_token=foobar; domain=.gnip.com; path=/"
- end
-
- it "should set the url that we can stream data from" do
- subject.parse_response_header("LOCATION" => "http://example.com", "SET_COOKIE" => ["session_token=foobar; domain=.gnip.com; path=/;
- expires=Wed, 13-Jul-2011 22:10:10 GMT _base_session=hello; path=/; HttpOnly"])
- subject.location.should == "http://example.com"
- end
-
- it "should raise an error saying that authentication has failed if it can not parse headers" do
- lambda { subject.parse_response_header("bad header") }.should raise_error(/Authentication Failed/)
- end
- end
-end
@@ -3,46 +3,27 @@
describe GnipStream::PowertrackClient do
let(:fake_stream) { double("GnipStream::JsonStream").as_null_object }
- let(:fake_auth) { double("GnipStream::PowertrackAuthentication").as_null_object }
before do
GnipStream::JsonStream.stub(:new => fake_stream)
- GnipStream::PowertrackAuthentication.stub(:new => fake_auth)
end
subject { GnipStream::PowertrackClient.new("http://example.com", "user", "password") }
describe "#initialize" do
- it "initializes a PowertrackAuthentication instance" do
- GnipStream::PowertrackAuthentication.should_receive(:new)
- subject
- end
-
it "initializes an instance JsonStream" do
- GnipStream::JsonStream.should_receive(:new)
+ GnipStream::JsonStream.should_receive(:new)
subject
end
end
describe "configure_handlers" do
it "sets up the appropriate error and close handlers" do
- fake_stream.should_receive(:on_error).twice
+ fake_stream.should_receive(:on_error).twice
fake_stream.should_receive(:on_connection_close).twice
subject.configure_handlers
end
end
- describe "#authenticate" do
- it "performs the necessary authentication and passes appropriate credentials to stream" do
- fake_auth.should_receive(:authenticate)
- fake_auth.should_receive(:location).and_return("http://example.com")
- fake_auth.should_receive(:cookies).and_return("some cookie")
-
- fake_stream.should_receive(:url=).with("http://example.com")
- fake_stream.should_receive(:headers=)
- subject.authenticate
- end
- end
-
describe "#consume" do
it "setup the client callback" do
fake_stream.should_receive(:on_message)

0 comments on commit 0f62753

Please sign in to comment.