Sha256: 4eb72f3757bf72990a72b7a6e18b1f1c68015317750a0253559e6373d474bae3

Contents?: true

Size: 998 Bytes

Versions: 1

Compression:

Stored size: 998 Bytes

Contents

module Board
  module API

    class Base

      def initialize(api_key, base_url)
        @api_key  = api_key
        @base_url = base_url
      end

      private

      def get(*args)
        request(:get, *args)
      end

      def post(*args)
        request(:post, *args)
      end

      def request(http_method, *args)
        path   = args.shift
        params = args.shift.merge(:user_credentials => @api_key)
        url    = @base_url + path

        response = case http_method
        when :get
          RestClient.get(url, { :params => params, :accept => :json })
        when :post
          RestClient.post(url, encode_json(params), :content_type => :json, :accept => :json)
        else
          raise ArgumentError, "unknown http method: #{http_method}"
        end

        decode_json(response.body)
      end

      def encode_json(obj)
        Yajl::Encoder.encode(obj)
      end

      def decode_json(json)
        Yajl::Parser.parse(json)
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
board-client-0.1.0 lib/board/api/base.rb