Class: Credly::Client

Inherits:
Object
  • Object
show all
Includes:
Connection, Requierable
Defined in:
lib/credly/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Requierable

#require_at_least_one_file, #require_present

Methods included from Connection

#connection, #new_connection, #request

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(args = {})
  @options = Credly.options
  if args[:username] && args[:password]
    @options[:access_token] = authenticate(args[:username], args[:password])
  elsif args[:access_token]
    @options[:access_token] = args[:access_token]
  else
    raise ArgumentError.new("Need either an 'access_token' or 'username' and 'password' parameters")
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/credly/client.rb', line 6

def options
  @options
end

Instance Method Details

#access_tokenObject



31
32
33
# File 'lib/credly/client.rb', line 31

def access_token
  options[:access_token]
end

#authenticate(username, password) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/credly/client.rb', line 19

def authenticate(username, password)
  connection = new_connection
  connection.basic_auth(username, password)
  resp = connection.post(versioned_path('authenticate'))
  resp = MultiJson.load(resp.body)
  if resp['meta']['status_code'] == 200
    resp['data']['token']
  else
    raise "The username or password was invalid\n#{resp}"
  end
end

#badges(id = nil) ⇒ Object



55
56
57
# File 'lib/credly/client.rb', line 55

def badges(id = nil)
  Api::Badges.new(:client => self, :id => id)
end

#base_urlObject



51
52
53
# File 'lib/credly/client.rb', line 51

def base_url
  options[:base_endpoint]
end

#delete(path, params = {}, headers = {}) ⇒ Object



47
48
49
# File 'lib/credly/client.rb', line 47

def delete(path, params = {}, headers = {})
  super(versioned_path(path), {:access_token => access_token}.merge(params), headers)
end

#get(path, params = {}, headers = {}) ⇒ Object



35
36
37
# File 'lib/credly/client.rb', line 35

def get(path, params = {}, headers = {})
  super(versioned_path(path), {:access_token => access_token}.merge(params), headers)
end

#member_badges(id = nil) ⇒ Object



59
60
61
# File 'lib/credly/client.rb', line 59

def member_badges(id = nil)
  Api::MemberBadges.new(:client => self, :id => id)
end

#members(id = nil) ⇒ Object



63
64
65
# File 'lib/credly/client.rb', line 63

def members(id = nil)
  Api::Members.new(:client => self, :id => id)
end

#post(path, params = {}, headers = {}) ⇒ Object



39
40
41
# File 'lib/credly/client.rb', line 39

def post(path, params = {}, headers = {})
  super(versioned_path(path), {:access_token => access_token}.merge(params), headers)
end

#put(path, params = {}, headers = {}) ⇒ Object



43
44
45
# File 'lib/credly/client.rb', line 43

def put(path, params = {}, headers = {})
  super(versioned_path(path), {:access_token => access_token}.merge(params), headers)
end