Module: Rufus::RTM

Defined in:
lib/rufus/rtm/base.rb,
lib/rufus/rtm/resources.rb,
lib/rufus/rtm/credentials.rb

Defined Under Namespace

Classes: List, MilkResource, TagArray, Task

Constant Summary collapse

VERSION =
'0.1.2'
AUTH_ENDPOINT =
"http://www.rememberthemilk.com/services/auth/"
REST_ENDPOINT =
"http://api.rememberthemilk.com/services/rest/"

Class Method Summary collapse

Class Method Details

.auth_get_frobObject

:nodoc:



28
29
30
31
32
# File 'lib/rufus/rtm/credentials.rb', line 28

def self.auth_get_frob #:nodoc:

  r = milk(:method => 'rtm.auth.getFrob')
  r['frob']
end

.auth_get_frob_and_urlObject

:nodoc:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rufus/rtm/credentials.rb', line 34

def self.auth_get_frob_and_url #:nodoc:

  frob = auth_get_frob

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

  [
    frob,
    AUTH_ENDPOINT + '?' + p.collect { |k, v| "#{k}=#{v}" }.join("&")
  ]
end

.auth_get_token(frob) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
# File 'lib/rufus/rtm/credentials.rb', line 50

def self.auth_get_token (frob) #:nodoc:

  begin
    milk(:method => 'rtm.auth.getToken', :frob => frob)['auth']['token']
  rescue Exception => e
    nil
  end
end

.get(endpoint, hash) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/rufus/rtm/base.rb', line 97

def self.get(endpoint, hash)
   query=hash.inject(""){|result, item|
     result+="#{item[0].to_s}=#{CGI.escape(item[1].to_s)}&"
   }
   url=endpoint+'?'+query[0..query.length-2]
   p url
   Net::HTTP.get(URI.parse(url))
end

.get_timelineObject

Requests a timeline from RTM.



92
93
94
95
# File 'lib/rufus/rtm/base.rb', line 92

def self.get_timeline #:nodoc:

  milk(:method => 'rtm.timelines.create')['timeline']
end

.milk(params = {}) ⇒ Object

Calls an API method (milk the cow).



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rufus/rtm/base.rb', line 57

def self.milk (params={}) #:nodoc:

  sleep 1

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

  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['format'] = 'json'

  secret = params[:shared_secret] || ENV['RTM_SHARED_SECRET']

  sign(ps, secret)

  res = get(endpoint,ps)

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

.sign(params, secret) ⇒ Object

Signs the RTM request (sets the ‘api_sig’ parameter).



45
46
47
48
49
50
51
52
# File 'lib/rufus/rtm/base.rb', line 45

def self.sign (params, secret) #:nodoc:

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

  params['api_sig'] = sig.to_s

  params
end