Class: George::OAuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/george/oauth_client.rb

Constant Summary collapse

DEFAULT_COMMANDS =
{
  /(mingw|mswin|windows|cygwin)/i => ['cmd', '/C', 'start', '/b'],
  /(darwin|mac os)/i              => ['open'],
  /(linux|bsd|aix|solaris)/i      => ['xdg-open']
}
GIF_PATH =
File.join(TEMPLATE_PATH, 'thumbsup.gif')
RESPONSE_BODY =
"<!doctype html>\n<html>\n  <head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n<title>Tea is on its way!</title>\n<style type=\"text/css\">\n  body {\n    background: #fff;\n    color: #666;\n    font: 18px/1.5 FreeSans, Helvetica, Arial, sans-serif;\n  }\n\n  section {\n    width: 400px;\n    margin: 40px auto;\n  }\n\n  img {\n    display: block;\n    width: 360px;\n    border: 10px solid #eee;\n  }\n\n  h1 {\n    font-weight: normal;\n    font-size: 2.4em;\n    margin: 0.6em 0 0.4em;\n  }\n</style>\n  </head>\n  <body>\n\n<section>\n  <img alt=\"Thumbs up!\" src=\"data:image/gif;base64,\#{ Base64.encode64(File.read(GIF_PATH)).gsub(/[\\s\\n\\r\\t]/, '') }\">\n\n  <h1>Tea is on its way!</h1>\n\n  <p>You&rsquo;re authorized to post to Twitter. Close this window and\n    return to your terminal.</p>\n</section>\n\n  </body>\n</html>\n"

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ OAuthClient



63
64
65
66
67
68
69
70
# File 'lib/george/oauth_client.rb', line 63

def initialize(config)
  @config = config
  @client = OAuth::Consumer.new(
              config.consumer_key,
              config.consumer_secret, 
              :site => config.provider_site
            )
end

Instance Method Details

#authorize_urlObject



81
82
83
# File 'lib/george/oauth_client.rb', line 81

def authorize_url
  @request_token.authorize_url(:oauth_callback => @config.callback_url)
end

#bootObject



72
73
74
75
76
77
78
79
# File 'lib/george/oauth_client.rb', line 72

def boot
  @server_thread = Thread.new do
    handler = Rack::Handler.get('webrick')
    handler.run(self, :Port => DEFAULT_PORT,
                      :AccessLog => [], 
                      :Logger => WEBrick::Log::new(nil, 0))
  end
end

#call(env) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/george/oauth_client.rb', line 101

def call(env)
  return [404, {}, []] unless env['PATH_INFO'] == CALLBACK_PATH

  params = Rack::Request.new(env).params
  @access_token = @request_token.get_access_token(:oauth_verifier => params['oauth_verifier'])

  @oauth_credentials = {
    'token'  => @access_token.token,
    'secret' => @access_token.secret
  }

  [200, {'Content-Type' => 'text/html'}, [RESPONSE_BODY]]
end

#get_oauth_credentialsObject



85
86
87
88
89
90
# File 'lib/george/oauth_client.rb', line 85

def get_oauth_credentials
  @request_token = @client.get_request_token(:oauth_callback => @config.callback_url)
  launch_browser(authorize_url)
  sleep 1 until @oauth_credentials
  @oauth_credentials
end

#launch_browser(url) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/george/oauth_client.rb', line 92

def launch_browser(url)
  os   = RbConfig::CONFIG['host_os']
  key  = DEFAULT_COMMANDS.keys.find { |key| os =~ key }
  argv = DEFAULT_COMMANDS[key] + [url]

  @browser = ChildProcess.build(*argv)
  @browser.start
end