Sha256: d5bf5dfae25dd7a06b67d91e58d78ab0c4c1b9e957589c7c6999308ed98d1b97

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

require 'kookaburra/api_driver'
require 'delegate'
require 'active_support/json'

class Kookaburra
  # Delegates all methods (by default) to and instance of
  # {Kookaburra::APIDriver}.
  #
  # Expects the application's API to accept and respond with JSON formatted
  # data. All methods will decode the response body using
  # `ActiveSupport::JSON.decode`. Methods that take input data ({#post} and
  # {#put}) will encode the post data using `ActiveSupport::JSON.encode`.
  class JsonApiDriver < SimpleDelegator
    #
    # Sets both the "Content-Type" and "Accept" headers to "application/json".
    #
    # @param [Kookaburra::Configuration] configuration
    # @param [Kookaburra::APIDriver] api_driver (Kookaburra::APIDriver.new)
    #   The APIDriver instance to be delegated to. Changing this is probably
    #   only useful for testing Kookaburra itself.
    def initialize(configuration, api_driver = nil)
      api_driver = api_driver || APIDriver.new(configuration)
      api_driver.headers.merge!(
        'Content-Type' => 'application/json',
        'Accept' => 'application/json'
      )
      super(api_driver)
    end

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

    def put(path, data, *args)
      request(:put, path, data, *args)
    end

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

    def delete(path, *args)
      request(:delete, path, nil, *args)
    end

    private

    def request(type, path, data = nil, *args)
      # don't want to send data to methods that don't accept it
      args = [path, encode(data), args].flatten.compact
      output = __getobj__.send(type, *args)

      decode(output)
    end

    def encode(data)
      ActiveSupport::JSON.encode(data) unless data.nil?
    end

    def decode(data)
      ActiveSupport::JSON.decode(data)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kookaburra-0.22.2 lib/kookaburra/json_api_driver.rb
kookaburra-0.22.1 lib/kookaburra/json_api_driver.rb
kookaburra-0.22.0 lib/kookaburra/json_api_driver.rb