class Moodle::Api::Request

Handles making the request to the Moodle API and interpreting the results. The Moodle API is not consistent in the way it returns responses so the type of response is determined, successful responses are returned, failures are parsed and raised in a generic fashion.

Attributes

response[R]

Public Instance Methods

post(path, options = {}) click to toggle source
# File lib/moodle/api/request.rb, line 13
def post(path,  options = {})
  @response = Typhoeus.post(path, options)
  resolve_response if response.success?
end

Private Instance Methods

exception_raised?() click to toggle source
# File lib/moodle/api/request.rb, line 32
def exception_raised?
  external_services_api_exception? && token_service_api_exception?
end
external_services_api_exception?() click to toggle source
# File lib/moodle/api/request.rb, line 36
def external_services_api_exception?
  response_body.is_a?(Hash) && response_body['exception']
end
request_raised_exception?() click to toggle source
# File lib/moodle/api/request.rb, line 24
def request_raised_exception?
  if external_services_api_exception?
    fail MoodleError, response_body['message']
  elsif token_service_api_exception?
    fail MoodleError, response_body['error']
  end
end
resolve_response() click to toggle source
# File lib/moodle/api/request.rb, line 20
def resolve_response
  response_body unless request_raised_exception?
end
response_body() click to toggle source

API calls that return null are considered successful

# File lib/moodle/api/request.rb, line 45
def response_body
  JSON.parse(response.body)
rescue JSON::ParserError
  response.body
end
token_service_api_exception?() click to toggle source
# File lib/moodle/api/request.rb, line 40
def token_service_api_exception?
  response_body.is_a?(Hash) && response_body['error']
end