Class: D2L::Valence::UserContext

Inherits:
Object
  • Object
show all
Defined in:
lib/d2l/valence/user_context.rb

Overview

UserContext

Instances of this class are used to make D2L Valance API calls with the current user credentials

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_context:, user_id:, user_key:) ⇒ UserContext

Returns a new instance of UserContext.

Parameters:

  • app_context (D2L::Valence::AppContext)
  • user_id (String)

    User ID returned from the D2L Server in authentication process

  • user_key (String)

    User Key returned the D2L Server in authentication process



15
16
17
18
19
20
# File 'lib/d2l/valence/user_context.rb', line 15

def initialize(app_context:, user_id:, user_key:)
  @app_context = app_context
  @user_id = user_id
  @user_key = user_key
  @server_skew = 0
end

Instance Attribute Details

#app_contextObject (readonly)

Returns the value of attribute app_context.



6
7
8
# File 'lib/d2l/valence/user_context.rb', line 6

def app_context
  @app_context
end

#server_skewObject

in seconds



10
11
12
# File 'lib/d2l/valence/user_context.rb', line 10

def server_skew
  @server_skew
end

#user_idObject (readonly)

Returns the value of attribute user_id.



6
7
8
# File 'lib/d2l/valence/user_context.rb', line 6

def user_id
  @user_id
end

#user_keyObject (readonly)

Returns the value of attribute user_key.



6
7
8
# File 'lib/d2l/valence/user_context.rb', line 6

def user_key
  @user_key
end

Instance Method Details

#api_call(http_method:, route:, route_params:, query_params:) ⇒ D2L::Valence::RequestResult

Calls a API method on the Valance API

API routes

When providing the route you can also provide the variables for the parameters in the route. For example, the following route will require β€˜1, group_category_id: 23` for `route_params`:

/d2l/api/lp/:version/:org_unit_id/groupcategories/:group_category_id

The β€˜to_uri` method will place the parameters in the route to make the final path. For example:

/d2l/api/lp/1.0/1/groupcategories/23

There are known parameters such as β€˜:version` which is provided when you create your initial AppContext instance. This will mean that some routes will require no parameters for example:

/d2l/api/lp/:version/users/whoami

which becomes /d2l/api/lp/1.0/users/whoami

Parameters:

  • http_method (String)

    the HTTP Method for the call (i.e. PUT, GET, POST, DELETE)

  • route (String)

    the API method route (e.g. /d2l/api/lp/:version/users/whoami)

  • route_params (Hash)

    the parameters for the API method route (option)

  • query_params (Hash)

    the query parameters for the method call

Returns:

  • (D2L::Valence::RequestResult)

    returns a request



47
48
49
50
51
52
53
54
55
# File 'lib/d2l/valence/user_context.rb', line 47

def api_call(http_method:, route:, route_params:, query_params:)
  D2L::Valence::Request.new(
    user_context: self,
    http_method: http_method,
    route: route,
    route_params: route_params,
    query_params: query_params
  ).execute
end