Class: LabelPrinter::PmbClient

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

Class Method Summary collapse

Class Method Details

.base_urlObject


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

def self.base_url
  configatron.pmb_api
end

.get_label_template_by_name(name) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/label_printer/label_printer/pmb_client.rb', line 43

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


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

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

.label_templates_filter_urlObject


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

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

.prettify_new_errors(errors) ⇒ Object


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

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

.prettify_old_errors(errors) ⇒ Object


87
88
89
90
91
92
93
94
# File 'lib/label_printer/label_printer/pmb_client.rb', line 87

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

.pretty_errors(errors) ⇒ Object


66
67
68
69
70
71
72
73
74
75
# File 'lib/label_printer/label_printer/pmb_client.rb', line 66

def self.pretty_errors(errors)
  if errors.present?
    parsed_errors = JSON.parse(errors)['errors']
    if parsed_errors.is_a? Array
      prettify_new_errors(parsed_errors)
    elsif parsed_errors.is_a? Hash
      prettify_old_errors(parsed_errors)
    end
  end
end

31
32
33
34
35
36
37
38
39
40
41
# File 'lib/label_printer/label_printer/pmb_client.rb', line 31

def self.print(attributes)
  RestClient.post print_job_url, { 'data' => { 'attributes' => 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

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

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

.printer_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)

61
62
63
64
# File 'lib/label_printer/label_printer/pmb_client.rb', line 61

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

.printers_filter_urlObject


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

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

.printers_urlObject


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

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

.register_printer(name) ⇒ Object


55
56
57
58
59
# File 'lib/label_printer/label_printer/pmb_client.rb', line 55

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