lib/rufus/rtm/credentials.rb in rufus-rtm-0.1 vs lib/rufus/rtm/credentials.rb in rufus-rtm-0.1.2

- old
+ new

@@ -1,122 +1,103 @@ - -# #-- -# Copyright (c) 2008, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2008-2009, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: -# +# # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # -# (MIT license) +# Made in Japan. #++ -# -# -# John Mettraux -# -# Made in Japan -# -# 2008/02/07 -# module Rufus::RTM - - def self.auth_get_frob #:nodoc: - r = milk :method => "rtm.auth.getFrob" - r["frob"] - end + def self.auth_get_frob #:nodoc: - def self.auth_get_frob_and_url #:nodoc: + r = milk(:method => 'rtm.auth.getFrob') + r['frob'] + end - frob = auth_get_frob + def self.auth_get_frob_and_url #:nodoc: - p = {} - p['api_key'] = API_KEY - p['perms'] = "delete" - p['frob'] = frob - sign p + frob = auth_get_frob - [ - frob, - AUTH_ENDPOINT + "?" + p.collect { |k, v| "#{k}=#{v}" }.join("&") - ] - end + p = {} + p['api_key'] = ENV['RTM_API_KEY'] + p['perms'] = 'delete' + p['frob'] = frob + sign(p, ENV['RTM_SHARED_SECRET']) - def self.auth_get_token (frob) #:nodoc: + [ + frob, + AUTH_ENDPOINT + '?' + p.collect { |k, v| "#{k}=#{v}" }.join("&") + ] + end - begin - milk(:method => "rtm.auth.getToken", :frob => frob)['auth']['token'] - rescue Exception => e - nil - end + def self.auth_get_token (frob) #:nodoc: + + begin + milk(:method => 'rtm.auth.getToken', :frob => frob)['auth']['token'] + rescue Exception => e + nil end + end - # - # ensuring the credentials are present... + # + # ensuring the credentials are present... - API_KEY = ENV['RTM_API_KEY'] - SHARED_SECRET = ENV['RTM_SHARED_SECRET'] + unless ENV['RTM_FROB'] - raise "API_KEY missing from environment, cannot use Rufus::RTM" \ - unless API_KEY - - FROB = ENV['RTM_FROB'] - AUTH_TOKEN = ENV['RTM_AUTH_TOKEN'] + frob, auth_url = auth_get_frob_and_url - unless FROB + puts <<-EOS - frob, auth_url = auth_get_frob_and_url - - puts <<-EOS - please visit this URL with your browser and then hit 'enter' : #{auth_url} - EOS + EOS - STDIN.gets - puts "ok, now getting auth token...\n" + STDIN.gets + puts "ok, now getting auth token...\n" - auth_token = auth_get_token frob + auth_token = auth_get_token frob - if auth_token + if auth_token - puts <<-EOS + puts <<-EOS here are your RTM_FROB and RTM_AUTH_TOKEN, make sure to place them in your environment : export RTM_FROB=#{frob} export RTM_AUTH_TOKEN=#{auth_token} - EOS - else + EOS + else - puts <<-EOS + puts <<-EOS couldn't get auth token, please retry... - EOS - end - - exit 0 + EOS end + + exit 0 + end end