Class: Rumember

Inherits:
Object
  • Object
show all
Defined in:
lib/rumember.rb,
lib/rumember/list.rb,
lib/rumember/task.rb,
lib/rumember/account.rb,
lib/rumember/abstract.rb,
lib/rumember/location.rb,
lib/rumember/timeline.rb,
lib/rumember/transaction.rb

Defined Under Namespace

Modules: Dispatcher Classes: Abstract, Account, Error, List, Location, ResponseError, Task, Timeline, Transaction

Constant Summary collapse

API_KEY =
'36f62f69fba7135e8049adbe307ff9ba'
SHARED_SECRET =
'0c33513097c09be4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = API_KEY, shared_secret = SHARED_SECRET) ⇒ Rumember

Returns a new instance of Rumember.



55
56
57
58
# File 'lib/rumember.rb', line 55

def initialize(api_key = API_KEY, shared_secret = SHARED_SECRET)
  @api_key = api_key
  @shared_secret = shared_secret
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



53
54
55
# File 'lib/rumember.rb', line 53

def api_key
  @api_key
end

#shared_secretObject (readonly)

Returns the value of attribute shared_secret.



53
54
55
# File 'lib/rumember.rb', line 53

def shared_secret
  @shared_secret
end

Class Method Details

.accountObject



121
122
123
# File 'lib/rumember.rb', line 121

def self.
  @account ||= new.
end

.config_fileObject



101
102
103
# File 'lib/rumember.rb', line 101

def self.config_file
  File.expand_path('~/.rtm.yml')
end

.run(argv) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rumember.rb', line 39

def self.run(argv)
  if argv.empty?
    puts "Logged in as #{.username}"
  else
    .smart_add(argv.join(' '))
  end
rescue Error
  $stderr.puts "#$!"
  exit 1
rescue Interrupt
  $stderr.puts "Interrupted!"
  exit 130
end

Instance Method Details

#account(auth_token = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rumember.rb', line 105

def (auth_token = nil)
  if auth_token
    Account.new(self, auth_token)
  else
    require 'yaml'
    @account ||=
      begin
        reconfigure unless File.exist?(self.class.config_file)
        t = YAML.load(File.read(self.class.config_file))['auth_token']
        (t)
      end
  end
end

#api_sig(params) ⇒ Object



60
61
62
63
64
65
# File 'lib/rumember.rb', line 60

def api_sig(params)
  require 'digest/md5'
  Digest::MD5.hexdigest(
    shared_secret + params.sort_by {|k,v| k.to_s}.join
  )
end

#auth_url(perms = :delete, extra = {}) ⇒ Object



79
80
81
82
# File 'lib/rumember.rb', line 79

def auth_url(perms = :delete, extra = {})
  "http://rememberthemilk.com/services/auth?" +
    params({'perms' => perms}.merge(extra))
end

#authenticateObject



84
85
86
87
88
89
90
91
92
# File 'lib/rumember.rb', line 84

def authenticate
  require 'launchy'
  frob = dispatch('auth.getFrob')['frob']
  Launchy.open(auth_url(:delete, 'frob' => frob))
  first = true
  puts 'Press enter when authentication is complete'
  $stdin.gets
  dispatch('auth.getToken', 'frob' => frob)['auth']
end

#autoconfigureObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rumember.rb', line 119

def (auth_token = nil)
  if auth_token
    Account.new(self, auth_token)
  else
    require 'yaml'
    @account ||=
      begin
        reconfigure unless File.exist?(self.class.config_file)
        t = YAML.load(File.read(self.class.config_file))['auth_token']
        (t)
      end
  end
end

#dispatch(method, params = {}) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rumember.rb', line 129

def dispatch(method, params = {})
  require 'json'
  require 'open-uri'
  raw = open(url(params.merge('method' => "rtm.#{method}", 'format' => 'json'))).read
  rsp = JSON.parse(raw)['rsp']
  case rsp['stat']
  when 'fail'
    error = ResponseError.new(rsp['err']['msg'])
    error.code = rsp['err']['code']
    error.set_backtrace caller
    raise error
  when 'ok'
    rsp.delete('stat')
    rsp
  else
    raise ResponseError.new(rsp.inspect)
  end
end

#params(params) ⇒ Object



72
73
74
75
76
77
# File 'lib/rumember.rb', line 72

def params(params)
  require 'cgi'
  sign(params).map do |k,v|
    "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
  end.join('&')
end

#reconfigureObject



94
95
96
97
98
99
# File 'lib/rumember.rb', line 94

def reconfigure
  token = authenticate['token']
  File.open(self.class.config_file,'w') do |f|
    f.puts "auth_token: #{token}"
  end
end

#sign(params) ⇒ Object



67
68
69
70
# File 'lib/rumember.rb', line 67

def sign(params)
  params = params.merge('api_key' => api_key)
  params.update('api_sig' => api_sig(params))
end

#url(params) ⇒ Object



125
126
127
# File 'lib/rumember.rb', line 125

def url(params)
  "http://api.rememberthemilk.com/services/rest?#{params(params)}"
end