Module: OpenStax::Api::RSpecHelpers

Defined in:
lib/openstax/api/rspec_helpers.rb

Instance Method Summary collapse

Instance Method Details

#api_delete(action, doorkeeper_token = nil, args = {}) ⇒ Object



32
33
34
# 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 = {}) ⇒ Object



20
21
22
# 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 = {}) ⇒ Object



40
41
42
# 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 = {}) ⇒ Object



36
37
38
# 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 = {}) ⇒ Object



28
29
30
# 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 = {}) ⇒ Object



24
25
26
# 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 = {}) ⇒ Object

Raises:

  • (IllegalArgument)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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.[: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