lib/airborne/base.rb in airborne-0.1.15 vs lib/airborne/base.rb in airborne-0.1.16
- old
+ new
@@ -1,21 +1,26 @@
require 'json'
-require 'active_support/hash_with_indifferent_access'
+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?)
+ if !Airborne.configuration.requester_module.nil?
base.send(:include, Airborne.configuration.requester_module)
- elsif(!Airborne.configuration.rack_app.nil?)
+ elsif !Airborne.configuration.rack_app.nil?
base.send(:include, RackTestRequester)
else
base.send(:include, RestClientRequester)
end
end
@@ -23,57 +28,46 @@
def self.configuration
RSpec.configuration
end
def get(url, headers = nil)
- set_response(make_request(:get, url, {headers: headers}))
+ set_response(make_request(:get, url, headers: headers))
end
def post(url, post_body = nil, headers = nil)
- set_response(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 )
- set_response(make_request(:patch, url, {body: patch_body, headers: headers}))
+ def patch(url, patch_body = nil, headers = nil)
+ set_response(make_request(:patch, url, body: patch_body, headers: headers))
end
- def put(url, put_body = nil, headers = nil )
- set_response(make_request(:put, url, {body: put_body, headers: headers}))
+ def put(url, put_body = nil, headers = nil)
+ set_response(make_request(:put, url, body: put_body, headers: headers))
end
def delete(url, headers = nil)
- set_response(make_request(:delete, url, {headers: headers}))
+ set_response(make_request(:delete, url, headers: headers))
end
def head(url, headers = nil)
- set_response(make_request(:head, url, {headers: headers}))
+ set_response(make_request(:head, url, headers: headers))
end
def options(url, headers = nil)
- set_response(make_request(:options, url, {headers: headers}))
+ set_response(make_request(:options, url, headers: headers))
end
- def response
- @response
- end
-
- def headers
- @headers
- end
-
- def body
- @body
- end
-
def json_body
- raise InvalidJsonError, "Api request returned invalid json" unless @json_body
+ set_response(@response) if @json_body.nil? && !@response.nil?
+ fail InvalidJsonError, 'Api request returned invalid json' unless @json_body
@json_body
end
private
def get_url(url)
- base = Airborne.configuration.base_url || ""
+ base = Airborne.configuration.base_url || ''
base + url
end
def set_response(res)
@response = res