Skip to content

Commit

Permalink
todo #23665 : can now pass rtm key and secret via method params (Gior…
Browse files Browse the repository at this point in the history
…gio's request)
  • Loading branch information
jmettraux committed Jan 22, 2009
1 parent a37d102 commit ecea532
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
= rufus-rtm CHANGELOG.txt


== rufus-rtm - 0.2 not yet released

- todo #23665 : can now pass rtm key and secret via method params (Giorgio's
request)


== rufus-rtm - 0.1 released 2008/02/14

- initial release
Expand Down
2 changes: 2 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ http://rubyforge.org/frs/?group_id=4812
* RTM_FROB
* RTM_AUTH_TOKEN

(Note since version 0.2, it's OK to not set these environment variables and to pass their values for each method with :api_key, :shared_secret, :frob and :auth_token optional parameters (see test_2 of test/tasks_test.rb))

You have to apply for the first two ones at http://www.rememberthemilk.com/services/api/keys.rtm

Once you have the API key and the shared secret, you have to get the frob and the auth token. Fire your 'irb' and
Expand Down
33 changes: 20 additions & 13 deletions lib/rufus/rtm/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ module RTM
#
# Signs the RTM request (sets the 'api_sig' parameter).
#
def self.sign (params) #:nodoc:
def self.sign (params, secret) #:nodoc:

sig = MD5.md5(secret + params.sort.flatten.join)

sig = MD5.md5(SHARED_SECRET + params.sort.flatten.join)
params['api_sig'] = sig.to_s

params
Expand All @@ -65,26 +66,32 @@ def self.milk (params={}) #:nodoc:

sleep 1

endpoint = params.delete :endpoint
endpoint = params.delete(:endpoint)
endpoint = AUTH_ENDPOINT if endpoint == :auth
endpoint = endpoint || REST_ENDPOINT

ps = params.inject({}) do |r, (k, v)|
r[k.to_s] = v
r
end
ps = params.inject({}) { |r, (k, v)| r[k.to_s] = v; r }

ps['api_key'] = params[:api_key] || ENV['RTM_API_KEY']

raise 'API_KEY missing from environment or parameters, cannot proceed' \
unless ps['api_key']

ps['frob'] = params[:frob] || ENV['RTM_FROB']
ps.delete('frob') if ps['frob'] == nil

ps['auth_token'] = params[:auth_token] || ENV['RTM_AUTH_TOKEN']
ps.delete('auth_token') if ps['auth_token'] == nil

ps['api_key'] = API_KEY
ps['format'] = 'json'

ps['frob'] = FROB if FROB
ps['auth_token'] = AUTH_TOKEN if AUTH_TOKEN
secret = params[:shared_secret] || ENV['RTM_SHARED_SECRET']

sign ps
sign(ps, secret)

res = get endpoint, :query => ps
res = get(endpoint, :query => ps)

JSON.parse(res.body)["rsp"]
JSON.parse(res.body)['rsp']
end

#
Expand Down
17 changes: 4 additions & 13 deletions lib/rufus/rtm/credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ def self.auth_get_frob_and_url #:nodoc:
frob = auth_get_frob

p = {}
p['api_key'] = API_KEY
p['perms'] = "delete"
p['api_key'] = ENV['RTM_API_KEY']
p['perms'] = 'delete'
p['frob'] = frob
sign p
sign(p, ENV['RTM_SHARED_SECRET'])

[
frob,
Expand All @@ -68,16 +68,7 @@ def self.auth_get_token (frob) #:nodoc:
#
# ensuring the credentials are present...

API_KEY = ENV['RTM_API_KEY']
SHARED_SECRET = ENV['RTM_SHARED_SECRET']

raise "API_KEY missing from environment, cannot use Rufus::RTM" \
unless API_KEY

FROB = ENV['RTM_FROB']
AUTH_TOKEN = ENV['RTM_AUTH_TOKEN']

unless FROB
unless ENV['RTM_FROB']

frob, auth_url = auth_get_frob_and_url

Expand Down
14 changes: 7 additions & 7 deletions lib/rufus/rtm/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ def save!
def self.milk_attr (*att_names) #:nodoc:

att_names.each do |att_name|
class_eval """
class_eval %{
def #{att_name}
@hsh['#{att_name}']
end
"""
}
end
end

Expand Down Expand Up @@ -128,11 +128,11 @@ class Task < MilkResource
def self.task_attr (*att_names) #:nodoc:

att_names.each do |att_name|
class_eval """
class_eval %{
def #{att_name}
@hsh['task']['#{att_name}']
end
"""
}
end
end

Expand Down Expand Up @@ -276,10 +276,10 @@ def initialize (h)
@list_id = h['id']
end

def self.find
def self.find (params={})

execute('getList')[resource_name]['list'].collect do |h|
self.new h
execute('getList', params)[resource_name]['list'].collect do |h|
self.new(h)
end
end
end
Expand Down
19 changes: 18 additions & 1 deletion test/tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

require 'test/unit'

$: << File.dirname(__FILE__) + '/../lib'

require 'rufus/rtm'

include Rufus::RTM
Expand Down Expand Up @@ -57,7 +59,7 @@ def test_0
def test_1

lists = List.find
assert_not_nil(lists.find { |e| e.name == "Inbox" })
assert_not_nil(lists.find { |e| e.name == 'Inbox' })

work = lists.find { |e| e.name == "Work" }

Expand All @@ -76,5 +78,20 @@ def test_1

t0.delete!
end

def test_2

key = ENV.delete('RTM_API_KEY')
secret = ENV.delete('RTM_SHARED_SECRET')

assert_raise RuntimeError do
lists = List.find
end

assert_not_nil List.find(:api_key => key, :shared_secret => secret)

ENV['RTM_API_KEY'] = key
ENV['RTM_SHARED_SECRET'] = secret
end
end

0 comments on commit ecea532

Please sign in to comment.