Module: Hackpad::Cli::Api

Defined in:
lib/hackpad/cli/api.rb

Class Method Summary collapse

Class Method Details

.cleanup_md(text) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/hackpad/cli/api.rb', line 56

def cleanup_md(text)
  back = ReverseMarkdown.convert(text, github_flavored: true).strip
  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

.get(url, json = true, to_md = false) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hackpad/cli/api.rb', line 39

def get(url, json = true, to_md = false)
  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)
      else
        res.body
      end
    end
  else
    fail ApiException, "HTTP error, code #{res.code}"
  end
end

.listObject



26
27
28
# File 'lib/hackpad/cli/api.rb', line 26

def list
  get '/api/1.0/pads/all'
end

.prepare(workspace) ⇒ Object



17
18
19
20
# File 'lib/hackpad/cli/api.rb', line 17

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

.read(id, ext) ⇒ Object



34
35
36
37
# File 'lib/hackpad/cli/api.rb', line 34

def read(id, ext)
  realext = (ext == 'md') ? 'html' : ext
  get "/api/1.0/pad/#{id}/content/latest.#{realext}", false, (ext == 'md')
end

.read_options(id) ⇒ Object



30
31
32
# File 'lib/hackpad/cli/api.rb', line 30

def read_options(id)
  get "/api/1.0/pad/#{id}/options"
end

.search(term, start = 0) ⇒ Object



22
23
24
# File 'lib/hackpad/cli/api.rb', line 22

def search(term, start = 0)
  get "/api/1.0/search?q=#{CGI.escape term}&start=#{start}&limit=100"
end