lib/airborne/base.rb in airborne-0.0.18 vs lib/airborne/base.rb in airborne-0.0.19

- old
+ new

@@ -1,39 +1,49 @@ -require 'rest_client' require 'json' +require 'active_support/core_ext' module Airborne include RequestExpectations 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) - make_request(:get, url, {headers: headers}) + set_response(make_request(:get, url, {headers: headers})) end def post(url, post_body = nil, headers = nil) - make_request(:post, url, {body: post_body, headers: headers}) + set_response(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}) + set_response(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}) + set_response(make_request(:put, url, {body: put_body, headers: headers})) end def delete(url, headers = nil) - make_request(:delete, url, {headers: headers}) + set_response(make_request(:delete, url, {headers: headers})) end def response @response end @@ -50,39 +60,18 @@ @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 + @headers = HashWithIndifferentAccess.new(res.headers.deep_symbolize_keys) unless res.headers.nil? begin @json_body = JSON.parse(res.body, symbolize_names: true) unless res.body.empty? rescue end end