Class: OpenStax::Exchange::RealClient

Inherits:
Object
  • Object
show all
Includes:
ClientInstance
Defined in:
lib/openstax/exchange/real_client/real_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(exchange_configuration) ⇒ RealClient

Returns a new instance of RealClient.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/openstax/exchange/real_client/real_client.rb', line 9

def initialize(exchange_configuration)
  @platform_id     = exchange_configuration.client_platform_id
  @platform_secret = exchange_configuration.client_platform_secret
  @server_url      = exchange_configuration.client_server_url
  @api_version     = exchange_configuration.client_api_version

  @oauth_client = OAuth2::Client.new(
    @platform_id,
    @platform_secret,
    site: @server_url)

  @oauth_token = @oauth_client.client_credentials.get_token
end

Instance Method Details

#create_identifierObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/openstax/exchange/real_client/real_client.rb', line 31

def create_identifier
  options = {}
  add_accept_header! options

  response = @oauth_token.request(
    :post,
    "#{@server_url}/api/identifiers",
    options)

  return JSON.parse(response.body)['identifier']
end

#is_real_client?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/openstax/exchange/real_client/real_client.rb', line 23

def is_real_client?
  true
end

#record_multiple_choice_answer(identifier, resource, trial, answer) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/openstax/exchange/real_client/real_client.rb', line 43

def record_multiple_choice_answer(identifier, resource, trial, answer)
  options = {}
  add_accept_header! options
  add_authorization_header! options

  options[:body] = { identifier: identifier, resource: resource, trial: trial, answer: answer }.to_json

  response = @oauth_token.request(
    :post,
    "#{@server_url}/api/events/platforms/multiple_choices",
    options)

  return JSON.parse(response.body)
end

#tokenObject



27
28
29
# File 'lib/openstax/exchange/real_client/real_client.rb', line 27

def token
  @oauth_token.token
end