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, #connection=, #new_connection, #request

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



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

def initialize(args = {})
  @options = Credly.options.merge(args.except(:username, :password))
  if args[:username] && args[:password]
    @options[:access_token] = authenticate(args[:username], args[:password])
  elsif !args[:access_token]
    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



29
30
31
# File 'lib/credly/client.rb', line 29

def access_token
  options[:access_token]
end

#authenticate(username, password) ⇒ Object



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

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 AuthFailed.new("The username or password was invalid\n#{resp.inspect}")
  end
end

#badge_builder(id = nil) ⇒ Object



69
70
71
# File 'lib/credly/client.rb', line 69

def badge_builder(id = nil)
  Api::BadgeBuilder.new(:client => self, :id => id)
end

#badges(id = nil) ⇒ Object



53
54
55
# File 'lib/credly/client.rb', line 53

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

#base_urlObject



49
50
51
# File 'lib/credly/client.rb', line 49

def base_url
  options[:base_endpoint]
end

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



45
46
47
# File 'lib/credly/client.rb', line 45

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

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



33
34
35
# File 'lib/credly/client.rb', line 33

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

#me(id = nil) ⇒ Object



65
66
67
# File 'lib/credly/client.rb', line 65

def me(id = nil)
  Api::Me.new(:client => self, :id => id)
end

#member_badges(id = nil) ⇒ Object



57
58
59
# File 'lib/credly/client.rb', line 57

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

#members(id = nil) ⇒ Object



61
62
63
# File 'lib/credly/client.rb', line 61

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

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



37
38
39
# File 'lib/credly/client.rb', line 37

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

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



41
42
43
# File 'lib/credly/client.rb', line 41

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

#versioned_path(path) ⇒ Object



73
74
75
# File 'lib/credly/client.rb', line 73

def versioned_path(path)
  [options[:version], path].join('/')
end