Sha256: 7d203011179d166359089ecc810340985ecebd1339861dcea8e82bbee58c3ee5

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

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

module Hackpad
  module Cli

    class ApiException < StandardError
    end

    module Api
      extend self

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

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

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

      def title(id)
        show(id, 'txt').lines.first
      end

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

      def read(id, ext)
        get "/api/1.0/pad/#{id}/content.#{ext}", false
      end

      def get(url, json=true)
        res = @token.get url
        if res.is_a? Net::HTTPSuccess
          puts res.body.inspect if ENV['DEBUG']
          if json
            JSON.parse res.body
          else
            res.body
          end
        else
          raise ApiException, "HTTP error, code #{res.code}"
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hackpad-cli-0.0.7 lib/hackpad/cli/api.rb
hackpad-cli-0.0.6 lib/hackpad/cli/api.rb
hackpad-cli-0.0.5 lib/hackpad/cli/api.rb