lib/hackpad/cli/api.rb in hackpad-cli-0.1.2 vs lib/hackpad/cli/api.rb in hackpad-cli-0.1.3

- old
+ new

@@ -1,26 +1,24 @@ require 'oauth' require 'net/http' require 'json' require 'reverse_markdown' +require_relative '../../reverse_markdown/converters/head' + module Hackpad module Cli class ApiException < StandardError end module Api module_function - def prepare(config) - consumer = OAuth::Consumer.new( - config.client_id, - config.secret, - site: config.site - ) - @token = OAuth::AccessToken.new consumer + def prepare(workspace) + @hackpad ||= OAuth::Consumer.new(workspace.client_id, workspace.secret, site: workspace.site) + @version = File.read(File.expand_path('../../../../CHANGELOG.md', __FILE__))[/([0-9]+\.[0-9]+\.[0-9]+)/] end def search(term, start = 0) get "/api/1.0/search?q=#{CGI.escape term}&start=#{start}&limit=100" end @@ -33,16 +31,16 @@ get "/api/1.0/pad/#{id}/options" end def read(id, ext) realext = (ext == 'md') ? 'html' : ext - get "/api/1.0/pad/#{id}/content.#{realext}", false, (ext == 'md') + get "/api/1.0/pad/#{id}/content/latest.#{realext}", false, (ext == 'md') end def get(url, json = true, to_md = false) - res = @token.get url, 'User-Agent' => "hackpad-cli v#{Hackpad::Cli::VERSION}" - if res.is_a? Net::HTTPSuccess + res = @hackpad.request :get, url + if res.is_a? Net::HTTPOK if json JSON.parse(res.body) else if to_md cleanup_md(res.body) @@ -55,13 +53,14 @@ end end def cleanup_md(text) back = ReverseMarkdown.convert(text, github_flavored: true).strip - back.sub!(/<head>.*<\/head>\n/m, '') - back.gsub!(/-([^\n]+)\n\n -/m, "-\\1\n -") - back.gsub!(/\n( )*-([^\n]+)\n?\n( )*-([^\n]+)\n?\n/m, "\n\\1-\\2\n\\3-\\4\n") - back.gsub(/\n\n\*\*([^\*]+)\*\*\n\n/, "\n\n### \\1\n\n") + back.gsub!(/\n-\s*\n/m, "\n") # empty list items + back.gsub!(/\n\\\*\s*\n/m, "\n") # images are shown as \* + back.gsub!(/-([^\n]+)\n\n -/m, "-\\1\n -") # avoid extra blank lines in lists + back.gsub!(/\n( )*-([^\n]+)\n?\n( )*-([^\n]+)\n?\n/m, "\n\\1-\\2\n\\3-\\4\n") # another more generalist for lists + back.gsub(/\n\n?\*\*([^\*]+)\*\*\n\n?/, "\n\n### \\1\n\n") # transform isolated titles from bold to h3 end end end end