Permalink
Browse files

Initial commit to earthquake.

  • Loading branch information...
0 parents commit be61c21dad18d46999e0b84347468676f76fb6a7 @jugyo jugyo committed Mar 16, 2011
Showing with 330 additions and 0 deletions.
  1. +5 −0 .document
  2. +1 −0 .rspec
  3. +21 −0 Gemfile
  4. +49 −0 Gemfile.lock
  5. +20 −0 LICENSE.txt
  6. +9 −0 README.md
  7. +45 −0 Rakefile
  8. +4 −0 bin/earthquake
  9. +19 −0 lib/earthquake.rb
  10. +138 −0 lib/earthquake/core.rb
  11. +7 −0 spec/earthquake_spec.rb
  12. +12 −0 spec/spec_helper.rb
@@ -0,0 +1,5 @@
+lib/**/*.rb
+bin/*
+-
+features/**/*.feature
+LICENSE.txt
1 .rspec
@@ -0,0 +1 @@
+--color
21 Gemfile
@@ -0,0 +1,21 @@
+source "http://rubygems.org"
+# Add dependencies required to use your gem here.
+# Example:
+# gem "activesupport", ">= 2.3.5"
+
+gem 'twitter-stream', :require => 'twitter/json_stream'
+gem 'notify'
+gem 'i18n'
+gem 'activesupport', :require => 'active_support/core_ext'
+gem 'awesome_print', :require => 'ap'
+gem 'launchy'
+gem 'oauth'
+
+# Add dependencies to develop your gem here.
+# Include everything needed to run rake, tests, features, etc.
+group :development do
+ gem "rspec", "~> 2.3.0"
+ gem "bundler", "~> 1.0.0"
+ gem "jeweler", "~> 1.5.2"
+ gem "rcov", ">= 0"
+end
@@ -0,0 +1,49 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.0.5)
+ awesome_print (0.3.2)
+ configuration (1.2.0)
+ diff-lcs (1.1.2)
+ eventmachine (0.12.10)
+ git (1.2.5)
+ i18n (0.5.0)
+ jeweler (1.5.2)
+ bundler (~> 1.0.0)
+ git (>= 1.2.5)
+ rake
+ launchy (0.4.0)
+ configuration (>= 0.0.5)
+ rake (>= 0.8.1)
+ notify (0.3.0)
+ oauth (0.4.4)
+ rake (0.8.7)
+ rcov (0.9.9)
+ roauth (0.0.3)
+ rspec (2.3.0)
+ rspec-core (~> 2.3.0)
+ rspec-expectations (~> 2.3.0)
+ rspec-mocks (~> 2.3.0)
+ rspec-core (2.3.1)
+ rspec-expectations (2.3.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.3.0)
+ twitter-stream (0.1.12)
+ eventmachine (>= 0.12.8)
+ roauth (>= 0.0.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activesupport
+ awesome_print
+ bundler (~> 1.0.0)
+ i18n
+ jeweler (~> 1.5.2)
+ launchy
+ notify
+ oauth
+ rcov
+ rspec (~> 2.3.0)
+ twitter-stream
@@ -0,0 +1,20 @@
+Copyright (c) 2011 jugyo
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
+Earthquake
+====
+
+Twitter Client on Terminal with Twitter Streaming API.
+
+Copyright
+----
+
+Copyright (c) 2011 jugyo. See LICENSE.txt for further details.
@@ -0,0 +1,45 @@
+require 'rubygems'
+require 'bundler'
+begin
+ Bundler.setup(:default, :development)
+rescue Bundler::BundlerError => e
+ $stderr.puts e.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit e.status_code
+end
+require 'rake'
+
+require 'jeweler'
+Jeweler::Tasks.new do |gem|
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
+ gem.name = "earthquake"
+ gem.homepage = "http://github.com/jugyo/earthquake"
+ gem.license = "MIT"
+ gem.summary = %Q{Twitter Client on Terminal.}
+ gem.description = %Q{Twitter Client on Terminal with Twitter Streaming API.}
+ gem.email = "jugyo.org@gmail.com"
+ gem.authors = ["jugyo"]
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
+end
+Jeweler::RubygemsDotOrgTasks.new
+
+require 'rspec/core'
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = FileList['spec/**/*_spec.rb']
+end
+
+task :default => :spec
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "earthquake #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+$:.unshift(File.expand_path('../../lib', __FILE__))
+require 'earthquake'
+Earthquake.start(ARGV)
@@ -0,0 +1,19 @@
+require 'json'
+require 'thread'
+require 'readline'
+require 'bundler/setup'
+Bundler.require :default
+
+$:.unshift File.expand_path('..', __FILE__)
+require 'earthquake/core'
+
+# TODO: command completion
+# TODO: command system
+# TODO: filter system
+# TODO: colorize
+# TODO: setup Twitter client to post and etc
+# TODO: reconnect
+
+module Earthquake
+ extend Earthquake::Core
+end
@@ -0,0 +1,138 @@
+module Earthquake
+ module Core
+ attr_accessor :config
+ attr_reader :item_queue
+
+ def init(*argv)
+ # TODO: parse argv
+ self.config = {
+ :dir => File.expand_path('~/.earthquake'),
+ :consumer_key => 'qOdgatiUm6HIRcdoGVqaZg',
+ :consumer_secret => 'DHcL0bmS02vjSMHMrbFxCQqbDxh8yJZuLuzKviyFMo'
+ }
+ config[:file] ||= File.join(config[:dir], 'config')
+ load config[:file]
+
+ get_access_token unless self.config[:access_key] && self.config[:access_secret]
+ end
+
+ def get_access_token
+ consumer = OAuth::Consumer.new(
+ self.config[:consumer_key],
+ self.config[:consumer_secret],
+ :site => 'http://api.twitter.com'
+ )
+ request_token = consumer.get_request_token
+
+ puts "1) open: #{request_token.authorize_url}"
+ Launchy::Browser.run(request_token.authorize_url)
+
+ print "2) Enter the PIN: "
+ pin = gets.strip
+
+ access_token = request_token.get_access_token(:oauth_verifier => pin)
+ config[:access_key] = access_token.token
+ config[:access_secret] = access_token.secret
+
+ puts "Saving 'access_key' and 'access_secret' to '#{config[:file]}'"
+ File.open(config[:file], 'a') do |f|
+ f << "\n"
+ f << "Earthquake.config[:access_key] = '#{config[:access_key]}'"
+ f << "\n"
+ f << "Earthquake.config[:access_secret] = '#{config[:access_secret]}'"
+ end
+ end
+
+ def start(*argv)
+ init(*argv)
+
+ @item_queue = []
+
+ Thread.abort_on_exception = true
+
+ Thread.start do
+ while buf = Readline.readline("[earthquake] ", true)
+ begin
+ buf = buf.strip
+
+ if buf == '/exit'
+ # TODO: refactor
+ @stream.stop if @stream
+ EventMachine.stop if EventMachine.reactor_running?
+ end
+ rescue => e
+ notify "[ERROR] #{e}"
+ end
+ end
+ end
+
+ Thread.start do
+ loop do
+ # TODO: handle the response that include friends
+ if Readline.line_buffer.empty?
+ while item = item_queue.shift
+ begin
+ insert "#{item["user"]["screen_name"]}: #{item["text"]}"
+ rescue => e
+ insert do
+ ap item
+ end
+ # notify "[ERROR] #{e}"
+ end
+ end
+ end
+ sleep 1
+ end
+ end
+
+ EventMachine::run {
+ @stream = Twitter::JSONStream.connect(
+ :ssl => true,
+ :host => 'userstream.twitter.com',
+ :path => '/2/user.json',
+ :oauth => config.slice(:consumer_key, :consumer_secret, :access_key, :access_secret)
+ )
+
+ @stream.each_item do |item|
+ item_queue << JSON.parse(item)
+ end
+
+ @stream.on_error do |message|
+ $stdout.print "error: #{message}\n"
+ $stdout.flush
+ end
+
+ @stream.on_reconnect do |timeout, retries|
+ $stdout.print "reconnecting in: #{timeout} seconds\n"
+ $stdout.flush
+ end
+
+ @stream.on_max_reconnects do |timeout, retries|
+ $stdout.print "Failed after #{retries} failed reconnects\n"
+ $stdout.flush
+ end
+
+ trap('TERM') {
+ @stream.stop
+ EventMachine.stop if EventMachine.reactor_running?
+ }
+ }
+ end
+
+ def notify(message)
+ Notify.notify 'earthquake', message
+ end
+
+ def insert(*messages)
+ clear_line
+ puts messages unless messages.empty?
+ yield if block_given?
+ ensure
+ Readline.refresh_line
+ end
+
+ def clear_line
+ print "\e[0G" + "\e[K"
+ end
+ end
+end
@@ -0,0 +1,7 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "Earthquake" do
+ it "fails" do
+ fail "hey buddy, you should probably rename this file and start specing for real"
+ end
+end
@@ -0,0 +1,12 @@
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'rspec'
+require 'earthquake'
+
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+RSpec.configure do |config|
+
+end

0 comments on commit be61c21

Please sign in to comment.