/
add_send_to_link.rb
68 lines (56 loc) · 1.63 KB
/
add_send_to_link.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'nokogiri'
require 'net/http'
# sorta command-patterny
class AddSendToLink
def initialize google_api, current_account
@google_api, @current_account = google_api, current_account
@info = {}
end
attr_reader :google_api, :current_account
attr_reader :info # for debugging and error output
def prefs_to_hash prefs
h = {}
prefs.each do |pref|
h[pref['id']] = pref['value']
end
h
end
def perform
# todo: make response more pretty, probably a redirect too
data = nil
prefs = google_api.preference_list["prefs"]
prefs = prefs_to_hash(prefs)
if prefs["custom-item-links"]
value = prefs["custom-item-links"]
value_hash = JSON.parse(value)
else
value_hash = {
"builtinLinksEnabledState" => [],
"customLinks" => []
}
end
# customLinks == "Send To"
customLinks = value_hash['customLinks']
customLinks.delete_if{|entry|
entry['url'] =~ %r{^http://sharebro.org}
}
customLinks << {
"url" => "http://sharebro.org/send_to?sharebro_id=#{current_account["_id"]}&title=${title}&url=${url}&source=${source}",
"iconUrl" => "http://sharebro.org/favicon.ico",
"enabled" => true,
"name" => "Sharebro"
}
@info[:custom_links] = customLinks
set_params = {"k" => "custom-item-links",
"v" => JSON.dump(value_hash),
}
@info[:set_params] = set_params
response = google_api.post_json "/reader/api/0/preference/set", set_params
@info[:response] = response
if response == {:response => "OK"}
return :ok
else
return :error
end
end
end