lib/shelr/publisher.rb in shelr-0.11.8 vs lib/shelr/publisher.rb in shelr-0.11.9
- old
+ new
@@ -3,23 +3,51 @@
module Shelr
class Publisher
def publish(id)
- uri = URI.parse(Shelr::API_URL + '/records')
- params = { 'record' => prepare(id) }
- params.merge!({'api_key' => Shelr.api_key}) if api_key
- res = Net::HTTP.post_form(uri, params)
+ with_exception_handler do
+ uri = URI.parse(Shelr::API_URL + '/records')
+ params = { 'record' => prepare(id) }
+ params.merge!({'api_key' => Shelr.api_key}) if api_key
+ handle_response Net::HTTP.post_form(uri, params)
+ end
+ end
+
+ def dump(id)
+ with_exception_handler do
+ File.open(dump_filename, 'w+') do |f|
+ f.puts(prepare(id))
+ end
+ puts "=> record dumped to #{dump_filename}"
+ end
+ end
+
+ private
+
+ def with_exception_handler(&block)
+ yield
+ rescue => e
+ puts "=> Something went wrong..."
+ puts e.message
+ puts e.backtrace.join("\n")
+ end
+
+ def handle_response(res)
res = JSON.parse(res.body)
if res['ok']
puts res['message']
puts Shelr::API_URL + '/records/' + res['id']
else
puts res['message']
end
end
+ def dump_filename
+ File.join(Dir.getwd, 'shelr-record.json')
+ end
+
def api_key
unless Shelr.api_key
print 'Paste your API KEY [or Enter to publish as Anonymous]: '
key = STDIN.gets.strip
Shelr.api_key = key unless key.empty?
@@ -27,11 +55,11 @@
Shelr.api_key
end
def prepare(id)
puts
- puts 'Your record will be published under terms'
- puts 'of Creative Commons Attribution-ShareAlike 3.0 Unported'
+ puts 'Your record will be published under terms of'
+ puts 'Creative Commons Attribution-ShareAlike 3.0 Unported'
puts 'See http://creativecommons.org/licenses/by-sa/3.0/ for details.'
puts
out = {}
['meta', 'timing', 'typescript'].each do |file|