Class: Printr::Machine

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

Overview

Instance Methods

Instance Method Summary collapse

Constructor Details

#initializeMachine

Returns a new instance of Machine.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/printr.rb', line 85

def initialize()
  Printr.get_printers
  # You can access these within the views by calling @printr.codes, or whatever
  # you named the instance variable, as it will be snagged by the Binding
  @codes = Printr.codes
  
  # You can override the above codes in the printers.yml, to add
  # say an ASCII header or some nonsense, or if they are using a
  # standard printer etc etc.
  if Printr.conf[:codes] then
    Printr.conf[:codes].each do |key,value|
      Printr.conf[key.to_sym] = value
    end
  end
  Printr.open_printers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/printr.rb', line 140

def method_missing(sym, *args, &block)
  Printr.log "[Printr] Called with: #{sym}"
  if Printr.printrs[sym] then
    if args[1].class == Binding then
      Printr.log "Binding was passed"
      print_to(sym,template(args[0],args[1])) #i.e. you call @printr.kitchen('item',binding)
    else
      Printr.log "No Binding was passed"
      print_to(sym,args[0])
    end
  end
end

Instance Method Details



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/printr.rb', line 106

def print_to(key,text)
  Printr.log "[Printr] print_to(#{key},#{text[0..55]})"
  key = key.to_sym
  if text.nil? then
    Printr.log "[Printr] Umm...text is nil dudes..."
    return
  end
  text = sanitize(text)
  if text.nil? then
    Printr.log "[Printr] Sanitize nillified the text..."
  end
  Printr.log "[Printr] Going ahead with printing of: " + text.to_s[0..55]

  Printr.log "[Printr] Printing to device..." + Printr.conf[key]
  begin
    Printr.log "[Printr] Trying to open #{key} #{Printr.printrs[key]} as a SerialPort."
    SerialPort.open(Printr.printrs[key],9600) do |sp|
      sp.write text
    end
    return
  rescue Exception => e
    Printr.log "[Printr] Failed to open #{key} #{Printr.printrs[key]} as a SerialPort: #{e.inspect}. Trying as a File instead."
  end
  begin
    File.open(Printr.conf[key],'w:ISO8859-15') do |f|
      Printr.log "[Printr] Writing text."
      text.force_encoding 'ISO-8859-15'
      f.write text
    end
  rescue Exception => e
    Printr.log "[Printr] Failed to open #{key} #{Printr.printrs[key]} as a File."
  end
end

#sane_template(name, bndng) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/printr.rb', line 186

def sane_template(name,bndng)
  Printr.log "[Printr] attempting to print with template #{RAILS_ROOT}/app/views/#{Printr.scope}/#{name}.prnt.erb"
  begin
    erb = ERB.new(File.new("#{RAILS_ROOT}/app/views/#{Printr.scope}/#{name}.prnt.erb",'r').read,0,'>')
  rescue Exception => e
    Printr.log "[Printr] Exception in view: " + $!.inspect
  end
  Printr.log "[Printr] returning text"
  text = erb.result(bndng)
  if text.nil? then
    text = 'erb result made me nil'
  end
  return sanitize(text)
end

#sanitize(text) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/printr.rb', line 153

def sanitize(text)
  # Printr.sanitize_tokens is a pair array, that is index 0 is the needle and 1 is the replace, 2 is the needle
  # 3 the replacement etc. [needle,replace,needle,replace,needle,replace]
  Printr.log "sanitize(#{text[0..55]})"
  Printr.log "Forcing encoding to: " + Printr.encoding # Printr.encoding can be set in initializer with config.encoding = ISO-8859-15 etc
  begin 
    text.encode! Printr.encoding; 
  rescue
    Printr.log "Failed to encode with #{Printr.encoding}, trying UTF-8"
    begin text.encode! "UTF-8"; rescue; end  
  end
  char = ['ä', 'ü', 'ö', 'Ä', 'Ü', 'Ö', 'é', 'è', 'ú', 'ù', 'á', 'à', 'í', 'ì', 'ó', 'ò', 'â', 'ê', 'î', 'ô', 'û', 'ñ', 'ß']
  replacement = ["\x84", "\x81", "\x94", "\x8E", "\x9A", "\x99", "\x82", "\x8A", "\xA3", "\x97", "\xA0", "\x85", "\xA1", "\x8D", "\xA2", "\x95", "\x83", "\x88", "\x8C", "\x93", "\x96", "\xA4", "\xE1"]
  i = 0
  Printr.log "Adding some tokens to the sanitize array"
  begin
    rx = Regexp.new(char[i].encode(Printr.encoding))
    rep = replacement[i].force_encoding(Printr.encoding)
    Printr.sanitize_tokens << rx
    Printr.sanitize_tokens << rep
    i += 1
  end while i < char.length
  i = 0
  begin
    rx = Printr.sanitize_tokens[i]
    rep = Printr.sanitize_tokens[i+1]
    Printr.log "Replacing: " + rx.to_s + " with " + rep.to_s
    begin text.gsub!(rx, rep); rescue; end
    i += 2
  end while i < Printr.sanitize_tokens.length
  return text
end

#template(name, bndng) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/printr.rb', line 200

def template(name,bndng)
  Printr.log "[Printr] attempting to print with template #{RAILS_ROOT}/app/views/#{Printr.scope}/#{name}.prnt.erb"
  begin
    erb = ERB.new(File.new("#{RAILS_ROOT}/app/views/#{Printr.scope}/#{name}.prnt.erb",'r').read,0,'>')
  rescue Exception => e
    Printr.log "[Printr] Exception in view: " + $!.inspect
  end
  Printr.log "[Printr] returning text"
  text = erb.result(bndng)
  if text.nil? then
    text = 'erb result made me nil'
  end
  return text
end

#test(key) ⇒ Object



102
103
104
# File 'lib/printr.rb', line 102

def test(key)
  
end