Class: Google_Lab_Interface

Inherits:
Poller
  • Object
show all
Defined in:
lib/publisher/google_lab_interface.rb

Constant Summary collapse

OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME =
'Google Apps Script API Ruby Quickstart'.freeze
SCOPE =

these two cannot be hardcoded. CREDENTIALS_PATH = ‘credentials.json’.freeze TOKEN_PATH = ‘token.yaml’.freeze

'https://www.googleapis.com/auth/script.projects'.freeze
SCOPES =
["https://www.googleapis.com/auth/documents","https://www.googleapis.com/auth/drive","https://www.googleapis.com/auth/script.projects","https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/script.external_request"]

Constants inherited from Poller

Poller::COMPLETED, Poller::EDTA, Poller::ESR, Poller::FLUORIDE, Poller::LAST_REQUEST_AT, Poller::LAST_REQUEST_STATUS, Poller::PLASMA, Poller::POLL_STATUS_KEY, Poller::REQUISITIONS_HASH, Poller::REQUISITIONS_SORTED_SET, Poller::RUNNING, Poller::SERUM, Poller::URINE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Poller

#assign_tube, #build_tests_hash, #default_checkpoint, #determine_tube, #get_checkpoint, #merge_with_requisitions_hash, #post_poll_LIS, #pre_poll_LIS, #prepare_redis, #process_LIS_response, #root_path, #update_LIS

Constructor Details

#initialize(mpg = nil, credentials_path, token_path, script_id, real_time_db) ⇒ Google_Lab_Interface

@param mpg : path to mappings file. Defaults to nil. @param credentials_path : the path to look for the credentials.json file, defaults to nil ,and will raise an error unless provided @param token_path : the path where the oauth token will be stored, also defaults to the path of the gem : eg. ./token.yaml - be careful with write permissions, because token.yaml gets written to this path after the first authorization.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/publisher/google_lab_interface.rb', line 51

def initialize(mpg=nil,credentials_path,token_path,script_id,real_time_db)
  super(mpg,real_time_db)
  self.credentials_path = credentials_path
  self.token_path = token_path
  self.script_id = script_id
  raise "Please provide the full path of the google oauth credentials.json file. If you don't have this file, please go to the Apps Script project, which has your google apps script, and Choose Create Credentials -> help me choose -> and use 'Calling Scripts Api from a UI based platform'. Also ensure that your script has permissions set for Drive, Sheets, and more. Lastly in the Apps script project ensure that settings -> google apps script API is ON." if self.credentials_path.nil?
  raise "Please provide a script id for your google script" if self.script_id.blank?
  AstmServer.log("Initialized Google Lab Interface")
  $service = Google::Apis::ScriptV1::ScriptService.new
  $service.client_options.application_name = APPLICATION_NAME
  $service.client_options.send_timeout_sec = 1200
  $service.client_options.open_timeout_sec = 1200
  $service.request_options.retries = 3
  $service.authorization = authorize
end

Instance Attribute Details

#credentials_pathObject

Returns the value of attribute credentials_path.



20
21
22
# File 'lib/publisher/google_lab_interface.rb', line 20

def credentials_path
  @credentials_path
end

#script_idObject

Returns the value of attribute script_id.



22
23
24
# File 'lib/publisher/google_lab_interface.rb', line 22

def script_id
  @script_id
end

#token_pathObject

Returns the value of attribute token_path.



21
22
23
# File 'lib/publisher/google_lab_interface.rb', line 21

def token_path
  @token_path
end

Instance Method Details

#authorizeGoogle::Auth::UserRefreshCredentials

Ensure valid credentials, either by restoring from the saved credentials files or intitiating an OAuth2 authorization. If authorization is required, the user’s default browser will be launched to approve the request.

Returns:

  • (Google::Auth::UserRefreshCredentials)

    OAuth2 credentials



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/publisher/google_lab_interface.rb', line 30

def authorize
  client_id = Google::Auth::ClientId.from_file(self.credentials_path)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: self.token_path)
  authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPES, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  if credentials.nil?
    url = authorizer.get_authorization_url(base_url: OOB_URI)
    puts 'Open the following URL in the browser and enter the ' \
         "resulting code after authorization:\n" + url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end

#notify_patientsObject

sends emails of report pdfs to patients



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/publisher/google_lab_interface.rb', line 145

def notify_patients
  request = Google::Apis::ScriptV1::ExecutionRequest.new(
    function: 'process_email_log'
  )

  begin
    AstmServer.log("Processing Email Log")
    AstmServer.log(request.parameters.to_s)
    resp = $service.run_script(self.script_id, request)
    if resp.error
      AstmServer.log("Error Processing Email Log, message follows")
      AstmServer.log("error: #{resp.error.message} : code: #{resp.error.code}")
      false
    else
      AstmServer.log("Email log processing successfull")
      true
    end
  rescue => e
    AstmServer.log("Error processing email log, backtrace follows")
    AstmServer.log(e.backtrace.to_s)
    false
  end
end

#pollObject



169
170
171
172
173
174
175
# File 'lib/publisher/google_lab_interface.rb', line 169

def poll
    pre_poll_LIS
    poll_LIS_for_requisition
    update_LIS
    notify_patients
    post_poll_LIS
end

#poll_LIS_for_requisitionObject

how to decide for what to poll for the requisition. this should be tested.



69
70
71
72
73
74
75
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
# File 'lib/publisher/google_lab_interface.rb', line 69

def poll_LIS_for_requisition
  
  AstmServer.log("polling LIS for new requisitions")
  
  epoch = get_checkpoint
  
  pp = {
    :input => JSON.generate([epoch])
  }



  request = Google::Apis::ScriptV1::ExecutionRequest.new(
    function: 'get_latest_test_information',
    parameters: pp
  )

  puts "params are: #{pp}"

  #begin 
    resp = $service.run_script(self.script_id, request)
    if resp.error
      AstmServer.log("Response Error polling LIS for requisitions: #{resp.error.message}: #{resp.error.code}")
    else
      process_LIS_response(resp.response["result"])
      AstmServer.log("Successfully polled lis for requisitions: #{resp.response}")
    end
  #rescue => e
    #AstmServer.log("Rescue Error polling LIS for requisitions: #{e.to_s}")
    #AstmServer.log("Error backtrace")
    #AstmServer.log(e.backtrace.to_s)
  #ensure
    
  #end

end

#update(data) ⇒ Object

method overriden from adapter. data should be an array of objects. see adapter for the recommended structure. @return true/false : depending on if there was an error or not.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/publisher/google_lab_interface.rb', line 111

def update(data)

  orders = JSON.generate(data)

  pp = {
    :input => orders
  }

  request = Google::Apis::ScriptV1::ExecutionRequest.new(
    function: 'update_report',
    parameters: pp
  )

  begin
    AstmServer.log("updating following results to LIS")
    AstmServer.log(request.parameters.to_s)
    resp = $service.run_script(self.script_id, request)
    if resp.error
      AstmServer.log("Error updating results to LIS, message follows")
      AstmServer.log("error: #{resp.error.message} : code: #{resp.error.code}")
      false
    else
      AstmServer.log("Updating results to LIS successfull")
      true
    end
  rescue => e
    AstmServer.log("Error updating results to LIS, backtrace follows")
    AstmServer.log(e.backtrace.to_s)
    false
  end

end