Class: Gspush::Oauth2
- Inherits:
-
Object
- Object
- Gspush::Oauth2
- Defined in:
- lib/gspush/oauth2.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#expires_at ⇒ Object
Returns the value of attribute expires_at.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
Class Method Summary collapse
Instance Method Summary collapse
- #client ⇒ Object
- #expired? ⇒ Boolean
- #get_access_token ⇒ Object
-
#initialize(file_path = nil) ⇒ Oauth2
constructor
A new instance of Oauth2.
- #load ⇒ Object
- #save ⇒ Object
- #to_hash ⇒ Object
- #token ⇒ Object
Constructor Details
#initialize(file_path = nil) ⇒ Oauth2
Returns a new instance of Oauth2.
9 10 11 12 |
# File 'lib/gspush/oauth2.rb', line 9 def initialize(file_path = nil) @file_path = file_path || "#{ENV["HOME"]}/.gspushrc" @redirect_uri = "urn:ietf:wg:oauth:2.0:oob" end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
6 7 8 |
# File 'lib/gspush/oauth2.rb', line 6 def access_token @access_token end |
#client_id ⇒ Object
Returns the value of attribute client_id.
7 8 9 |
# File 'lib/gspush/oauth2.rb', line 7 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
7 8 9 |
# File 'lib/gspush/oauth2.rb', line 7 def client_secret @client_secret end |
#expires_at ⇒ Object
Returns the value of attribute expires_at.
6 7 8 |
# File 'lib/gspush/oauth2.rb', line 6 def expires_at @expires_at end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
7 8 9 |
# File 'lib/gspush/oauth2.rb', line 7 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
6 7 8 |
# File 'lib/gspush/oauth2.rb', line 6 def refresh_token @refresh_token end |
Class Method Details
.generate ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gspush/oauth2.rb', line 26 def self.generate puts "Input your application information" puts "(see https://code.google.com/apis/console )" oauth2 = new hi = HighLine.new oauth2.client_id = hi.ask("Client ID? > ") oauth2.client_secret = hi.ask("Client Secret? > ") oauth2.get_access_token oauth2.save end |
Instance Method Details
#client ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/gspush/oauth2.rb', line 62 def client OAuth2::Client.new( @client_id, @client_secret, :site => "https://accounts.google.com", :token_url => "/o/oauth2/token", :authorize_url => "/o/oauth2/auth") end |
#expired? ⇒ Boolean
92 93 94 |
# File 'lib/gspush/oauth2.rb', line 92 def expired? @expires_at && Time.now.to_i > @expires_at end |
#get_access_token ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/gspush/oauth2.rb', line 70 def get_access_token auth_url = client.auth_code.( :redirect_uri => @redirect_uri, :scope => ["https://docs.google.com/feeds/", "https://docs.googleusercontent.com/", "https://spreadsheets.google.com/feeds/"].join(" ")) hi = HighLine.new puts "Access in your browser: #{auth_url}" = hi.ask("And please input authorization code: ") auth_token = client.auth_code.get_token( , :redirect_uri => @redirect_uri) @access_token = auth_token.token @refresh_token = auth_token.refresh_token @expires_at = auth_token.expires_at @access_token end |
#load ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gspush/oauth2.rb', line 47 def load if FileTest.exist?(@file_path) o = YAML.load(File.read(@file_path)) @access_token = o[:access_token] @refresh_token = o[:refresh_token] @expires_at = o[:expires_at] @client_id = o[:client_id] @client_secret = o[:client_secret] @redirect_uri = o[:redirect_uri] true else false end end |
#save ⇒ Object
40 41 42 43 44 45 |
# File 'lib/gspush/oauth2.rb', line 40 def save File.open(@file_path, 'w') do |f| f.print to_hash.to_yaml end puts "ok saved in #{@file_path}" end |
#to_hash ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/gspush/oauth2.rb', line 96 def to_hash { :client_id => client_id, :client_secret => client_secret, :redirect_uri => redirect_uri, :access_token => access_token, :refresh_token => refresh_token, :expires_at => expires_at, } end |
#token ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gspush/oauth2.rb', line 14 def token self.load or raise "First you need gspush_generate to create oauth2 configuration" token = OAuth2::AccessToken.from_hash(client, to_hash) if expired? token.refresh! save end token end |