Class: PlateBarcode

Inherits:
Object
  • Object
show all
Extended by:
Dev::PlateBarcode::CacheBarcodes
Defined in:
app/models/plate_barcode.rb

Overview

Class that handles the access to Baracoda to obtain new barcodes

Constant Summary

Constants included from Dev::PlateBarcode::CacheBarcodes

Dev::PlateBarcode::CacheBarcodes::MAX_SIZE_CACHE

Class Method Summary collapse

Methods included from Dev::PlateBarcode::CacheBarcodes

barcode_in_cache?, cache_barcode, data_cache, dev_cache_get_next_barcode, reset_cache, resize_cache

Class Method Details

._connection_scope(url, data = nil) ⇒ Object

Creates required objects to perform a call to the server so they can be reused inside the scope. Args: - url: string that contains the url to connect - data: object that we will POST to the url. If not defined it will be an empty POST. Yields: - http_connection - Net::HTTP instance to connect to the host/port - request - Net::HTTP::Post object that contains the params for the request like the body, headers, etc



93
94
95
96
97
98
99
100
101
# File 'app/models/plate_barcode.rb', line 93

def self._connection_scope(url, data = nil)
  uri = URI(url)
  initheader = { 'Content-Type' => 'application/json' }
  request = Net::HTTP::Post.new(uri.path, initheader)
  request.body = data.to_json if data
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http_connection|
    yield http_connection, request
  end
end

._retries_scope(retries, wait_timeout) ⇒ Object

Retries the number of times specified before calling the block. It sleeps wait_timeout in between calls except with the last call. Args: - retries - int, Number of times it will retry to use the block - wait_timeout - float. Time sleep in between calls to baracoda when connection is refused Yields: Nothing



73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/plate_barcode.rb', line 73

def self._retries_scope(retries, wait_timeout)
  while retries.positive?
    begin
      yield
    rescue Errno::ECONNREFUSED
      Rails.logger.error('Failed connection to Baracoda')
    end
    sleep(wait_timeout) if retries >= 1
    retries -= 1
  end
end

.create_barcodeObject

Creates a new single barcode in baracoda Returns: - Barcode instance, using Sequencescape22 format



16
17
18
19
# File 'app/models/plate_barcode.rb', line 16

def self.create_barcode
  response = fetch_response("#{site}/barcodes/#{prefix}/new")
  Barcode.build_sequencescape22(response)
end

.create_barcode_with_text(text) ⇒ Object

Creates a new single barcode with a code text in baracoda Arguments: text - str with the code of up to 3 characters that will be appended after the prefix Returns: - Barcode instance, using Sequencescape22 format



27
28
29
30
# File 'app/models/plate_barcode.rb', line 27

def self.create_barcode_with_text(text)
  response = fetch_response("#{site}/barcodes/#{prefix}/new", { text: })
  Barcode.build_sequencescape22(response)
end

.create_child_barcodes(parent_barcode, count = 1) ⇒ Object

Creates a new group of child barcodes from a parent barcode. Args: - parent_barcode - String with the barcode we want to create children from - count - Number of children to create Returns: - Barcode instance, using Sequencescape22 format



38
39
40
41
# File 'app/models/plate_barcode.rb', line 38

def self.create_child_barcodes(parent_barcode, count = 1)
  response = fetch_response("#{site}/child-barcodes/#{prefix}/new", { barcode: parent_barcode, count: count })
  response[:barcodes_group][:barcodes].map! { |barcode| Barcode.build_sequencescape22(barcode:) }
end

.fetch_response(url, data = nil, retries = 3, wait_timeout = 0.1) ⇒ Object

Obtain a record from Baracoda and retries the specified amount of time. If the number or retries is reached the method will raise an exception Args: - http_connection - Net::HTTP instance to connect to the host/port - request - Net::HTTP::Post object that contains the params for the request like the body, headers, etc - retries - int, defaults to 3. Number of times it will retry to call baracoda. After that time period it will raise exception - wait_timeout - float, defaults to 0.1 (100 ms). Time sleep in between calls to baracoda when connection is refused Returns: - Json parsed hash with the response from Baracoda - If no answers is obtained, it raises an exception



55
56
57
58
59
60
61
62
63
64
# File 'app/models/plate_barcode.rb', line 55

def self.fetch_response(url, data = nil, retries = 3, wait_timeout = 0.1)
  _connection_scope(url, data) do |http_connection, request|
    # Baracoda has a drop out bug, until this is fixed we need to retry a few times
    _retries_scope(retries, wait_timeout) do
      response = http_connection.request(request)
      return JSON.parse(response.body, symbolize_names: true) if response.code == '201'
    end
  end
  raise 'Could not obtain a barcode from Baracoda'
end

.prefixObject



9
10
11
# File 'app/models/plate_barcode.rb', line 9

def self.prefix
  configatron.plate_barcode_prefix
end

.siteObject



5
6
7
# File 'app/models/plate_barcode.rb', line 5

def self.site
  configatron.baracoda_api
end