Class: LabelPrinter::PmbClient
- Inherits:
-
Object
- Object
- LabelPrinter::PmbClient
- Defined in:
- lib/label_printer/pmb_client.rb
Class Method Summary collapse
- .base_url ⇒ Object
- .base_url_v1 ⇒ Object
- .get_label_template_by_name(name) ⇒ Object
- .headers ⇒ Object
- .headers_v1 ⇒ Object
- .label_templates_filter_url ⇒ Object
- .prettify_new_errors(errors) ⇒ Object
- .prettify_old_errors(errors) ⇒ Object
- .pretty_errors(errors) ⇒ Object
- .print(attributes) ⇒ Object
- .print_job_url ⇒ Object
- .printer_exists?(name) ⇒ Boolean
- .printers_filter_url ⇒ Object
- .printers_url ⇒ Object
- .register_printer(name, printer_type) ⇒ Object
Class Method Details
.base_url ⇒ Object
8 9 10 |
# File 'lib/label_printer/pmb_client.rb', line 8 def self.base_url configatron.pmb_api end |
.base_url_v1 ⇒ Object
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 |
.headers ⇒ Object
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_v1 ⇒ Object
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_url ⇒ Object
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 |
.print(attributes) ⇒ Object
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 |
.print_job_url ⇒ Object
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
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_url ⇒ Object
28 29 30 |
# File 'lib/label_printer/pmb_client.rb', line 28 def self.printers_filter_url "#{base_url}/printers?filter[name]=" end |
.printers_url ⇒ Object
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 |