printr 0.1.6 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/printr.rb +47 -65
- data/printr.gemspec +1 -1
- metadata +12 -4
    
        data/lib/printr.rb
    CHANGED
    
    | @@ -4,10 +4,16 @@ require 'erb' | |
| 4 4 | 
             
            require 'serialport'
         | 
| 5 5 | 
             
            module Printr
         | 
| 6 6 | 
             
              require 'printr/engine' if defined?(Rails)
         | 
| 7 | 
            +
              mattr_accessor :debug
         | 
| 8 | 
            +
              @@debug = false
         | 
| 9 | 
            +
              mattr_accessor :serial_baud_rate
         | 
| 10 | 
            +
              @@serial_baud_rate = 9600
         | 
| 7 11 | 
             
              mattr_accessor :scope
         | 
| 8 12 | 
             
              @@scope = 'printr'
         | 
| 9 13 | 
             
              mattr_accessor :printr_source #:yaml or :active_record
         | 
| 10 14 | 
             
              @@printr_source = :yaml
         | 
| 15 | 
            +
              mattr_accessor :logger
         | 
| 16 | 
            +
              @@logger = STDOUT
         | 
| 11 17 | 
             
              # :active_record => {:class_name => ClassName, :name => :model_field_name, :path => :model_path_name
         | 
| 12 18 | 
             
                                   # E.G. printr_model = {:class_name => Printer, :name => :short_name, :path => :location }
         | 
| 13 19 | 
             
                                   # to create the list of printers it will call: 
         | 
| @@ -17,9 +23,8 @@ module Printr | |
| 17 23 | 
             
              @@sanitize_tokens = []
         | 
| 18 24 | 
             
              mattr_accessor :codes
         | 
| 19 25 | 
             
              @@codes = {
         | 
| 20 | 
            -
                  : | 
| 21 | 
            -
                  : | 
| 22 | 
            -
                  :footer => "\n\n\n\n\x1DV\x00\x16\x20105"
         | 
| 26 | 
            +
                  :initialize => "\e@",
         | 
| 27 | 
            +
                  :papercut => "\x1DV\x00"
         | 
| 23 28 | 
             
                }
         | 
| 24 29 | 
             
              mattr_accessor :printrs
         | 
| 25 30 | 
             
              @@printrs = {}
         | 
| @@ -28,6 +33,17 @@ module Printr | |
| 28 33 | 
             
              def self.new
         | 
| 29 34 | 
             
                 return Printr::Machine.new
         | 
| 30 35 | 
             
              end
         | 
| 36 | 
            +
              def self.log(text)
         | 
| 37 | 
            +
                if @@logger == STDOUT
         | 
| 38 | 
            +
                  puts text
         | 
| 39 | 
            +
                else
         | 
| 40 | 
            +
                  if @@logger.respond_to? :info
         | 
| 41 | 
            +
                    @@logger.info text
         | 
| 42 | 
            +
                  else
         | 
| 43 | 
            +
                    puts text
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
              end
         | 
| 31 47 | 
             
              def self.setup
         | 
| 32 48 | 
             
                yield self
         | 
| 33 49 | 
             
              end
         | 
| @@ -46,41 +62,12 @@ module Printr | |
| 46 62 | 
             
              end
         | 
| 47 63 | 
             
              def self.open_printers
         | 
| 48 64 | 
             
                @@conf.each do |key,value|
         | 
| 49 | 
            -
                  key =  | 
| 50 | 
            -
                  puts "[Printr]  Trying to open #{key} at path: #{value}..."
         | 
| 51 | 
            -
                   begin
         | 
| 52 | 
            -
                     @@printrs[key] =  SerialPort.new(value,9600)
         | 
| 53 | 
            -
                     puts "[Printr] Success for SerialPort: #{ @printrs[key].inspect }"
         | 
| 54 | 
            -
                   rescue Exception => e
         | 
| 55 | 
            -
                     puts "[Printr]    Failed to open as SerialPort: #{ e.inspect }"
         | 
| 56 | 
            -
                     @@printrs[key] = nil
         | 
| 57 | 
            -
                   end
         | 
| 58 | 
            -
                   next if @@printrs[key]
         | 
| 59 | 
            -
                   # Try to open it as USB
         | 
| 60 | 
            -
                   begin 
         | 
| 61 | 
            -
                     @@printrs[key] = "ECHO"
         | 
| 62 | 
            -
                     puts "[Printr] opened as usb"
         | 
| 63 | 
            -
                   rescue Errno::EBUSY
         | 
| 64 | 
            -
                     @@printrs[key] = "ECHO"
         | 
| 65 | 
            -
                   rescue Exception => e
         | 
| 66 | 
            -
                     @@printrs[key] = File.open("#{RAILS_ROOT}/tmp/dummy-#{key}.txt","a")
         | 
