This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
queue.rb
100 lines (90 loc) · 2.69 KB
/
queue.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'erb'
require_relative 'ipp'
module PuppetX
module Cups
# Namespace encapsulating helpers to query the CUPS server for print queue attributes
#
# @author Leo Arnold
# @since 1.0.0
module Queue
# Retrieves the value of a queue attribute
#
# @author Leo Arnold
# @since 2.0.0
#
# @param queue [String] The name of a queue
# @param property [String] The name of a queue attribute
#
# @return [String] The value of the requested queue attribute
def self.attribute(queue, property)
attribute = Attribute.new(queue, property)
attribute.value
end
# A wrapper class for queue attribute queries
#
# @author Leo Arnold
# @since 2.0.0
class Attribute
# Returns a new instance of Attribute
#
# @author Leo Arnold
# @since 2.0.0
#
# @param queue [String] The name of the queue
# @param name [String] The name of the attribute
def initialize(queue, name)
@queue = queue
@name = name
end
# Executes the query and returns the attribute value
# without surrounding quotes, or an empty string
# if the query did not return a value
#
# @author Leo Arnold
# @since 2.0.0
#
# @return [String] The attribute value (if any) without surrounding quotes, or an empty string
def value
query = PuppetX::Cups::Ipp.query(resource, request)
answer = query.results.empty? ? '' : query.results.first
answer.gsub(/\A"|"\Z/, '')
end
private
# @private
#
# Encodes the queue name and returns the resource to query
#
# @author Leo Arnold
# @since 2.0.0
#
# @return [String] The resource to query
def resource
"/printers/#{ERB::Util.url_encode(@queue)}"
end
# @private
#
# The IPP request required to retrieve the value of the requested attribute
#
# @see https://www.cups.org/doc/spec-ipp.html
#
# @author Leo Arnold
# @since 1.0.0
#
# @return [String] IPP `Get-Printer-Attributes` request to display the value of the requested attribute
def request
<<-REQUEST
{
OPERATION Get-Printer-Attributes
GROUP operation
ATTR charset attributes-charset utf-8
ATTR language attributes-natural-language en
ATTR uri printer-uri $uri
STATUS successful-ok
DISPLAY #{@name}
}
REQUEST
end
end
end
end
end