plugin/yo_update.rb in tdiary-contrib-4.0.5.1 vs plugin/yo_update.rb in tdiary-contrib-4.1.0
- old
+ new
@@ -16,10 +16,22 @@
YO_UPDATE_TIMEOUT = 10
class YoUpdateError < StandardError; end
+def yo_update_url(date = nil, frag = nil) # date: Time frag: e.g. 'p01'
+ url = @conf.index.dup
+ url[0, 0] = base_url unless %r|^https?://|i =~ url
+ url.gsub!( %r|/\./|, '/' )
+ if date
+ ymd = date.strftime('%Y%m%d')
+ anc = frag ? "#{ymd}##{frag}" : ymd
+ url += anchor(anc)
+ end
+ url
+end
+
def yo_update_api_key
r = @conf['yo_update.api_key']
if not r or r.empty?
return nil
end
@@ -46,36 +58,39 @@
rescue SocketError => e
raise YoUpdateError, e.message
end
end
-def yo_update_send_yo(username = nil)
+def yo_update_send_yo(username = nil, url = '')
api_key = yo_update_api_key
unless api_key
raise YoUpdateError, "Yo API Key is not set"
end
+ data = {'api_token' => api_key}
+ data['link'] = url unless url.empty?
unless username
req = Net::HTTP::Post.new(URI("http://api.justyo.co/yoall/"))
- req.set_form_data('api_token' => yo_update_api_key)
+ req.set_form_data(data)
expected = '{}'
else
req = Net::HTTP::Post.new(URI("http://api.justyo.co/yo/"))
- req.set_form_data('api_token' => yo_update_api_key, 'username' => username)
+ data['username'] = username
+ req.set_form_data(data)
expected = '{"result": "OK"}'
end
res = yo_update_access_api(req)
data = res.body
unless data == expected
raise YoUpdateError, "error from Yo API: #{data}"
end
return data
end
-def yo_update_send_yo_or_log(username = nil)
+def yo_update_send_yo_or_log(username = nil, url = '')
return unless yo_update_api_key
begin
- yo_update_send_yo(username)
+ yo_update_send_yo(username, url)
rescue YoUpdateError => e
@logger.error "yo_update.rb: #{e.message}"
end
end
@@ -129,11 +144,11 @@
#{%w(send_on_update send_on_comment).map{|action|
checked = conf["yo_update.#{action}"] ? ' checked' : ''
%Q|<li><label for="yo_update.#{action}"><input id="yo_update.#{action}" name="yo_update.#{action}" value="t" type="checkbox"#{checked}>#{action_label[action]}</label>|
}.join("\n\t")}
</ul>
- <p>Test sending Yo! to <input name="yo_update.test" value="" size="10">#{test_result}</p>
+ <p>Test sending Yo! to <input name="yo_update.test" value="" size="10"> with optional link <input name="yo_update.link" value="#{yo_update_url}" size="40">#{test_result}</p>
<h3 class="subtitle">Current Subscribers</h3>
<p>#{h n_subscribers}</p>
<h3 class="subtitle">Yo button</h3>
<p>Add the following to somewhere or your diary.</p>
<pre><div id="yo-button"></div></pre>
@@ -154,13 +169,14 @@
@conf['yo_update.api_key'] = @cgi.params['yo_update.api_key'][0]
@conf['yo_update.username'] = @cgi.params['yo_update.username'][0]
@conf['yo_update.send_on_update'] = (@cgi.params['yo_update.send_on_update'][0] == 't')
@conf['yo_update.send_on_comment'] = (@cgi.params['yo_update.send_on_comment'][0] == 't')
test_username = @cgi.params['yo_update.test'][0]
+ test_link = @cgi.params['yo_update.link'][0]
if test_username and not test_username.empty?
begin
- result = yo_update_send_yo(test_username)
+ result = yo_update_send_yo(test_username, test_link)
rescue YoUpdateError => e
result = e.message
end
test_result = yo_update_test_result_label(test_username, result)
end
@@ -176,12 +192,15 @@
yo_update_conf_html(@conf, n_subscribers, test_result)
end
add_update_proc do
if @mode == 'append' and @conf['yo_update.send_on_update']
- yo_update_send_yo_or_log
+ url = yo_update_url(@date) # link to the date
+ yo_update_send_yo_or_log(nil, url)
elsif @mode == 'comment' and @comment and @comment.visible? and @conf['yo_update.send_on_comment']
- yo_update_send_yo_or_log
+ frag = "c%02d" % @diaries[@date.strftime("%Y%m%d")].count_comments(true)
+ url = yo_update_url(@date, frag)
+ yo_update_send_yo_or_log(nil, url)
end
end
add_header_proc do
if @conf['yo_update.api_key']