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'
API_VERSION =
'2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Rumember.



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

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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



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

def api_version
  @api_version
end

#shared_secretObject (readonly)

Returns the value of attribute shared_secret.



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

def shared_secret
  @shared_secret
end

Class Method Details

.accountObject



123
124
125
# File 'lib/rumember.rb', line 123

def self.
  @account ||= new.
end

.config_fileObject



103
104
105
# File 'lib/rumember.rb', line 103

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

.run(argv) ⇒ Object



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

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



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

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



62
63
64
65
66
67
# File 'lib/rumember.rb', line 62

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



81
82
83
84
# File 'lib/rumember.rb', line 81

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

#authenticateObject



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

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



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

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



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

def dispatch(method, params = {})
  require 'json'
  require 'open-uri'
  raw = URI.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



74
75
76
77
78
79
# File 'lib/rumember.rb', line 74

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



96
97
98
99
100
101
# File 'lib/rumember.rb', line 96

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

#sign(params) ⇒ Object



69
70
71
72
# File 'lib/rumember.rb', line 69

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

#url(params) ⇒ Object



127
128
129
# File 'lib/rumember.rb', line 127

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