Sha256: bf8b9867b697f611331011eff68b9cb989f1fb567c95e025b50cded918e62ed7

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require 'faraday'
require 'cassette-rack/response'

module CassetteRack
  module Request
    attr_reader :request_options

    def get(path, params={}, headers={})
      request(:get, path, params, headers)
    end

    def post(path, body={}, headers={})
      request(:post, path, {}, headers, body)
    end

    def patch(path, body={}, headers={})
      request(:patch, path, {}, headers, body)
    end

    def put(path, body={}, headers={})
      request(:put, path, {}, headers, body)
    end

    def delete(path, headers={})
      request(:delete, path, {}, headers)
    end

    def request(method, path, params={}, headers={}, body={}, options={})
      if request_options
        options = request_options
      else
        options = { url: CassetteRack.config.url, headers: headers }
      end

      conn = Faraday.new(options)
      res = conn.send(method) do |req|
        case method
        when :get, :delete
          req.url path, params
        when :post, :patch, :put
          req.path = path
          req.body = parse_content(body, req)
        end
      end

      @response = CassetteRack::Response.new(res)
    end

    def response
      @response
    end

    def parse_content(body, req)
      if req.headers['content-type'] == 'application/json' and body.is_a?(Hash)
        body.to_json
      else
        body
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cassette-rack-0.10.0 lib/cassette-rack/request.rb
cassette-rack-0.9.0 lib/cassette-rack/request.rb
cassette-rack-0.8.1 lib/cassette-rack/request.rb
cassette-rack-0.8.0 lib/cassette-rack/request.rb