Sha256: 60cfb012891c10cd1a3981b0c0944109ddc847dcf9233bcf9686b71464efa1a3

Contents?: true

Size: 1.86 KB

Versions: 20

Compression:

Stored size: 1.86 KB

Contents

require 'json'
require 'active_support'
require 'active_support/core_ext/hash/indifferent_access'

module Airborne
  class InvalidJsonError < StandardError; end

  include RequestExpectations

  attr_reader :response, :headers, :body

  def self.configure
    RSpec.configure do |config|
      yield config
    end
  end

  def self.included(base)
    if !Airborne.configuration.requester_module.nil?
      base.send(:include, Airborne.configuration.requester_module)
    elsif !Airborne.configuration.rack_app.nil?
      base.send(:include, RackTestRequester)
    else
      base.send(:include, RestClientRequester)
    end
  end

  def self.configuration
    RSpec.configuration
  end

  def get(url, headers = nil)
    @response = make_request(:get, url, headers: headers)
  end

  def post(url, post_body = nil, headers = nil)
    @response = make_request(:post, url, body: post_body, headers: headers)
  end

  def patch(url, patch_body = nil, headers = nil)
    @response = make_request(:patch, url, body: patch_body, headers: headers)
  end

  def put(url, put_body = nil, headers = nil)
    @response = make_request(:put, url, body: put_body, headers: headers)
  end

  def delete(url, delete_body = nil, headers = nil)
    @response = make_request(:delete, url, body: delete_body, headers: headers)
  end

  def head(url, headers = nil)
    @response = make_request(:head, url, headers: headers)
  end

  def options(url, headers = nil)
    @response = make_request(:options, url, headers: headers)
  end

  def response
    @response
  end

  def headers
    HashWithIndifferentAccess.new(response.headers)
  end

  def body
    response.body
  end

  def json_body
    JSON.parse(response.body, symbolize_names: true) rescue fail InvalidJsonError, 'Api request returned invalid json'
  end

  private

  def get_url(url)
    base = Airborne.configuration.base_url || ''
    base + url
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
airborne-0.3.2 lib/airborne/base.rb
airborne-0.3.1 lib/airborne/base.rb
airborne-0.3.0 lib/airborne/base.rb
hops-airborne-0.2.16 lib/airborne/base.rb
hops-airborne-0.2.15 lib/airborne/base.rb
airborne-0.2.13 lib/airborne/base.rb
airborne-0.2.12 lib/airborne/base.rb
airborne-0.2.11 lib/airborne/base.rb
airborne-0.2.10 lib/airborne/base.rb
airborne-0.2.9 lib/airborne/base.rb
airborne-0.2.8 lib/airborne/base.rb
airborne-0.2.7 lib/airborne/base.rb
airborne-0.2.6 lib/airborne/base.rb
airborne-0.2.5 lib/airborne/base.rb
airborne-0.2.4 lib/airborne/base.rb
airborne-0.2.3 lib/airborne/base.rb
airborne-0.2.2 lib/airborne/base.rb
airborne-0.2.1 lib/airborne/base.rb
airborne-0.2.0 lib/airborne/base.rb
airborne-0.1.20 lib/airborne/base.rb