Class: Labimotion::Libs::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) ⇒ Object



51
52
53
54
55
56
# File 'lib/labimotion/libs/template_hub.rb', line 51

def self.fetch_identifier(klass, identifier)
  # body = { klass: klass, identifier: identifier }
  response = HTTParty.get("#{uri('fetch')}?klass=#{klass}&identifier=#{identifier}")
  # response.parsed_response if response.code == 200
  JSON.parse(response.body) if response.code == 200
end

.handle_response(oat, response) ⇒ Object

rubocop: disable Metrics/PerceivedComplexity



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

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



30
31
32
# File 'lib/labimotion/libs/template_hub.rb', line 30

def self.header(opt = {})
  opt || {}
end

.list(klass) ⇒ Object



44
45
46
47
48
49
# File 'lib/labimotion/libs/template_hub.rb', line 44

def self.list(klass)
  body = { klass: klass }
  response = HTTParty.get("#{uri('list')}?klass=#{klass}")
  # response.parsed_response if response.code == 200
  JSON.parse(response.body) if response.code == 200
end

.loggerObject



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

def self.logger
  @@converter_logger ||= Logger.new(Rails.root.join('log/labimotion.log')) # rubocop:disable Style/ClassVars
end

.process(data) ⇒ Object



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
82
83
# File 'lib/labimotion/libs/template_hub.rb', line 57

def self.process(data)
  response = nil
  begin
    ofile = Rails.root.join(data[:f], data[:a].filename)
    FileUtils.cp(data[:a].attachment_url, ofile)
    File.open(ofile, 'r') do |f|
      body = { file: f }

      response = HTTParty.post(
        uri('conversions'),
        basic_auth: auth,
        body: body,
        timeout: timeout,
      )
    end
    if response.ok?
      Labimotion::Libs::Converter.handle_response(data[:a], response)
    else
      Labimotion::Libs::Converter.logger.error ["Converter Response Error: id: [#{data[:a]&.id}], filename: [#{data[:a]&.filename}], response: #{response}"].join($INPUT_RECORD_SEPARATOR)
    end
    response
  rescue StandardError => e
    raise e
  ensure
    FileUtils.rm_f(ofile)
  end
end

.timeoutObject



26
27
28
# File 'lib/labimotion/libs/template_hub.rb', line 26

def self.timeout
  Rails.configuration.try(:converter).try(:timeout) || 30
end

.uri(api_name) ⇒ Object



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

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