Module: OpenStax::Api::RSpecHelpers

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

Instance Method Summary collapse

Instance Method Details

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



29
30
31
# File 'lib/openstax/api/rspec_helpers.rb', line 29

def api_delete(action, doorkeeper_token, args={})
  api_request(:delete, action, doorkeeper_token, args)
end

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



17
18
19
# File 'lib/openstax/api/rspec_helpers.rb', line 17

def api_get(action, doorkeeper_token, args={})
  api_request(:get, action, doorkeeper_token, args)
end

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



37
38
39
# File 'lib/openstax/api/rspec_helpers.rb', line 37

def api_head(action, doorkeeper_token, args={})
  api_request(:head, action, doorkeeper_token, args)
end

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



33
34
35
# File 'lib/openstax/api/rspec_helpers.rb', line 33

def api_patch(action, doorkeeper_token, args={}) 
  api_request(:patch, action, doorkeeper_token, args)
end

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



25
26
27
# File 'lib/openstax/api/rspec_helpers.rb', line 25

def api_post(action, doorkeeper_token, args={})
  api_request(:post, action, doorkeeper_token, args)
end

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



21
22
23
# File 'lib/openstax/api/rspec_helpers.rb', line 21

def api_put(action, doorkeeper_token, args={})
  api_request(:put, action, doorkeeper_token, args)
end

#api_request(type, action, doorkeeper_token, args = {}) ⇒ Object

Raises:

  • (IllegalArgument)


41
42
43
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 41

def api_request(type, action, doorkeeper_token, args={})
  raise IllegalArgument if ![:get, :post, :put, :delete, :patch, :head].include?(type)

  header = is_a_controller_spec? ? request.env : {}

  # Add the doorkeeper token info
  header['HTTP_AUTHORIZATION'] = "Bearer #{doorkeeper_token.token}" \
    if doorkeeper_token

  # 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?
  header['HTTP_ACCEPT'] = "application/vnd.openstax.#{version_string}"

  # Set the raw post data in the request, converting to JSON if needed
  if args[:raw_post_data]
    header['RAW_POST_DATA'] = args[:raw_post_data].is_a?(Hash) ? 
                                args[:raw_post_data].to_json : 
                                args[:raw_post_data]
  end

  # Set the data format
  args[:parameters] ||= {}
  args[:parameters][:format] = 'json'
  header['CONTENT_TYPE'] = 'application/json'

  # 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

  if action.is_a? String
    action = "/#{action}" if !action.starts_with?("/")
    action = "/api#{action}" if !action.starts_with?("/api/")
  end

  if is_a_controller_spec?
    send(type, action, args[:parameters], args[:session], args[:flash])
  else
    send(type, action, args[:parameters].to_json, header)
  end
end