class Gyunyu::Token

Constants

API_KEY
SHARED_SECRET

Public Class Methods

get() click to toggle source
# File lib/gyunyu/token.rb, line 14
def self.get
  @self = self.new
  token = @self.load

  if ( !token )
    token = @self.request
    @self.save( token )
  end

  RTM::API.token = token
  token
end
new() click to toggle source
# File lib/gyunyu/token.rb, line 10
def initialize
  RTM::API.init( API_KEY, SHARED_SECRET )
end
purge() click to toggle source
# File lib/gyunyu/token.rb, line 27
def self.purge
  RTM::API.token = nil
  self.new.instance_eval {
    while ( File.exist?( magazine ) )
      rm( magazine )
    end
  }
end

Public Instance Methods

dotfile() click to toggle source
# File lib/gyunyu/token.rb, line 73
def dotfile
  File.join( ENV['HOME'], '.gyunyu' )
end
env() click to toggle source
# File lib/gyunyu/token.rb, line 69
def env
  ENV['RTMSTAT_TOKEN']
end
load() click to toggle source
# File lib/gyunyu/token.rb, line 50
def load
  token = nil

  if ( File.exist?( magazine ) )
    open( magazine, 'r+b' ) { |f|
      # TODO WARN if permission is too loose
      f.flock( File::LOCK_SH )
      token = f.read
      f.flock( File::LOCK_UN )
    }
  end

  token
end
magazine() click to toggle source
# File lib/gyunyu/token.rb, line 65
def magazine
  env || dotfile
end
request() click to toggle source
# File lib/gyunyu/token.rb, line 77
def request
  frob = RTM::Auth::GetFrob.new.invoke
  url  = RTM::API.get_auth_url('read', frob)
  puts "open RTM with browser and accept API KEY."
  puts "and hit enter."
  system("open '#{url}'")
  STDIN.gets

  res = RTM::Auth::GetToken.new(frob).invoke
  res[:token]
end
save( token ) click to toggle source
# File lib/gyunyu/token.rb, line 36
def save( token )
  if ( !File.exist?( magazine ) )
    open( magazine, 'w', 0600 ) { }
  end

  open( magazine, 'r+b', 0600 ) { |f|
    f.flock( File::LOCK_EX )
    f.truncate(0)
    f.print token
    f.flock( File::LOCK_UN )
  }
  token
end