| 67 | 
            -
                     puts "[Printr]    Failed to open as either SerialPort or USB File. Created Dummy #{ @printrs[key].inspect } instead."
         | 
| 68 | 
            -
                   end 
         | 
| 65 | 
            +
                  @@printrs[key] = value
         | 
| 69 66 | 
             
                 end
         | 
| 70 67 | 
             
                @@printrs
         | 
| 71 68 | 
             
              end 
         | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
                puts "[Printr]CLOSING Printers..."
         | 
| 75 | 
            -
                @@printrs.map do |p| 
         | 
| 76 | 
            -
                  begin
         | 
| 77 | 
            -
                    p.close
         | 
| 78 | 
            -
                  rescue
         | 
| 79 | 
            -
                    
         | 
| 80 | 
            -
                  end
         | 
| 81 | 
            -
                  p = nil
         | 
| 82 | 
            -
                end
         | 
| 83 | 
            -
              end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
             | 
| 84 71 | 
             
              # Instance Methods
         | 
| 85 72 | 
             
              class Machine
         | 
| 86 73 | 
             
                def initialize()
         | 
| @@ -104,44 +91,39 @@ module Printr | |
| 104 91 |  | 
| 105 92 | 
             
                end
         | 
| 106 93 |  | 
| 107 | 
            -
                def logger
         | 
| 108 | 
            -
                  ActiveRecord::Base.logger
         | 
| 109 | 
            -
                end
         | 
| 110 94 | 
             
                def print_to(key,text)
         | 
| 111 95 | 
             
                  key = key.to_sym
         | 
| 112 96 | 
             
                  if text.nil? then
         | 
| 113 | 
            -
                     | 
| 97 | 
            +
                    Printr.log "[Printr] Umm...text is nil dudes..."
         | 
| 114 98 | 
             
                    return
         | 
| 115 99 | 
             
                  end
         | 
| 100 | 
            +
                  text = sanitize(text)
         | 
| 101 | 
            +
                  if text.nil? then
         | 
| 102 | 
            +
                    Printr.log "[Printr] Sanitize nillified the text..."
         | 
| 103 | 
            +
                  end
         | 
| 104 | 
            +
                  Printr.log "[Printr] Going ahead with printing of: " + text.to_s[0..100]
         | 
| 105 | 
            +
             | 
| 106 | 
            +
                  Printr.log "[Printr] Printing to device..." + Printr.conf[key]
         | 
| 116 107 | 
             
                  begin
         | 
| 117 | 
            -
                     | 
| 118 | 
            -
                     | 
| 119 | 
            -
                       | 
| 108 | 
            +
                    Printr.log "[Printr] Trying to open #{key} #{Printr.printrs[key]} as a SerialPort."
         | 
| 109 | 
            +
                    SerialPort.open(Printr.printrs[key],9600) do |sp|
         | 
| 110 | 
            +
                      sp.write text
         | 
| 120 111 | 
             
                    end
         | 
| 121 | 
            -
                     | 
| 122 | 
            -
             | 
| 123 | 
            -
                     | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
                     | 
| 127 | 
            -
             | 
| 128 | 
            -
                      elsif Printr.printrs[key] == 'ECHO' then
         | 
| 129 | 
            -
                        puts "[Printr] Printing to device..." + Printr.conf[key]
         | 
| 130 | 
            -
                        File.open(Printr.conf[key],'w:ISO8859-15') do |f|
         | 
| 131 | 
            -
                          f.write Printr.codes[:header]
         | 
| 132 | 
            -
                          f.write text
         | 
| 133 | 
            -
                          f.write Printr.codes[:footer]
         | 
| 134 | 
            -
                        end
         | 
| 135 | 
            -
                    else
         | 
| 136 | 
            -
                        puts "Could not find #{key} #{Printr.printrs[key].class}"
         | 
| 112 | 
            +
                    return
         | 
| 113 | 
            +
                  rescue Exception => e
         | 
| 114 | 
            +
                    Printr.log "[Printr] Failed to open #{key} #{Printr.printrs[key]} as a SerialPort: #{e.inspect}. Trying as a File instead."
         | 
| 115 | 
            +
                  end
         | 
| 116 | 
            +
                  begin
         | 
| 117 | 
            +
                    File.open(Printr.conf[key],'w:ISO8859-15') do |f|
         | 
| 118 | 
            +
                      f.write text
         | 
| 137 119 | 
             
                    end
         | 
| 138 120 | 
             
                  rescue Exception => e
         | 
| 139 | 
            -
                     | 
| 140 | 
            -
                    Printr.close
         | 
| 121 | 
            +
                    Printr.log "[Printr] Failed to open #{key} #{Printr.printrs[key]} as a File."
         | 
