Added better handling for errors.
[arvados.git] / examples / buzz.rb
1 $:.unshift('lib')
2 #####!/usr/bin/ruby1.8
3
4 # Copyright:: Copyright 2011 Google Inc.
5 # License:: All Rights Reserved.
6 # Original Author:: Bob Aman
7 # Maintainer:: Daniel Dobson (mailto:wolff@google.com)
8 # Maintainer:: Jenny Murphy (mailto:mimming@google.com)
9
10 require 'rubygems'
11 require 'sinatra'
12 require 'google/api_client'
13 require 'httpadapter/adapters/net_http'
14 require 'pp'
15
16 use Rack::Session::Pool, :expire_after => 86400 # 1 day
17
18 # Configuration
19 # See README for getting API id and secret
20
21 if (ARGV.size < 2)
22   set :oauth_client_id, 'oauth_client_id'
23   set :oauth_client_secret, 'oauth_client_secret'
24
25   if (settings.oauth_client_id == 'oauth_client_id')
26     puts 'See README for getting API id and secret.  Server terminated.'
27     exit(0)
28   end
29 else
30   set :oauth_client_id, ARGV[0]
31   set :oauth_client_secret, ARGV[1]
32 end
33
34 # Configuration that you probably don't have to change
35 set :oauth_scopes, 'https://www.googleapis.com/auth/buzz'
36
37 class TokenPair
38   @refresh_token
39   @access_token
40   @expires_in
41   @issued_at
42
43   def update_token!(object)
44     @refresh_token = object.refresh_token
45     @access_token = object.access_token
46     @expires_in = object.expires_in
47     @issued_at = object.issued_at
48   end
49
50   def to_hash
51     return {
52       :refresh_token => @refresh_token,
53       :access_token => @access_token,
54       :expires_in => @expires_in,
55       :issued_at => Time.at(@issued_at)
56     }
57   end
58 end
59
60 # At the beginning of any request, make sure the OAuth token is available.
61 # If it's not available, kick off the OAuth 2 flow to authorize.
62 before do
63   @client = Google::APIClient.new(
64     :authorization => :oauth_2,
65     :host => 'www.googleapis.com',
66     :http_adapter => HTTPAdapter::NetHTTPAdapter.new
67   )
68
69   @client.authorization.client_id = settings.oauth_client_id
70   @client.authorization.client_secret = settings.oauth_client_secret
71   @client.authorization.scope = settings.oauth_scopes
72   @client.authorization.redirect_uri = to('/oauth2callback')
73   @client.authorization.code = params[:code] if params[:code]
74   if session[:token]
75     # Load the access token here if it's available
76     @client.authorization.update_token!(session[:token].to_hash)
77   end
78
79   @buzz = @client.discovered_api('buzz')
80   unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/
81     redirect to('/oauth2authorize')
82   end
83 end
84
85 # Part of the OAuth flow
86 get '/oauth2authorize' do
87   <<OUT
88 <!DOCTYPE html>
89 <html>
90 <head>
91   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
92   <title>Google Ruby API Buzz Sample</title>
93 </head>
94 <body>
95 <header><h1>Google Ruby API Buzz Sample</h1></header>
96 <div class="box">
97 <a class='login' href='#{@client.authorization.authorization_uri.to_s}'>Connect Me!</a>
98 </div>
99 </body>
100 </html>
101 OUT
102 end
103
104 # Part of the OAuth flow
105 get '/oauth2callback' do
106   @client.authorization.fetch_access_token!
107   unless session[:token]
108     token_pair = TokenPair.new
109     token_pair.update_token!(@client.authorization)
110     # Persist the token here
111     session[:token] = token_pair
112   end
113   redirect to('/')
114 end
115
116 # The method you're probably actually interested in. This one lists a page of your
117 # most recent activities
118 get '/' do
119   response = @client.execute(
120     @buzz.activities.list,
121     'userId' => '@me', 'scope' => '@consumption', 'alt'=> 'json'
122   )
123   status, headers, body = response
124   [status, {'Content-Type' => 'application/json'}, body]
125 end