Permalink
Browse files

implementation

  • Loading branch information...
1 parent 9f2e5a5 commit f6762aa50303f56bb6ee0583825aadbfd4721649 @maccman committed Jan 28, 2012
Showing with 149 additions and 245 deletions.
  1. +3 −0 .gitignore
  2. +4 −0 Gemfile
  3. +5 −0 Gemfile.lock
  4. +1 −0 Procfile
  5. +1 −0 app/models/note.rb
  6. +24 −0 app/models/user.rb
  7. +1 −1 config/application.rb
  8. +4 −1 config/initializers/omniauth.rb
  9. +3 −2 config/routes.rb
  10. +32 −0 db/schema.rb
  11. +71 −0 lib/google.rb
  12. +0 −241 public/index.html
View
@@ -13,3 +13,6 @@
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
+
+.env
+.DS_Store
View
@@ -17,6 +17,10 @@ gem 'omniauth-twitter'
gem 'omniauth-google'
gem 'omniauth-google-oauth2'
+gem 'oauth'
+gem 'roauth'
+gem 'net-http-spy'
+
gem 'nestful'
gem 'macgap'
View
@@ -91,6 +91,7 @@ GEM
multipart-post (1.1.4)
nestful (0.0.8)
activesupport (>= 3.0.0.beta)
+ net-http-spy (0.2.1)
oauth (0.4.5)
oauth2 (0.5.2)
faraday (~> 0.7)
@@ -142,6 +143,7 @@ GEM
json (~> 1.4)
rest-client (1.6.7)
mime-types (>= 1.16)
+ roauth (0.0.8)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
@@ -188,12 +190,15 @@ DEPENDENCIES
jquery-rails
macgap
nestful
+ net-http-spy
+ oauth
omniauth
omniauth-google
omniauth-google-oauth2
omniauth-twitter
pg
rails (= 3.2.1)
+ roauth
ruby-debug19
spine-rails!
sqlite3
View
@@ -0,0 +1 @@
+web: bundle exec thin -R config.ru start -p $PORT -e ${RACK_ENV:-development}
View
@@ -1,2 +1,3 @@
class Note < ActiveRecord::Base
+ belongs_to :user
end
View
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ has_one :note
+
class << self
def authorize_google!(auth)
return unless auth && auth.uid
@@ -9,4 +11,26 @@ def authorize_google!(auth)
user
end
end
+
+ def calendar
+ google.calendar
+ end
+
+ def email
+
+ end
+
+ def serializable_hash(options = {})
+ super(options.merge(
+ :methods => [
+ :calendar, :email
+ ],
+ :include => :note
+ ))
+ end
+
+ protected
+ def google
+ Google::Client.new(google_token)
+ end
end
View
@@ -16,7 +16,7 @@ class Application < Rails::Application
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
- # config.autoload_paths += %W(#{config.root}/extras)
+ config.autoload_paths += %W(#{config.root}/lib)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
@@ -1,3 +1,6 @@
Rails.application.config.middleware.use OmniAuth::Builder do
- provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"], scope: "userinfo.email,userinfo.profile,https://www.google.com/m8/feeds"
+ provider :google_oauth2, ENV["GOOGLE_KEY"], ENV["GOOGLE_SECRET"], scope: "userinfo.email,userinfo.profile,https://www.googleapis.com/auth/calendar.readonly,https://mail.google.com/mail/feed/atom/"
+ on_failure do |env, msg|
+ [200, {}, [env["omniauth.error"].inspect + ' ' + msg.to_s]]
+ end
end
View
@@ -1,11 +1,12 @@
Dashboard::Application.routes.draw do
match '/auth/google' => redirect('/auth/google_oauth2')
- match '/auth/google_oauth2/callback' => 'authorize#google'
+ match '/auth/google_oauth2/callback' => 'authorize#create'
match '/auth/failure' => 'authorize#failure'
match '/logout' => 'authorize#destroy', :as => :logout
match '/authorize' => redirect('/auth/google'), :as => :authorize
+ root :to => 'app#index'
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -63,4 +64,4 @@
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
-end
+end
View
@@ -0,0 +1,32 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20120128032536) do
+
+ create_table "notes", :force => true do |t|
+ t.text "body"
+ t.integer "user_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ create_table "users", :force => true do |t|
+ t.string "name"
+ t.string "email"
+ t.string "uid"
+ t.string "google_token"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+end
View
@@ -0,0 +1,71 @@
+require 'nestful'
+require 'nestful/oauth'
+
+module Google
+ class Email
+ def initialize(result)
+ @subject = result['title']
+ @summary = result['summary']
+ @link = result['link']
+ end
+
+ def serializable_hash(options = nil)
+ {
+ subject: @subject,
+ subject: @subject,
+ link: @link
+ }
+ end
+ end
+
+ class Event
+ def initialize(result)
+ @link = result['htmlLink']
+ @name = result['summary']
+ @start_at = result['start']['date']
+ @end_at = result['end']['date']
+ end
+
+ def serializable_hash(options = nil)
+ {
+ link: @link,
+ name: @name,
+ start_at: @start_at,
+ end_at: @end_at
+ }
+ end
+ end
+
+ class Client
+ attr_reader :token, :options
+
+ def initialize(token, options = {})
+ @token = token
+ @options = options
+ end
+
+ def email
+ result = Nestful.get(
+ "https://mail.google.com/mail/feed/atom/",
+ oauth: {access_key: ENV["GOOGLE_SECRET"], access_secret: token}
+ )
+ result = Hash.from_xml(result)
+ result["entry"]#.map {|e| Email.new(e) }
+ end
+
+ def calendar
+ result = Nestful.get(
+ "https://www.googleapis.com/calendar/v3/calendars/primary/events",
+ params: {
+ access_token: token,
+ maxResults: 100000,
+ orderBy: "updated",
+ timeMin: Time.current.beginning_of_day.xmlschema,
+ timeMax: Time.current.end_of_day.xmlschema
+ }
+ )
+ result = ActiveSupport::JSON.decode(result)
+ result["items"].map {|i| Event.new(i) }
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit f6762aa

Please sign in to comment.