Class: Learnosity::Sdk::Request::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/learnosity/sdk/request/init.rb

Constant Summary collapse

@@valid_security_keys =

Keynames that are valid in the security_packet, they are also in the correct order for signature generation.

['consumer_key', 'domain', 'timestamp', 'expires', 'user_id']
@@valid_services =

Service names that are valid for ‘$service`

['assess', 'author', 'data', 'events', 'items', 'questions', 'reports', 'authoraide']
@@telemetry_enabled =

Determines if telemetry is enabled

true
@@signaturePrefix =
'$02$'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, security_packet, secret, request_packet = nil, action = nil) ⇒ Init

Returns a new instance of Init.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/learnosity/sdk/request/init.rb', line 37

def initialize(service, security_packet, secret, request_packet = nil, action = nil)
  @sign_request_data = false
  @service = service
  @security_packet = security_packet.clone unless security_packet.nil?
  @secret = secret
  @request_packet = request_packet.clone unless request_packet.nil?
  @action = action

  validate

  if @@telemetry_enabled
    add_meta
  end

  set_service_options

  @request_string = generate_request_string
  @security_packet['signature'] = generate_signature
end

Instance Attribute Details

#request_stringObject

XXX: Needs to be public for unit tests



15
16
17
# File 'lib/learnosity/sdk/request/init.rb', line 15

def request_string
  @request_string
end

#security_packetObject

XXX: Needs to be public for unit tests



15
16
17
# File 'lib/learnosity/sdk/request/init.rb', line 15

def security_packet
  @security_packet
end

Class Method Details

.disable_telemetryObject



33
34
35
# File 'lib/learnosity/sdk/request/init.rb', line 33

def self.disable_telemetry
  @@telemetry_enabled = false
end

.enable_telemetryObject



29
30
31
# File 'lib/learnosity/sdk/request/init.rb', line 29

def self.enable_telemetry
  @@telemetry_enabled = true
end

Instance Method Details

#generate(encode = true) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/learnosity/sdk/request/init.rb', line 76

def generate(encode = true)
  output = {}

  case @service
  when 'assess', 'author', 'data', 'items', 'reports', 'authoraide'
    output['security'] =  @security_packet

    unless @request_packet.nil?
      output['request'] = @request_packet
    end

    case @service
    when 'data'
      data_output = { 'security' => JSON.generate(output['security']) }

      if output.key?('request')
        data_output['request'] = JSON.generate(output['request'])
      end

      unless @action.nil?
        data_output['action'] = @action
      end

      return data_output

    when 'assess'
      output = output['request']
    end

  when 'questions'
    output = hash_except(@security_packet, 'domain')

    unless @request_packet.nil?
      output = output.merge(@request_packet)
    end

  when 'events'
    output['security'] =  @security_packet
    output['config'] =  @request_packet
  else
    raise Exception, "generate() for #{@service} not implemented"
  end

  unless encode
    return output
  end

  JSON.generate(output)
end

#generate_signatureObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/learnosity/sdk/request/init.rb', line 57

def generate_signature
  signature_array = []
  @@valid_security_keys.each do |k|
    if @security_packet.include? k
      signature_array.<< @security_packet[k]
    end
  end

  if @sign_request_data and ! @request_string.nil?
    signature_array << @request_string
  end

  unless @action.nil?
    signature_array << @action
  end

  hash_signature(signature_array, @secret)
end