Sha256: 199ea04d3183a130cd357342628510441b191dc205513fb4b73af80217389bb8

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

require 'oauth2'

module OpenStax
  module Exchange

    class RealClient
      include ClientInstance

      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

      def is_real_client?
        true
      end

      def token
        @oauth_token.token
      end

      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

      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

      private

      def add_header_hash!(options)
        options[:headers] = {} unless options.has_key? :headers
      end

      def add_accept_header!(options)
        add_header_hash! options
        options[:headers].merge!({ 'Accept' => "application/vnd.exchange.openstax.#{@api_version}" })
      end

      def add_authorization_header!(options)
        add_header_hash! options
        options[:headers].merge!({ 'Authorization' => "Bearer #{token}" })
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
openstax_exchange-0.0.2 lib/openstax/exchange/real_client/real_client.rb
openstax_exchange-0.0.1 lib/openstax/exchange/real_client/real_client.rb