Sha256: eb4ee686b25febc047cf435dfc85a4cfb2ffa636db7bd67b4a2f082fdc02d205

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

# Ruby interface to Wesabe's Grendel (http://github.com/wesabe/grendel)
module Grendel
  class Client
    attr_accessor :debug, :debug_output
    attr_reader :base_uri

    # Create a new Grendel client instance
    def initialize(base_uri, options = {})
      @base_uri = base_uri
      @debug = options[:debug]
      @debug_output = options[:debug_output] || $stderr
    end

    def get(uri, options = {})
      process_response HTTParty.get(@base_uri + uri, process_options(options))
    end

    def head(uri, options = {})
      process_response HTTParty.head(@base_uri + uri, process_options(options))
    end

    def post(uri, data = {}, options = {})
      process_response HTTParty.post(@base_uri + uri, process_options(options, data))
    end

    def put(uri, data = {}, options = {})
      process_response HTTParty.put(@base_uri + uri, process_options(options, data))
    end

    def delete(uri, options = {})
      process_response HTTParty.delete(@base_uri + uri, process_options(options))
    end

    def users
      UserManager.new(self)
    end

    private
      def process_response(response)
        raise HTTPException.new(response) if response.code >= 400
        return response
      end

      def process_options(options, data=nil)
        options = options.dup

        if data
          data = data.to_json unless options.delete(:raw_data)
          options[:body] ||= data
          options[:headers] = (options[:headers] || {}).dup
          options[:headers]['Content-Type'] ||= 'application/json'
        end

        options[:debug_output] ||= @debug_output if @debug

        return options
      end

    class HTTPException < Exception
      def initialize(response)
        msg = "#{response.code} #{response.message}"
        msg << "\n#{response.body}" unless response.body.blank?
        super(msg)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grendel-ruby-0.1.3 lib/grendel/client.rb