module OpenStax::Api::RSpecHelpers
Public Instance Methods
api_delete(action, doorkeeper_token = nil, args={})
click to toggle source
# File lib/openstax/api/rspec_helpers.rb, line 32 def api_delete(action, doorkeeper_token = nil, args={}) api_request(:delete, action, doorkeeper_token, args) end
api_get(action, doorkeeper_token = nil, args={})
click to toggle source
# File lib/openstax/api/rspec_helpers.rb, line 20 def api_get(action, doorkeeper_token = nil, args={}) api_request(:get, action, doorkeeper_token, args) end
api_head(action, doorkeeper_token = nil, args={})
click to toggle source
# File lib/openstax/api/rspec_helpers.rb, line 40 def api_head(action, doorkeeper_token = nil, args={}) api_request(:head, action, doorkeeper_token, args) end
api_patch(action, doorkeeper_token = nil, args={})
click to toggle source
# File lib/openstax/api/rspec_helpers.rb, line 36 def api_patch(action, doorkeeper_token = nil, args={}) api_request(:patch, action, doorkeeper_token, args) end
api_post(action, doorkeeper_token = nil, args={})
click to toggle source
# File lib/openstax/api/rspec_helpers.rb, line 28 def api_post(action, doorkeeper_token = nil, args={}) api_request(:post, action, doorkeeper_token, args) end
api_put(action, doorkeeper_token = nil, args={})
click to toggle source
# File lib/openstax/api/rspec_helpers.rb, line 24 def api_put(action, doorkeeper_token = nil, args={}) api_request(:put, action, doorkeeper_token, args) end
api_request(type, action, doorkeeper_token = nil, args={})
click to toggle source
# File lib/openstax/api/rspec_helpers.rb, line 44 def api_request(type, action, doorkeeper_token = nil, args={}) raise IllegalArgument unless [:head, :get, :post, :patch, :put, :delete].include?(type) headers = is_a_controller_spec? ? request.headers : {} # Select the version of the API based on the spec metadata and populate the accept header version_string = self.class.metadata[:version].try(:to_s) raise ArgumentError, "Top-level 'describe' metadata must include a value for ':version'" \ if version_string.nil? headers['HTTP_ACCEPT'] = "application/vnd.openstax.#{version_string}" # Add the doorkeeper token header headers['HTTP_AUTHORIZATION'] = "Bearer #{doorkeeper_token.token}" \ if doorkeeper_token headers['CONTENT_TYPE'] = 'application/json' if is_a_controller_spec? request.headers.merge! headers args[:format] = :json # Convert the request body to JSON if needed args[:body] = args[:body].to_json unless args[:body].nil? || args[:body].is_a?(String) else args[:headers] = headers end # If these helpers are used from a request spec, action can # be a URL fragment string -- in such a case, prepend "/api" # to the front of the URL as a convenience to callers action = action.to_s unless is_a_controller_spec? if action.is_a?(String) && !action.include?('://') action = "/#{action}" if !action.starts_with?('/') action = "/api#{action}" if !action.starts_with?('/api/') end send type, action, **args end
Private Instance Methods
is_a_controller_spec?()
click to toggle source
# File lib/openstax/api/rspec_helpers.rb, line 85 def is_a_controller_spec? self.class.metadata[:type] == :controller end