Sha256: 476f68fc9fcc0aae41d9f037f24a7535ee6bef993fd71a89990d5b7ea0d227de

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'oauth'
require 'net/http'
require 'json'
require 'reverse_markdown'
require 'awesome_print'
require_relative 'config'

module Hackpad
  class Client

    def initialize(configdir)
      @configdir = configdir
      @config = Config.load configdir
      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 list
      res = @token.get "/api/1.0/pads/all"
      if res.is_a? Net::HTTPSuccess
        all = JSON.parse res.body
        all.each do |a|
          puts 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} - #{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.1 lib/hackpad/client.rb