Class: SPrintClient

Inherits:
Object
  • Object
show all
Defined in:
lib/sprint_client.rb

Overview

Interfaces with SPrint service: github.com/sanger/sprint

Class Method Summary collapse

Class Method Details

.get_template(path) ⇒ Object



56
57
58
# File 'lib/sprint_client.rb', line 56

def self.get_template(path)
  ERB.new File.read(path)
end

.send_post(body) ⇒ Object



49
50
51
52
53
54
# File 'lib/sprint_client.rb', line 49

def self.send_post(body)
  # send POST request to SPrint url and return response
  Net::HTTP.post URI(@@sprint_uri),
                 body.to_json,
                 'Content-Type' => 'application/json'
end

.send_print_request(printer_name, label_template_name, merge_fields_list) ⇒ Object

printer_name - a string showing which printer to send the request to label_template_name - a string to identify which label template to be used in the print request merge_fields_list - a list of hashes, each containing the field values for a particular label

e.g [{ barcode: "DN111111", date: "1-APR-2020" }, { barcode: "DN222222", date: "2-APR-2020" }] would print two labels


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sprint_client.rb', line 15

def self.send_print_request(printer_name, label_template_name, merge_fields_list)
  # define GraphQL print mutation
  query = "mutation Print($printRequest: PrintRequest!, $printer: String!) {
    print(printRequest: $printRequest, printer: $printer) {
      jobId
    }
  }"

  # locate the required label template
  path = get_label_template_path(label_template_name)
  template = get_template(path)

  # parse the template for each label
  layouts = set_layouts(merge_fields_list, template)

  # build the body of the print request
  body = {
    "query": query,
    "variables": {
      "printer": printer_name,
      "printRequest": {
        "layouts": layouts
      }
    }
  }

  send_post(body)

end

.set_layouts(merge_fields_list, template) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/sprint_client.rb', line 60

def self.set_layouts(merge_fields_list, template)
  layouts = []
  merge_fields_list.each do |merge_fields|
    template_array = YAML.load template.result binding
    template_array.each { |ar| layouts << ar }
  end
  layouts
end

.sprint_uri=(sprint_uri) ⇒ Object



45
46
47
# File 'lib/sprint_client.rb', line 45

def self.sprint_uri=(sprint_uri)
  @@sprint_uri = sprint_uri
end