Class: Labimotion::TemplateHub

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/template_hub.rb

Overview

TemplateHub

Constant Summary collapse

TARGET =
Rails.env.production? ? 'https://www.chemotion-repository.net/' : 'http://localhost:3000/'

Class Method Summary collapse

Class Method Details

.fetch_identifier(klass, identifier, origin) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/labimotion/libs/template_hub.rb', line 46

def self.fetch_identifier(klass, identifier, origin)
  body = { klass: klass, identifier: identifier, origin: origin }
  response = HTTParty.post(
    uri('fetch'),
    body: body,
    timeout: 10
  )
  # response.parsed_response if response.code == 200
  JSON.parse(response.body) if response.code == 201
rescue StandardError => e
  Labimotion.log_exception(e)
  error!('Cannot connect to Chemotion Repository', 401)
end

.handle_response(oat, response) ⇒ Object

rubocop: disable Metrics/PerceivedComplexity



26
27
28
29
30
31
32
33
34
# File 'lib/labimotion/libs/template_hub.rb', line 26

def self.handle_response(oat, response) # rubocop: disable Metrics/PerceivedComplexity
  begin
    response&.success? ? 'OK' : 'ERROR'
  rescue StandardError => e
    raise e
  ensure
    ## oat.update(status: response&.success? ? 'done' : 'failure')
  end
end

.header(opt = {}) ⇒ Object



22
23
24
# File 'lib/labimotion/libs/template_hub.rb', line 22

def self.header(opt = {})
  opt || { timeout: 10, headers: { 'Content-Type' => 'text/json' } }
end

.list(klass) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/labimotion/libs/template_hub.rb', line 36

def self.list(klass)
  body = { klass: klass }
  response = HTTParty.get("#{uri('list')}?klass=#{klass}", timeout: 10)
  # response.parsed_response if response.code == 200
  JSON.parse(response.body) if response.code == 200
rescue StandardError => e
  Labimotion.log_exception(e)
  error!('Cannot connect to Chemotion Repository', 401)
end

.uri(api_name) ⇒ Object



16
17
18
19
# File 'lib/labimotion/libs/template_hub.rb', line 16

def self.uri(api_name)
  url = TARGET
  "#{url}api/v1/labimotion_hub/#{api_name}"
end