| 141 122 | 
             
                  end
         | 
| 142 123 | 
             
                end
         | 
| 124 | 
            +
             | 
| 143 125 | 
             
                def method_missing(sym, *args, &block)
         | 
| 144 | 
            -
                   | 
| 126 | 
            +
                  Printr.log "[Printr] Called with: #{sym}"
         | 
| 145 127 | 
             
                  if Printr.printrs[sym] then
         | 
| 146 128 | 
             
                    if args[1].class == Binding then
         | 
| 147 129 | 
             
                      print_to(sym,template(args[0],args[1])) #i.e. you call @printr.kitchen('item',binding)
         | 
| @@ -160,19 +142,19 @@ module Printr | |
| 160 142 | 
             
                      end while i < Printr.sanitize_tokens.length
         | 
| 161 143 | 
             
                    end
         | 
| 162 144 | 
             
                  rescue Exception => e
         | 
| 163 | 
            -
                     | 
| 145 | 
            +
                    Printr.log "[Printr] Error in sanittize"
         | 
| 164 146 | 
             
                  end
         | 
| 165 147 | 
             
                  return text
         | 
| 166 148 | 
             
                end
         | 
| 167 149 | 
             
                def template(name,bndng)
         | 
| 168 | 
            -
                   | 
| 150 | 
            +
                  Printr.log "[Printr] attempting to print with template #{RAILS_ROOT}/app/views/#{Printr.scope}/#{name}.prnt.erb"
         | 
| 169 151 | 
             
                  begin
         | 
| 170 152 | 
             
                    erb = ERB.new(File.new("#{RAILS_ROOT}/app/views/#{Printr.scope}/#{name}.prnt.erb",'r').read)
         | 
| 171 153 | 
             
                  rescue Exception => e
         | 
| 172 | 
            -
                     | 
| 154 | 
            +
                    Printr.log "[Printr] Exception in view: " + e.inspect
         | 
| 173 155 |  | 
| 174 156 | 
             
                  end
         | 
| 175 | 
            -
                   | 
| 157 | 
            +
                  Printr.log "[Printr] returning text"
         | 
| 176 158 | 
             
                  text = erb.result(bndng)
         | 
| 177 159 | 
             
                  if text.nil? then
         | 
| 178 160 | 
             
                    text = 'erb result made me nil'
         | 
    
        data/printr.gemspec
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,8 +1,12 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: printr
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              prerelease: 
         | 
| 5 | 
            -
               | 
| 4 | 
            +
              prerelease: false
         | 
| 5 | 
            +
              segments: 
         | 
| 6 | 
            +
              - 0
         | 
| 7 | 
            +
              - 2
         | 
| 8 | 
            +
              - 1
         | 
| 9 | 
            +
              version: 0.2.1
         | 
| 6 10 | 
             
            platform: ruby
         | 
| 7 11 | 
             
            authors: 
         | 
| 8 12 | 
             
            - Michael Franzl
         | 
| @@ -11,7 +15,7 @@ autorequire: | |
| 11 15 | 
             
            bindir: bin
         | 
| 12 16 | 
             
            cert_chain: []
         | 
| 13 17 |  | 
| 14 | 
            -
            date: 2011- | 
| 18 | 
            +
            date: 2011-06-17 00:00:00 +02:00
         | 
| 15 19 | 
             
            default_executable: 
         | 
| 16 20 | 
             
            dependencies: []
         | 
| 17 21 |  | 
| @@ -60,17 +64,21 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 60 64 | 
             
              requirements: 
         | 
| 61 65 | 
             
              - - ">="
         | 
| 62 66 | 
             
                - !ruby/object:Gem::Version 
         | 
| 67 | 
            +
                  segments: 
         | 
| 68 | 
            +
                  - 0
         | 
| 63 69 | 
             
                  version: "0"
         | 
| 64 70 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 65 71 | 
             
              none: false
         | 
| 66 72 | 
             
              requirements: 
         | 
| 67 73 | 
             
              - - ">="
         | 
| 68 74 | 
             
                - !ruby/object:Gem::Version 
         | 
| 75 | 
            +
                  segments: 
         | 
| 76 | 
            +
                  - 0
         | 
| 69 77 | 
             
                  version: "0"
         | 
| 70 78 | 
             
            requirements: []
         | 
| 71 79 |  | 
| 72 80 | 
             
            rubyforge_project: printr
         | 
| 73 | 
            -
            rubygems_version: 1. | 
| 81 | 
            +
            rubygems_version: 1.3.7
         | 
| 74 82 | 
             
            signing_key: 
         | 
| 75 83 | 
             
            specification_version: 3
         | 
| 76 84 | 
             
            summary: An engine for interfacing with printers.
         |