Class: CompanionCube::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/companion_cube/client.rb

Constant Summary collapse

API_HEADERS =
{ "Accept" => "application/json" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(url:, access_key: nil, secret_key: nil) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
# File 'lib/companion_cube/client.rb', line 8

def initialize(url:, access_key: nil, secret_key: nil)
  raise "Missing required parameter 'url'" if url.nil? || url.empty?

  keys_empty = !access_key.nil? && access_key.empty?
  keys_empty ||= !secret_key.nil? && secret_key.empty?
  raise "Access/secret key cannot be blank" if keys_empty

  self.resource = RestClient::Resource.new(url, access_key, secret_key)
end

Instance Method Details

#course(id) ⇒ Object



32
33
34
35
36
# File 'lib/companion_cube/client.rb', line 32

def course(id)
  raise "Missing required parameter 'id'" if id.nil?

  JSON.parse(send_request(:get, "/courses/#{id}"))
end

#coursesObject



18
19
20
# File 'lib/companion_cube/client.rb', line 18

def courses
  JSON.parse(send_request(:get, "/courses"))
end

#create_course(params) ⇒ Object



22
23
24
25
26
# File 'lib/companion_cube/client.rb', line 22

def create_course(params)
  params[:multipart] = true

  send_request(:post, "/courses", body: params)
end

#delete_course(id) ⇒ Object



28
29
30
# File 'lib/companion_cube/client.rb', line 28

def delete_course(id)
  send_request(:delete, "/courses/#{id}")
end

#enroll_user(id, email) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/companion_cube/client.rb', line 38

def enroll_user(id, email)
  raise "Missing required parameter 'id'" if id.nil?
  raise "Missing required parameter 'email'" if email.nil?
  params = Hash.new
  params[:id] = id
  params[:email] = email
  send_request(:put, "/enrollments/#{id}", body: params)
end

#reports(type, params = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/companion_cube/client.rb', line 53

def reports(type, params = {})
  raise "Missing required parameter 'type'" if type.nil?
  format = params.fetch(:format, :json).to_sym

  response = send_request(:get, "/reports/#{type}", params: params, accept: format)
  return JSON.parse(response) if format == :json
  # csv, etc.
  response
end

#update_course(id, params) ⇒ Object



47
48
49
50
51
# File 'lib/companion_cube/client.rb', line 47

def update_course(id, params)
  params[:multipart] = true

  send_request(:put, "/courses/#{id}", body: params)
end