Sha256: cafadb0902b71b222c1fac7bab6e962dea1f3ae81fbbe41b5a416d3dc3491741

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

require 'rest_client'
require 'json'

module Airborne
	include RequestExpectations

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

	def self.configuration
		RSpec.configuration
	end

	def get(url, headers = nil)
		make_request(:get, url, {headers: headers})
	end

	def post(url, post_body = nil, headers = nil)
		make_request(:post, url, {body: post_body, headers: headers})
	end

	def patch(url, patch_body = nil, headers = nil )
		make_request(:patch, url, {body: patch_body, headers: headers})
	end

	def put(url, put_body = nil, headers = nil )
		make_request(:put, url, {body: put_body, headers: headers})
	end

	def delete(url, headers = nil)
		make_request(:delete, url, {headers: headers})
	end

	def response
		@response
	end

	def headers
		@headers
	end

	def body
		@body
	end

	def json_body
		@json_body
	end

	private

	def make_request(method, url, options = {})
		headers = (options[:headers] || {}).merge({content_type: :json})
		base_headers = Airborne.configuration.headers || {}
		headers = base_headers.merge(headers)
		res = if method == :post || method == :patch || method == :put
			begin
				RestClient.send(method, get_url(url), options[:body].nil? ? "" : options[:body].to_json, headers)
			rescue RestClient::Exception => e
				e.response
			end
		else
			begin
				RestClient.send(method, get_url(url), headers)
			rescue RestClient::Exception => e
				e.response
			end

		end
		set_response(res)
	end

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

	def set_response(res)
		@response = res
		@body = res.body
		@headers = res.headers
		begin
			@json_body = JSON.parse(res.body, symbolize_names: true) unless res.body == ""
		rescue
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
airborne-0.0.16 lib/airborne/base.rb
airborne-0.0.15 lib/airborne/base.rb