alexmchale-red-milk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +3 -0
- data/VERSION.yml +4 -0
- data/lib/redmilk/monkey.rb +48 -0
- data/lib/redmilk.rb +112 -0
- metadata +58 -0
data/README.markdown
ADDED
data/VERSION.yml
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'pp'
|
3
|
+
require 'andand'
|
4
|
+
require 'cgi'
|
5
|
+
require 'yaml'
|
6
|
+
require 'digest/md5'
|
7
|
+
|
8
|
+
class Array
|
9
|
+
def binary
|
10
|
+
map {|e| yield(e) ? [e, nil] : [nil, e]}.transpose.map {|a| a.compact}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Object
|
15
|
+
def to_cents
|
16
|
+
(to_s.gsub(/[^-.0-9]/, '').to_f * 100).to_i
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Date
|
21
|
+
def days_in_month
|
22
|
+
(Date.parse("12/31/#{strftime("%Y")}") << (12 - month)).day
|
23
|
+
end
|
24
|
+
|
25
|
+
def tomorrow; self + 1; end
|
26
|
+
def yesterday; self - 1; end
|
27
|
+
|
28
|
+
def last_sunday
|
29
|
+
d = self
|
30
|
+
d -= 1 until d.wday == 0
|
31
|
+
d
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Hash
|
36
|
+
def to_url
|
37
|
+
map {|key, value| "#{CGI.escape key.to_s}=#{CGI.escape value.to_s}"}.join "&"
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_cookie
|
41
|
+
map {|key, value| "#{key}=#{value}"}.join('; ')
|
42
|
+
end
|
43
|
+
|
44
|
+
def rtm_sig(secret)
|
45
|
+
Digest::MD5.hexdigest(secret + (to_a.sort {|a, b| a[0].to_s <=> b[0].to_s}.join))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
data/lib/redmilk.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'appconfig'
|
5
|
+
require 'redmilk/monkey.rb'
|
6
|
+
require 'pp'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
class RedMilk
|
10
|
+
def initialize
|
11
|
+
@http = Net::HTTP.new('www.rememberthemilk.com', 443)
|
12
|
+
@http.use_ssl = true
|
13
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
14
|
+
|
15
|
+
@config = AppConfig.new('~/.redmilk.yaml')
|
16
|
+
|
17
|
+
@token = get_token
|
18
|
+
end
|
19
|
+
|
20
|
+
def lists
|
21
|
+
get_lists
|
22
|
+
end
|
23
|
+
|
24
|
+
def tasks_due(date = Date.today)
|
25
|
+
get_tasks :filter => ("((list:Inbox AND due:never) OR dueBefore:%s) AND status:incomplete" % date.tomorrow.strftime("%m/%d/%Y"))
|
26
|
+
end
|
27
|
+
|
28
|
+
def inbox
|
29
|
+
get_tasks :filter => "list:Inbox status:incomplete"
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
API_KEY = 'eafde4fb7a120f2ba9f60a1dd4872bb4'
|
35
|
+
SHARED_SECRET = 'b42dd8157a178708'
|
36
|
+
PERM = 'delete'
|
37
|
+
|
38
|
+
def build_params(options = {})
|
39
|
+
options[:api_key] = API_KEY
|
40
|
+
options[:auth_token] = @token if @token
|
41
|
+
(options.merge({:api_sig => options.rtm_sig(SHARED_SECRET)})).to_url
|
42
|
+
end
|
43
|
+
|
44
|
+
def dispatch(method, options = {})
|
45
|
+
options.merge!({:method => method, :format => 'json'})
|
46
|
+
path = "/services/rest/?#{build_params options}"
|
47
|
+
pp path
|
48
|
+
response = @http.get(path, {})
|
49
|
+
JSON.parse(response.body)
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_lists(options = {})
|
53
|
+
dispatch('rtm.lists.getList', options).andand['rsp'].andand['lists'].andand['list']
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_tasks(options = {})
|
57
|
+
dispatch('rtm.tasks.getList', options).andand['rsp'].andand['tasks'].andand['list']
|
58
|
+
end
|
59
|
+
|
60
|
+
def auth_url(frob)
|
61
|
+
"https://www.rememberthemilk.com/services/auth/?#{build_params :frob => frob, :perms => PERM}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_new_frob
|
65
|
+
response = dispatch('rtm.auth.getFrob')
|
66
|
+
|
67
|
+
puts "New frob response:"
|
68
|
+
pp response
|
69
|
+
|
70
|
+
response.andand['rsp'].andand['frob']
|
71
|
+
end
|
72
|
+
|
73
|
+
def get_new_token(frob)
|
74
|
+
return nil unless frob
|
75
|
+
|
76
|
+
response = dispatch('rtm.auth.getToken', :frob => frob)
|
77
|
+
|
78
|
+
puts "New token response:"
|
79
|
+
pp response
|
80
|
+
|
81
|
+
response.andand['rsp'].andand['auth'].andand['token']
|
82
|
+
end
|
83
|
+
|
84
|
+
def check_token(token)
|
85
|
+
return false unless token
|
86
|
+
|
87
|
+
response = dispatch('rtm.auth.checkToken', :auth_token => token)
|
88
|
+
|
89
|
+
puts "Check token response:"
|
90
|
+
pp response
|
91
|
+
|
92
|
+
token1 = response.andand['rsp'].andand['auth'].andand['token']
|
93
|
+
perms1 = response.andand['rsp'].andand['auth'].andand['perms']
|
94
|
+
|
95
|
+
(token1 == token) && (perms1 == PERM)
|
96
|
+
end
|
97
|
+
|
98
|
+
def get_token
|
99
|
+
# Check if we have a good token already.
|
100
|
+
token = @config[:token]
|
101
|
+
return token if check_token(token)
|
102
|
+
|
103
|
+
# Check if we have a frob already which we're waiting for the user to register.
|
104
|
+
frob = @config[:frob]
|
105
|
+
@config[:token] = token = get_new_token(frob)
|
106
|
+
return token if token
|
107
|
+
|
108
|
+
# Get a new frob and ask the user to register it.
|
109
|
+
@config[:frob] = frob = get_new_frob
|
110
|
+
raise "new frob needs to be registered at: #{auth_url frob}"
|
111
|
+
end
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alexmchale-red-milk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex McHale
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-12 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: alexmchale@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- VERSION.yml
|
26
|
+
- README.markdown
|
27
|
+
- lib/redmilk.rb
|
28
|
+
- lib/redmilk
|
29
|
+
- lib/redmilk/monkey.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/alexmchale/red-milk
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options:
|
34
|
+
- --inline-source
|
35
|
+
- --charset=UTF-8
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.2.0
|
54
|
+
signing_key:
|
55
|
+
specification_version: 2
|
56
|
+
summary: RedMilk is a client for Remember The Milk.
|
57
|
+
test_files: []
|
58
|
+
|