Class: V0::VirtualAgent::ReportToCxdw

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/v0/virtual_agent/report_to_cxdw.rb

Instance Method Summary collapse

Instance Method Details

#get_new_token(dataverse_uri) ⇒ Object (private)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/v0/virtual_agent/report_to_cxdw.rb', line 19

def get_new_token(dataverse_uri)
  app_uri = Settings.virtual_agent.cxdw_app_uri
  client_id = Settings.virtual_agent.cxdw_client_id
  client_secret = Settings.virtual_agent.cxdw_client_secret

  url = URI("#{app_uri}/oauth2/v2.0/token")

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT

  body_grant_type = 'grant_type=client_credentials'
  body_client_id = "client_id=#{client_id}"
  body_client_secret = "client_secret=#{client_secret}"
  body_scope = "scope=#{dataverse_uri}"

  request = Net::HTTP::Post.new(url)
  request['content-type'] = 'application/x-www-form-urlencoded'
  request.body = "#{body_grant_type}&#{body_client_id}&#{body_client_secret}&#{body_scope}/.default"

  response = http.request(request)
  JSON.parse(response.read_body)['access_token']
rescue => e
  raise StandardError, "Errored retreiving dataverse token with error: #{e}"
end

#report_to_cxdw(icn, conversation_id) ⇒ Object



11
12
13
14
15
# File 'app/controllers/v0/virtual_agent/report_to_cxdw.rb', line 11

def report_to_cxdw(icn, conversation_id)
  dataverse_uri = Settings.virtual_agent.cxdw_dataverse_uri
  token = get_new_token dataverse_uri
  send_to_cxdw dataverse_uri, icn, conversation_id, token
end

#send_to_cxdw(dataverse_uri, icn, conversation_id, token) ⇒ Object (private)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/v0/virtual_agent/report_to_cxdw.rb', line 45

def send_to_cxdw(dataverse_uri, icn, conversation_id, token)
  cxdw_table_prefix = Settings.virtual_agent.cxdw_table_prefix
  response = Net::HTTP.post URI("#{dataverse_uri}/api/data/v9.2//#{cxdw_table_prefix}claimsqueries"),
                            {
                              "#{cxdw_table_prefix}id" => "#{conversation_id} - #{Time.current}",
                              "#{cxdw_table_prefix}icn" => icn,
                              "#{cxdw_table_prefix}conversationid" => conversation_id,
                              "#{cxdw_table_prefix}requestedtimestamp" => Time.current.to_s
                            }.to_json,
                            {
                              'Content-Type' => 'application/json; charset=utf-8',
                              'OData-MaxVersion' => '4.0',
                              'OData-Version' => '4.0',
                              'If-None-Match' => 'null',
                              'Authorization' => "Bearer #{token}"
                            }
  if response.code != '204'
    raise StandardError, "Errored posting to dataverse with response code #{response.code}"
  else
    response
  end
end