class OpenStax::Exchange::RealClient

Public Class Methods

new(exchange_configuration) click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 10
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

Public Instance Methods

create_identifiers() click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 32
def create_identifiers
  options = {}
  add_accept_header! options
  add_content_type_header! options

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

  return Hashie::Mash.new(JSON.parse(response.body))
end
is_real_client?() click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 24
def is_real_client?
  true
end
record_grade(identifier, resource, trial, grade, grader) click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 63
def record_grade(identifier, resource, trial, grade, grader)
  options = {}
  add_accept_header! options
  add_authorization_header! options
  add_content_type_header! options

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

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

  return JSON.parse(response.body)
end
record_multiple_choice_answer(identifier, resource, trial, answer) click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 46
def record_multiple_choice_answer(identifier, resource, trial, answer)
  options = {}
  add_accept_header! options
  add_authorization_header! options
  add_content_type_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
token() click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 28
def token
  @oauth_token.token
end

Private Instance Methods

add_accept_header!(options) click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 87
def add_accept_header!(options)
  add_header_hash! options
  options[:headers].merge!({ 'Accept' => "application/vnd.exchange.openstax.#{@api_version}" })
end
add_authorization_header!(options) click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 92
def add_authorization_header!(options)
  add_header_hash! options
  options[:headers].merge!({ 'Authorization' => "Bearer #{token}" })
end
add_content_type_header!(options) click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 97
def add_content_type_header!(options)
  add_header_hash! options
  options[:headers].merge!({ 'Content-Type' => "application/json" })
end
add_header_hash!(options) click to toggle source
# File lib/openstax/exchange/real_client/real_client.rb, line 83
def add_header_hash!(options)
  options[:headers] = {} unless options.has_key? :headers
end