Sha256: 11817d51f7f228a1309004975d168a963e0f074f5db972bd15a6b354f12249a0

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

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

module Hackpad
  module Cli

    class ApiException < StandardError
    end

    module Api
      module_function

      def prepare(config)
        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 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, 'User-Agent' => "hackpad-cli v#{Hackpad::Cli::VERSION}"
        if res.is_a? Net::HTTPSuccess
          json ? JSON.parse(res.body) : res.body
        else
          fail ApiException, "HTTP error, code #{res.code}"
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hackpad-cli-0.1.1 lib/hackpad/cli/api.rb