class GithubToCanvasQuiz::CanvasAPI::Client

Attributes

api_key[R]
host[R]

Public Class Methods

new(host:, api_key:) click to toggle source
# File lib/github_to_canvas_quiz/canvas_api/client.rb, line 10
def initialize(host:, api_key:)
  @host = host
  @api_key = api_key
end

Private Instance Methods

delete(endpoint) click to toggle source
# File lib/github_to_canvas_quiz/canvas_api/client.rb, line 59
def delete(endpoint)
  RestClient.delete(host + endpoint, {
    'Authorization' => "Bearer #{api_key}"
  })
rescue RestClient::BadRequest => e
  report_error(e)
end
get(endpoint) click to toggle source
# File lib/github_to_canvas_quiz/canvas_api/client.rb, line 17
def get(endpoint)
  response = RestClient.get(host + endpoint, {
    'Authorization' => "Bearer #{api_key}"
  })
  JSON.parse(response)
rescue RestClient::BadRequest => e
  report_error(e)
end
get_all(endpoint) click to toggle source
# File lib/github_to_canvas_quiz/canvas_api/client.rb, line 26
def get_all(endpoint)
  data = []
  url = host + endpoint
  while url
    response = RestClient.get(url, {
      'Authorization' => "Bearer #{api_key}"
    })
    data += JSON.parse(response)
    url = next_url(response.headers[:link])
  end
  data
rescue RestClient::BadRequest => e
  report_error(e)
end
next_url(link) click to toggle source
# File lib/github_to_canvas_quiz/canvas_api/client.rb, line 67
def next_url(link)
  link.split(/,/).detect { |rel| rel.match(/rel="next"/) }.split(/;/).first.strip[1..-2]
rescue NoMethodError
  nil
end
post(endpoint, payload) click to toggle source
# File lib/github_to_canvas_quiz/canvas_api/client.rb, line 41
def post(endpoint, payload)
  response = RestClient.post(host + endpoint, payload, {
    'Authorization' => "Bearer #{api_key}"
  })
  JSON.parse(response)
rescue RestClient::BadRequest => e
  report_error(e)
end
put(endpoint, payload) click to toggle source
# File lib/github_to_canvas_quiz/canvas_api/client.rb, line 50
def put(endpoint, payload)
  response = RestClient.put(host + endpoint, payload, {
    'Authorization' => "Bearer #{api_key}"
  })
  JSON.parse(response)
rescue RestClient::BadRequest => e
  report_error(e)
end
report_error(err) click to toggle source
# File lib/github_to_canvas_quiz/canvas_api/client.rb, line 73
def report_error(err)
  puts "Request failed. #{err.response.code} status code returned."
  puts JSON.parse(err.response.body)
  abort
end