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"]
SCRIPT_ID =
"M7JDg7zmo0Xldo4RTWFGCsI2yotVzKYhk"

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

#build_tests_hash, #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) ⇒ 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.



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

def initialize(mpg=nil,credentials_path,token_path)
  super(mpg)
  self.credentials_path = credentials_path
  self.token_path = token_path
  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?
  AstmServer.log("Initialized Google Lab Interface")
  $service = Google::Apis::ScriptV1::ScriptService.new
  $service.client_options.application_name = APPLICATION_NAME
  $service.authorization = authorize
end

Instance Attribute Details

#credentials_pathObject

Returns the value of attribute credentials_path.



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

def credentials_path
  @credentials_path
end

#token_pathObject

Returns the value of attribute token_path.



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

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



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

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

#pollObject



134
135
136
# File 'lib/publisher/google_lab_interface.rb', line 134

def poll
  super
end

#poll_LIS_for_requisitionObject



64
65
66
67
68
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
# File 'lib/publisher/google_lab_interface.rb', line 64

def poll_LIS_for_requisition
  
  
   
  AstmServer.log("polling LIS for new requisitions")
  
  epoch = (Time.now - 5.days).to_i*1000
  
  pp = {
    :input => JSON.generate([epoch])
  }

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

  begin 
    resp = $service.run_script(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.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/publisher/google_lab_interface.rb', line 103

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(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}")
      #puts "there was an error."
    else
      AstmServer.log("Updating results to LIS successfull")
    end
  rescue => e
    AstmServer.log("Error updating results to LIS, backtrace follows")
    AstmServer.log(e.backtrace.to_s)
  end

end