Class: LabelPrinter::PmbClient

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

Class Method Summary collapse

Class Method Details

.base_urlObject



8
9
10
# File 'lib/label_printer/pmb_client.rb', line 8

def self.base_url
  configatron.pmb_api
end

.base_url_v1Object



12
13
14
# File 'lib/label_printer/pmb_client.rb', line 12

def self.base_url_v1
  configatron.pmb_api_v1
end

.get_label_template_by_name(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/label_printer/pmb_client.rb', line 52

def self.get_label_template_by_name(name)
  JSON.parse(RestClient.get("#{label_templates_filter_url}#{name}", headers))
rescue RestClient::UnprocessableEntity => e
  raise PmbException.new(e), pretty_errors(e.response)
rescue RestClient::InternalServerError => e
  raise PmbException.new(e), 'something went wrong'
rescue RestClient::ServiceUnavailable => e
  raise PmbException.new(e), 'is too busy. Please try again later'
rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL, RestClient::BadGateway => e
  raise PmbException.new(e), 'service is down'
end

.headersObject



36
37
38
# File 'lib/label_printer/pmb_client.rb', line 36

def self.headers
  { content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json' }
end

.headers_v1Object



32
33
34
# File 'lib/label_printer/pmb_client.rb', line 32

def self.headers_v1
  { content_type: 'application/json', accept: 'application/json' }
end

.label_templates_filter_urlObject



24
25
26
# File 'lib/label_printer/pmb_client.rb', line 24

def self.label_templates_filter_url
  "#{base_url}/label_templates?filter[name]="
end

.prettify_new_errors(errors) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/label_printer/pmb_client.rb', line 89

def self.prettify_new_errors(errors)
  [].tap do |error_list|
      errors.each do |error|
        attribute = error['source']['pointer'].split('/').last.humanize
        error_list << (format('%<attribute>s %<message>s', attribute: attribute, message: error['detail']))
      end
    end
    .join('; ')
end

.prettify_old_errors(errors) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/label_printer/pmb_client.rb', line 99

def self.prettify_old_errors(errors)
  [].tap do |error_list|
      errors.each do |k, v|
        error_list << (format('%<attribute>s %<message>s', attribute: "#{k.capitalize}:", message: v.join(', ')))
      end
    end
    .join('; ')
end

.pretty_errors(errors) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/label_printer/pmb_client.rb', line 78

def self.pretty_errors(errors)
  return if errors.blank?
  parsed_errors = JSON.parse(errors)['errors']
  case parsed_errors
  when Array
    prettify_new_errors(parsed_errors)
  when Hash
    prettify_old_errors(parsed_errors)
  end
end


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/label_printer/pmb_client.rb', line 40

def self.print(attributes)
  RestClient.post print_job_url, { 'print_job' => attributes }.to_json, headers
rescue RestClient::UnprocessableEntity => e
  raise PmbException.new(e), pretty_errors(e.response)
rescue RestClient::InternalServerError => e
  raise PmbException.new(e), ': something went wrong'
rescue RestClient::ServiceUnavailable => e
  raise PmbException.new(e), 'is too busy. Please try again later'
rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL, RestClient::BadGateway => e
  raise PmbException.new(e), 'service is down'
end


16
17
18
# File 'lib/label_printer/pmb_client.rb', line 16

def self.print_job_url
  "#{base_url}/print_jobs"
end

.printer_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/label_printer/pmb_client.rb', line 73

def self.printer_exists?(name)
  response = JSON.parse(RestClient.get("#{printers_filter_url}#{name}", **headers))
  response['data'].present?
end

.printers_filter_urlObject



28
29
30
# File 'lib/label_printer/pmb_client.rb', line 28

def self.printers_filter_url
  "#{base_url}/printers?filter[name]="
end

.printers_urlObject



20
21
22
# File 'lib/label_printer/pmb_client.rb', line 20

def self.printers_url
  "#{base_url}/printers"
end

.register_printer(name, printer_type) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/label_printer/pmb_client.rb', line 64

def self.register_printer(name, printer_type)
  return if printer_exists?(name)
  RestClient.post(
    printers_url,
    { 'data' => { 'attributes' => { 'name' => name, 'printer_type' => printer_type } } }.to_json,
    **headers
  )
end