Class: TalentLMS::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


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

def initialize(config = {})
  raise ArgumentError.new('Missing api_key') unless config.has_key?(:api_key)
  @auth_header = auth_header(config[:api_key])

  raise ArgumentError.new('Missing sub_domain') unless config.has_key?(:sub_domain)
  @sub_domain = config[:sub_domain]

  @version = 'v1' || config[:version]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



29
30
31
32
33
# File 'lib/talentlms/client.rb', line 29

def method_missing(sym, *args, &block)
  url = route_for_method(sym.to_s, *args)
  response = HTTP.with(@auth_header).get(url)
  JSON.parse(response)
end

Instance Method Details

#auth_header(username) ⇒ Object



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

def auth_header(username)
  key = Base64.strict_encode64("#{username}:")
  { 'Authorization' => "Basic #{key}" }
end

#route_for_method(method, options = nil) ⇒ Object



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

def route_for_method(method, options=nil)
  url = "https://#{@sub_domain}.talentlms.com/api/#{@version}/#{method}"
  return url if options.nil?
  arguments = options.map {|k,v| "#{k}:#{v}"}.join(',')
  "#{url}/#{arguments}"
end