Permalink
Browse files

Logging in to CoTweet and getting a list of channels

  • Loading branch information...
0 parents commit 3e6502d5e9b48aeb75e8bf2500055d4e6b205a91 @ept committed Feb 5, 2012
Showing with 124 additions and 0 deletions.
  1. +5 −0 Gemfile
  2. +28 −0 Gemfile.lock
  3. +18 −0 lib/cotweet-export.rb
  4. +73 −0 lib/cotweet/connection.rb
@@ -0,0 +1,5 @@
+source :rubygems
+gem 'em-http-request'
+gem 'highline'
+gem 'deferrable_gratification'
+gem 'json'
@@ -0,0 +1,28 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.2.6)
+ cookiejar (0.3.0)
+ deferrable_gratification (0.3.1)
+ eventmachine
+ em-http-request (1.0.1)
+ addressable (>= 2.2.3)
+ cookiejar
+ em-socksify
+ eventmachine (>= 1.0.0.beta.4)
+ http_parser.rb (>= 0.5.3)
+ em-socksify (0.1.0)
+ eventmachine
+ eventmachine (1.0.0.beta.4)
+ highline (1.6.11)
+ http_parser.rb (0.5.3)
+ json (1.6.5)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ deferrable_gratification
+ em-http-request
+ highline
+ json
@@ -0,0 +1,18 @@
+require 'rubygems'
+require 'highline'
+require 'json'
+require 'eventmachine'
+require 'deferrable_gratification'
+require 'em-http'
+
+DG.enhance! EM::HttpClient
+
+$stdout.sync = true
+
+%w{
+ connection
+}.each do |filename|
+ require File.join(File.dirname(__FILE__), 'cotweet', filename)
+end
+
+CoTweet::Connection.test!
@@ -0,0 +1,73 @@
+module CoTweet
+ class Connection
+ BASE_URL = URI.parse('https://standard.cotweet.com/')
+
+ def initialize
+ cmd.say 'Please log in to your CoTweet account.'
+ @email_address = cmd.ask('Email address: ')
+ @password = cmd.ask('Password: ') {|q| q.echo = false }
+ @cookiejar = CookieJar::Jar.new
+ end
+
+ # Command-line interface helpers
+ def cmd
+ @cmd ||= HighLine.new
+ end
+
+ # Returns a deferrable that succeeds with self on successful login.
+ def login
+ cmd.say 'Logging in...'
+ request = EM::HttpRequest.new(BASE_URL.merge('/agent_sessions'))
+
+ request.post(:body => {
+ 'agent_session[email_address]' => @email_address,
+ 'agent_session[password]' => @password,
+ 'agent_session[remember_me]' => 0
+ }).transform do |http|
+
+ case http.response_header.status
+ when 200
+ cmd.say 'Incorrect password'; raise 'login failed'
+ when 302
+ [http.response_header.cookie].compact.flatten.each do |cookie|
+ @cookiejar.set_cookie(request.uri, cookie)
+ end
+ else
+ raise "HTTP #{http.response_header.status}"
+ end
+ self
+
+ end.errback do
+ cmd.say 'Sorry, could not connect to CoTweet.'
+ end
+ end
+
+ def cookies
+ @cookiejar.get_cookies(BASE_URL).map(&:to_s).join('; ')
+ end
+
+ def get_json(path, query={})
+ EM::HttpRequest.new(BASE_URL.merge(path)).
+ get(:query => query, :head => {:cookie => cookies}).
+ transform do |http|
+ if http.response_header.status == 200
+ JSON.parse(http.response)
+ else
+ cmd.say "Request to #{path} failed with HTTP #{http.response_header.status}"
+ raise "HTTP #{http.response_header.status}"
+ end
+ end
+ end
+
+ def self.test!
+ EM.run do
+ new.login.bind! do |connection|
+ connection.get_json '/api/1/channels/sources.json'
+ end.bothback do |result|
+ puts result.inspect
+ EM.stop
+ end
+ end
+ end
+ end
+end

0 comments on commit 3e6502d

Please sign in to comment.