Class: RememberTheMilk::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/rememberthemilk/api.rb

Constant Summary collapse

Host =
'www.rememberthemilk.com'
Url =
"http://#{ Host }"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Api

Returns a new instance of Api.



45
46
47
48
49
50
51
52
53
# File 'lib/rememberthemilk/api.rb', line 45

def initialize(*args, &block)
  options = args.options
  @api_key = options[:api_key]
  @shared_secret = options[:shared_secret]
  @frob = options[:frob]
  @token = options[:token]
  @username = options[:username]
  @password = options[:password]
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



38
39
40
# File 'lib/rememberthemilk/api.rb', line 38

def api_key
  @api_key
end

#frobObject

Returns the value of attribute frob.



40
41
42
# File 'lib/rememberthemilk/api.rb', line 40

def frob
  @frob
end

#passwordObject

Returns the value of attribute password.



43
44
45
# File 'lib/rememberthemilk/api.rb', line 43

def password
  @password
end

#shared_secretObject

Returns the value of attribute shared_secret.



39
40
41
# File 'lib/rememberthemilk/api.rb', line 39

def shared_secret
  @shared_secret
end

#tokenObject

Returns the value of attribute token.



41
42
43
# File 'lib/rememberthemilk/api.rb', line 41

def token
  @token
end

#usernameObject

Returns the value of attribute username.



42
43
44
# File 'lib/rememberthemilk/api.rb', line 42

def username
  @username
end

Class Method Details

.escape(val) ⇒ Object



33
34
35
# File 'lib/rememberthemilk/api.rb', line 33

def escape(val)
  CGI::escape(val.to_s).gsub(/ /, '+')
end

.query_string_for(query) ⇒ Object



29
30
31
# File 'lib/rememberthemilk/api.rb', line 29

def query_string_for(query)
  query.to_a.map{|k,v| [escape(k), escape(v)].join('=')}.join('&')
end

.url_for(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rememberthemilk/api.rb', line 7

def url_for(*args)
  options = args.options.pop

  service = options[:service] || :rest
  url = "/services/#{ service }" 
  url += args.flatten.compact.join('/') unless args.empty?
  url.squeeze('/')
  url.chomp!('/')
  url += '/'

  query = options[:query] || {}
  unless query.empty?
    url = url + '?' + query_string_for(query)
  end

  if options[:absolute]
    url = Url + url
  end

  url
end

Instance Method Details

#api_sig_for(query) ⇒ Object



61
62
63
64
# File 'lib/rememberthemilk/api.rb', line 61

def api_sig_for(query)
  kvs = query.to_a.map{|k,v| [k.to_s, v.to_s]}.sort
  Digest::MD5.hexdigest(@shared_secret + kvs.join).to_s
end

#apply_for_an_api_keyObject



55
56
57
58
59
# File 'lib/rememberthemilk/api.rb', line 55

def apply_for_an_api_key
  url = 'http://www.rememberthemilk.com/services/api/keys.rtm'
  puts(url)
  open(url)
end

#call(method, *args, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rememberthemilk/api.rb', line 70

def call(method, *args, &block)
  options = args.options

  query = Hash.new
  query['method'] = method unless method.to_s.empty?
  query['api_key'] = @api_key
  query['auth_token'] = @token
  query['format'] = 'json' 
  options.each do |key, val|
    query[key] = val
  end
  query['api_sig'] = api_sig_for(query)

  url = Api.url_for(:query => query, :service => 'rest')
  response = Net::HTTP.get_response(Host, url)

  json = JSON.parse(response.body)

  rsp = json['rsp']

  if rsp['stat'] != 'ok'
    raise(Error, "#{ method }(#{ args.inspect }) @ #{ url } #=> #{ rsp.inspect }")
  end

  rsp
end

#get_frobObject



128
129
130
131
# File 'lib/rememberthemilk/api.rb', line 128

def get_frob
  rsp = call('rtm.auth.getFrob')
  rsp['frob']
end

#get_token!Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rememberthemilk/api.rb', line 106

def get_token!
  require 'mechanize'
  require 'pp'
  frob = get_frob
  query = Hash.new
  query[:frob] = frob
  query[:api_key] = @api_key
  query[:perms] = 'delete'
  query['api_sig'] = api_sig_for(query)
  url = Api.url_for(:query => query, :service => :auth, :absolute => true)
  response = Net::HTTP.get_response(Host, url)
  location = response.header['Location']
  m = Mechanize.new
  page = m.get(url)
  form = page.forms.first
  form['username'] = @username
  form['password'] = @password
  page = form.submit
  rsp = call('rtm.auth.getToken', :frob => frob)
  token = rsp['auth']['token']
end

#localtime(time) ⇒ Object



97
98
99
100
101
102
# File 'lib/rememberthemilk/api.rb', line 97

def localtime(time)
  time = Time.parse(time.to_s) unless time.is_a?(Time)
  @settings ||= call('rtm.settings.getList')
  @timezone = TZInfo::Timezone.get(@settings['settings']['timezone'])
  @timezone.utc_to_local(time.utc)
end

#pingObject



66
67
68
# File 'lib/rememberthemilk/api.rb', line 66

def ping
  call('rtm.test.echo')
end