Class: LearnWorlds::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id: nil, client_secret: nil, base_url: nil, access_token: nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
# File 'lib/learn_worlds/client.rb', line 11

def initialize(client_id: nil, client_secret: nil, base_url: nil, access_token: nil)
  @client_id = client_id || ENV.fetch('LEARN_WORLDS_CLIENT_ID')
  @client_secret = client_secret || ENV.fetch('LEARN_WORLDS_CLIENT_SECRET', nil)
  @base_url = base_url || ENV.fetch('LEARN_WORLDS_BASE_URL')
  @access_token = access_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



9
10
11
# File 'lib/learn_worlds/client.rb', line 9

def access_token
  @access_token
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



8
9
10
# File 'lib/learn_worlds/client.rb', line 8

def base_url
  @base_url
end

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/learn_worlds/client.rb', line 8

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



8
9
10
# File 'lib/learn_worlds/client.rb', line 8

def client_secret
  @client_secret
end

#expires_inObject

Returns the value of attribute expires_in.



9
10
11
# File 'lib/learn_worlds/client.rb', line 9

def expires_in
  @expires_in
end

Instance Method Details

#authenticationObject



22
23
24
# File 'lib/learn_worlds/client.rb', line 22

def authentication
  AuthenticationResource.new(self)
end

#connectionObject



30
31
32
33
34
35
36
# File 'lib/learn_worlds/client.rb', line 30

def connection
  @connection ||= Faraday.new(base_url) do |conn|
    conn.request :json
    conn.response :json, content_type: "application/json"
    conn.headers = required_headers
  end
end

#required_headersObject



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

def required_headers
  headers = { 'Lw-Client': client_id }
  headers.merge({ Authorization: "Bearer #{access_token}" }) if access_token

  headers
end

#ssoObject



26
27
28
# File 'lib/learn_worlds/client.rb', line 26

def sso
  SingleSignOnResource.new(self)
end

#userObject



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

def user
  UserResource.new(self)
end