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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/labimotion/libs/template_hub.rb', line 42

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



22
23
24
25
26
27
28
29
30
# File 'lib/labimotion/libs/template_hub.rb', line 22

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

.header(opt = {}) ⇒ Object



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

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

.list(klass) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/labimotion/libs/template_hub.rb', line 32

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

.send_to_central_hub(klass, template, metadata, origin) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/labimotion/libs/template_hub.rb', line 56

def self.send_to_central_hub(klass, template, , origin)
  body = {
    template_klass: klass,
    template: template,
    metadata: ,
    origin: origin
  }
  response = HTTParty.post(
    Labimotion::TemplateHub.uri('template_submissions'),
    headers: {
      'Content-Type' => 'application/json',
      'X-Origin-URL' => origin
    },
    body: body.to_json,
    timeout: 10
  )

  if [200, 201].include?(response.code)
    parsed_response = JSON.parse(response.body)
    return { mc: 'ss00', data: { id: parsed_response['id'] } }
  end
  { mc: 'se00', msg: "HTTP #{response.code}: #{response.message}", data: {} }
rescue StandardError => e
  Labimotion.log_exception(e)
  { mc: 'se00', msg: "Connection failure: #{e.message}", data: {} }
end

.uri(api_name) ⇒ Object



13
14
15
16
# File 'lib/labimotion/libs/template_hub.rb', line 13

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