Module: Hackpad::Cli::Api

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

Class Method Summary collapse

Class Method Details

.cleanup_md(text) ⇒ Object



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

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")
end

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



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

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
    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



28
29
30
# File 'lib/hackpad/cli/api.rb', line 28

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

.prepare(config) ⇒ Object



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

def prepare(config)
  consumer = OAuth::Consumer.new(
    config.client_id,
    config.secret,
    site: config.site
  )
  @token = OAuth::AccessToken.new consumer
end

.read(id, ext) ⇒ Object



36
37
38
39
# File 'lib/hackpad/cli/api.rb', line 36

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

.read_options(id) ⇒ Object



32
33
34
# File 'lib/hackpad/cli/api.rb', line 32

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

.search(term, start = 0) ⇒ Object



24
25
26
# File 'lib/hackpad/cli/api.rb', line 24

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