Created
July 14, 2010 22:17
-
-
Save rjsvaljean/476188 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'ffi-ncurses' | |
| require 'twitter' | |
| require 'yaml' | |
| require 'singleton' | |
| module WorkingTitle | |
| class AuthStore | |
| include Singleton | |
| attr_reader :data | |
| def initialize | |
| @file= 'pin.yml' | |
| @data= YAML::load_file(@file) | |
| rescue | |
| File.open(@file, 'w') {|f| f.write('')} | |
| @data= {} | |
| end | |
| def method_missing(method, *val) | |
| method = method.to_s | |
| if method.match(/=/) | |
| @data[method.gsub('=','')]= val[0] | |
| File.open(@file, 'w') {|f| f.write(@data.to_yaml)} | |
| true | |
| else | |
| return @data[method] | |
| end | |
| end | |
| end | |
| class Client | |
| attr_reader :auth_url | |
| def initialize | |
| @oauth= Twitter::OAuth.new('OCr3t8IV5pTjF5FGApfPow', 'QJOjtQpoordkSe0dLveRSArrgeRO2P1feGlvurGe0') | |
| @auth_data= AuthStore.instance | |
| end | |
| def authorize | |
| if @auth_data.atoken && @auth_data.asecret | |
| @oauth.authorize_from_access(@auth_data.atoken, @auth_data.asecret) | |
| true | |
| elsif @auth_data.pin && @auth_data.rtoken && @auth_data.rsecret | |
| @oauth.authorize_from_request(@auth_data.rtoken, @auth_data.rsecret, @auth_data.pin) | |
| @auth_data.atoken= @oauth.access_token.token | |
| @auth_data.asecret= @oauth.access_token.secret | |
| true | |
| else | |
| @auth_data.rtoken= @oauth.request_token.token | |
| @auth_data.rsecret= @oauth.request_token.secret | |
| @auth_url= @oauth.request_token.authorize_url | |
| false | |
| end | |
| end | |
| def client | |
| @client ||= Twitter::Base.new(@oauth) | |
| end | |
| end | |
| class NcursesClient | |
| include FFI::NCurses | |
| def initialize | |
| @client= WorkingTitle::Client.new | |
| end | |
| def run | |
| begin | |
| stdscr = initscr | |
| clear | |
| curs_set 1 | |
| unless @client.authorize | |
| get_pin | |
| end | |
| draw_output_win(@client.client.friends_timeline.collect{|i| i.text}.join("\n")) | |
| draw_input_win | |
| ensure | |
| endwin | |
| end | |
| end | |
| private | |
| def draw_output_win(content) | |
| win = newwin(30, 80, 0, 0) | |
| wborder(win, 0, 0, 0, 0, 0, 0, 0, 0) | |
| waddstr(win, content) | |
| wrefresh(win) | |
| delwin(win) | |
| end | |
| def draw_input_win | |
| win = newwin(2, 80, 33, 0) | |
| wborder(win, 0, 0, 1, 0, 0, 0, 0, 0) | |
| wrefresh(win) | |
| input= '' | |
| while true | |
| while !(char= wgetch(win).chr).match("\n") | |
| input+= char | |
| end | |
| @client.client.update(input) | |
| wclear(win) | |
| end | |
| delwin(win) | |
| end | |
| def get_pin | |
| clear | |
| addstr("Go to #{@client.auth_url}. And enter the PIN: ") | |
| pin = '' | |
| while (char= getch.chr).match(/[0-9]/) | |
| pin += char | |
| end | |
| WorkingTitle::AuthStore.instance.pin= pin | |
| @client.authorize | |
| end | |
| end | |
| end | |
| WorkingTitle::NcursesClient.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment