class Moodle::Api::TokenGenerator

Generates tokens using username, password and service Used for accessing Moodle API

Attributes

configuration[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/moodle/api/token_generator.rb, line 8
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

call() click to toggle source
# File lib/moodle/api/token_generator.rb, line 12
def call
  if raise_token_exception?
    fail ArgumentError,
         'Username and password are required to generate a token'
  end

  generate_token
end

Private Instance Methods

generate_token() click to toggle source
# File lib/moodle/api/token_generator.rb, line 23
def generate_token
  response = Request.new.post(configuration.token_api_url,
                              params: request_params,
                              headers: { 'Accept' => 'json' })
  response['token']
end
raise_token_exception?() click to toggle source
# File lib/moodle/api/token_generator.rb, line 30
def raise_token_exception?
  configuration.username.nil? || configuration.password.nil?
end
request_params() click to toggle source
# File lib/moodle/api/token_generator.rb, line 34
def request_params
  {
    username: configuration.username,
    password: configuration.password,
    service: configuration.service
  }
end