HEX

Warning: set_time_limit() [function.set-time-limit]: Cannot set time limit - prohibited by configuration in /home/u547966/brikov.ru/www/wp-content/plugins/admin-menu-editor/menu-editor.php on line 745
Server: Apache
System: Linux 4.19.0-0.bpo.9-amd64 x86_64 at red40
User: u547966 (5490)
PHP: 5.3.29-mh2
Disabled: syslog, dl, popen, proc_open, proc_nice, proc_get_status, proc_close, proc_terminate, posix_mkfifo, chown, chgrp, accelerator_reset, opcache_reset, accelerator_get_status, opcache_get_status, pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wifcontinued, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority
Upload Files
File: //var/cache/puppet/lib/puppet_x/cups/queue.rb
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