Last active
August 29, 2015 14:05
-
-
Save hackugyo/65d42f64ce802aa7e4bb to your computer and use it in GitHub Desktop.
Google Calendar APIのRubyクライアント
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
| ### /Users/kwatanabe/.gitignore-boilerplates/ruby.gitignore | |
| *.gem | |
| *.rbc | |
| .bundle | |
| .config | |
| coverage | |
| InstalledFiles | |
| lib/bundler/man | |
| pkg | |
| rdoc | |
| spec/reports | |
| test/tmp | |
| test/version_tmp | |
| tmp | |
| # YARD artifacts | |
| .yardoc | |
| _yardoc | |
| doc/ | |
| # bunlder | |
| vendor/ | |
| ### /Users/kwatanabe/.gitignore-boilerplates/Global/osx.gitignore | |
| .DS_Store | |
| .AppleDouble | |
| .LSOverride | |
| Icon | |
| # Thumbnails | |
| ._* | |
| # Files that might appear on external disk | |
| .Spotlight-V100 | |
| .Trashes | |
| ### /Users/kwatanabe/.gitignore-boilerplates/Global/emacs.gitignore | |
| *~ | |
| \#*\# | |
| /.emacs.desktop | |
| /.emacs.desktop.lock | |
| .elc | |
| auto-save-list | |
| tramp | |
| .\#* | |
| # Org-mode | |
| .org-id-locations | |
| *_archive | |
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 'google/api_client' | |
| require 'google/api_client/client_secrets' | |
| require 'google/api_client/auth/file_storage' | |
| require 'sinatra' | |
| require 'logger' | |
| enable :sessions | |
| CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json" | |
| def logger; settings.logger end | |
| def api_client; settings.api_client; end | |
| def calendar_api; settings.calendar; end | |
| def user_credentials | |
| # Build a per-request oauth credential based on token stored in session | |
| # which allows us to use a shared API client. | |
| @authorization ||= ( | |
| auth = api_client.authorization.dup | |
| auth.redirect_uri = to('/oauth2callback') | |
| auth.update_token!(session) | |
| auth | |
| ) | |
| end | |
| configure do | |
| log_file = File.open('calendar.log', 'a+') | |
| log_file.sync = true | |
| logger = Logger.new(log_file) | |
| logger.level = Logger::DEBUG | |
| client = Google::APIClient.new( | |
| :application_name => 'Ruby Calendar sample', | |
| :application_version => '1.0.0') | |
| file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE) | |
| if file_storage.authorization.nil? | |
| client_secrets = Google::APIClient::ClientSecrets.load | |
| client.authorization = client_secrets.to_authorization | |
| client.authorization.scope = 'https://www.googleapis.com/auth/calendar' | |
| else | |
| client.authorization = file_storage.authorization | |
| end | |
| # Since we're saving the API definition to the settings, we're only retrieving | |
| # it once (on server start) and saving it between requests. | |
| # If this is still an issue, you could serialize the object and load it on | |
| # subsequent runs. | |
| calendar = client.discovered_api('calendar', 'v3') | |
| set :logger, logger | |
| set :api_client, client | |
| set :calendar, calendar | |
| end | |
| before do | |
| # Ensure user has authorized the app | |
| unless user_credentials.access_token || request.path_info =~ /\A\/oauth2/ | |
| redirect to('/oauth2authorize') | |
| end | |
| end | |
| after do | |
| # Serialize the access/refresh token to the session and credential store. | |
| session[:access_token] = user_credentials.access_token | |
| session[:refresh_token] = user_credentials.refresh_token | |
| session[:expires_in] = user_credentials.expires_in | |
| session[:issued_at] = user_credentials.issued_at | |
| file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE) | |
| file_storage.write_credentials(user_credentials) | |
| end | |
| get '/oauth2authorize' do | |
| # Request authorization | |
| redirect user_credentials.authorization_uri.to_s, 303 | |
| end | |
| get '/oauth2callback' do | |
| # Exchange token | |
| user_credentials.code = params[:code] if params[:code] | |
| user_credentials.fetch_access_token! | |
| redirect to('/') | |
| end | |
| get '/' do | |
| # Fetch list of events on the user's default calandar | |
| # calendar_id = 'primary' | |
| calendar_id = 'ktju3jonf98solrasjg5tqucbk@group.calendar.google.com' | |
| result = api_client.execute(:api_method => calendar_api.events.list, | |
| :parameters => {'calendarId' => calendar_id, 'timeMin' => DateTime.now, 'orderBy' => 'startTime', 'singleEvents' => 'True'}, | |
| :authorization => user_credentials) | |
| [result.status, {'Content-Type' => 'application/json'}, result.data.to_json] | |
| end |
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
| # A sample Gemfile | |
| source "https://rubygems.org" | |
| # gem "rails" | |
| gem "google-api-client", "0.7.1" | |
| gem "jwt", "1.0.0" | |
| gem "sinatra", "1.4.5" |
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
| GEM | |
| remote: https://rubygems.org/ | |
| specs: | |
| addressable (2.3.6) | |
| autoparse (0.3.3) | |
| addressable (>= 2.3.1) | |
| extlib (>= 0.9.15) | |
| multi_json (>= 1.0.0) | |
| extlib (0.9.16) | |
| faraday (0.9.0) | |
| multipart-post (>= 1.2, < 3) | |
| google-api-client (0.7.1) | |
| addressable (>= 2.3.2) | |
| autoparse (>= 0.3.3) | |
| extlib (>= 0.9.15) | |
| faraday (>= 0.9.0) | |
| jwt (>= 0.1.5) | |
| launchy (>= 2.1.1) | |
| multi_json (>= 1.0.0) | |
| retriable (>= 1.4) | |
| signet (>= 0.5.0) | |
| uuidtools (>= 2.1.0) | |
| jwt (1.0.0) | |
| launchy (2.4.2) | |
| addressable (~> 2.3) | |
| multi_json (1.10.1) | |
| multipart-post (2.0.0) | |
| rack (1.5.2) | |
| rack-protection (1.5.3) | |
| rack | |
| retriable (1.4.1) | |
| signet (0.5.1) | |
| addressable (>= 2.2.3) | |
| faraday (>= 0.9.0.rc5) | |
| jwt (>= 0.1.5) | |
| multi_json (>= 1.0.0) | |
| sinatra (1.4.5) | |
| rack (~> 1.4) | |
| rack-protection (~> 1.4) | |
| tilt (~> 1.3, >= 1.3.4) | |
| tilt (1.4.1) | |
| uuidtools (2.1.5) | |
| PLATFORMS | |
| ruby | |
| DEPENDENCIES | |
| google-api-client (= 0.7.1) | |
| jwt (= 1.0.0) | |
| sinatra (= 1.4.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment