Sha256: c637d9233610c01ad99c427b4e341b3a8df33d964ac6994fc620995d0a1a3efe

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'oauth'
require 'net/http'
require 'json'
require 'reverse_markdown'

require_relative 'config'

module Hackpad
  class Client

    def initialize(options)
      @config = Config.load options
      site = URI.parse @config['site']
      consumer = OAuth::Consumer.new(
        @config['client_id'],
        @config['secret'],
        site: @config['site']
      )
      @token = OAuth::AccessToken.new consumer
    end

    # GET /api/1.0/pads/all
    def listall
      res = @token.get "/api/1.0/pads/all"
      if res.is_a? Net::HTTPSuccess
        all = JSON.parse res.body
        all.each do |a|
          getinfo(a)
        end
      else
        puts "#{res.inspect}".colorize :red
        puts "#{res.body}".colorize :red
        return back
      end
    end

    def getinfo(pad)
      res = @token.get "/api/1.0/pad/#{pad}/content.txt"
      if res.is_a? Net::HTTPSuccess
        puts "#{@config['site']}/#{pad} - #{pad} - #{res.body.lines.first.chomp}"
      else
        puts "#{pad} failed".colorize :red
      end
    end

    def show(pad,format)
      ext = (format == 'md') ? 'html' : format
      res = @token.get "/api/1.0/pad/#{pad}/content.#{ext}"
      if res.is_a? Net::HTTPSuccess
        puts "#{@config['site']}/#{pad}"
        puts
        if format == 'md'
          puts ReverseMarkdown.convert(res.body, github_flavored: true)
        else
          puts res.body
        end
      else
        puts "#{pad} failed".colorize :red
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hackpad-cli-0.0.3 lib/hackpad/client.rb