rescpos 0.0.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/.gitignore +3 -0
- data/.rspec +3 -0
- data/.rvmrc +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +52 -0
- data/Guardfile +17 -0
- data/README.textile +75 -0
- data/Rakefile +9 -0
- data/examples/escposes/dish_item.escpos.erb +10 -0
- data/examples/print.rb +16 -0
- data/examples/reports/dish_item_report.rb +24 -0
- data/features/printer.feature +4 -0
- data/features/render_report.feature +38 -0
- data/features/step_definitions/.gitkeep +0 -0
- data/features/step_definitions/common_steps.rb +3 -0
- data/features/step_definitions/printer_steps.rb +7 -0
- data/features/step_definitions/report_steps.rb +25 -0
- data/features/support/env.rb +1 -0
- data/lib/core_ext/string.rb +11 -0
- data/lib/rescpos/configuration.rb +11 -0
- data/lib/rescpos/printer.rb +40 -0
- data/lib/rescpos/report.rb +16 -0
- data/lib/rescpos/report_util.rb +69 -0
- data/lib/rescpos/version.rb +3 -0
- data/lib/rescpos.rb +21 -0
- data/rescpos.gemspec +24 -0
- data/spec/lib/core_ext/string_spec.rb +7 -0
- data/spec/lib/rescpos/.gitkeep +0 -0
- data/spec/lib/rescpos/configuration_spec.rb +21 -0
- data/spec/lib/rescpos/printer_spec.rb +44 -0
- data/spec/lib/rescpos/report_spec.rb +31 -0
- data/spec/lib/rescpos/report_util_spec.rb +54 -0
- data/spec/reports/titled.escpos.erb +1 -0
- data/spec/spec_helper.rb +6 -0
- metadata +79 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.2@rescpos
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rescpos (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
builder (3.0.0)
|
10
|
+
cucumber (1.0.2)
|
11
|
+
builder (>= 2.1.2)
|
12
|
+
diff-lcs (>= 1.1.2)
|
13
|
+
gherkin (~> 2.4.5)
|
14
|
+
json (>= 1.4.6)
|
15
|
+
term-ansicolor (>= 1.0.5)
|
16
|
+
diff-lcs (1.1.2)
|
17
|
+
gherkin (2.4.16)
|
18
|
+
json (>= 1.4.6)
|
19
|
+
guard (0.6.2)
|
20
|
+
thor (~> 0.14.6)
|
21
|
+
guard-bundler (0.1.3)
|
22
|
+
bundler (>= 1.0.0)
|
23
|
+
guard (>= 0.2.2)
|
24
|
+
guard-cucumber (0.6.1)
|
25
|
+
cucumber (>= 0.10)
|
26
|
+
guard (>= 0.4.0)
|
27
|
+
guard-rspec (0.4.3)
|
28
|
+
guard (>= 0.4.0)
|
29
|
+
json (1.5.3)
|
30
|
+
rspec (2.6.0)
|
31
|
+
rspec-core (~> 2.6.0)
|
32
|
+
rspec-expectations (~> 2.6.0)
|
33
|
+
rspec-mocks (~> 2.6.0)
|
34
|
+
rspec-core (2.6.4)
|
35
|
+
rspec-expectations (2.6.0)
|
36
|
+
diff-lcs (~> 1.1.2)
|
37
|
+
rspec-instafail (0.1.8)
|
38
|
+
rspec-mocks (2.6.0)
|
39
|
+
term-ansicolor (1.0.6)
|
40
|
+
thor (0.14.6)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
ruby
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
cucumber
|
47
|
+
guard-bundler
|
48
|
+
guard-cucumber
|
49
|
+
guard-rspec
|
50
|
+
rescpos!
|
51
|
+
rspec
|
52
|
+
rspec-instafail
|
data/Guardfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
group 'rescpos' do
|
2
|
+
guard 'bundler' do
|
3
|
+
watch('Gemfile')
|
4
|
+
end
|
5
|
+
|
6
|
+
guard 'cucumber' do
|
7
|
+
watch(%r{^features/.+\.feature$})
|
8
|
+
watch(%r{^features/support/.+$}) { 'features' }
|
9
|
+
watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
|
10
|
+
end
|
11
|
+
|
12
|
+
guard 'rspec' do
|
13
|
+
watch(%r{^spec/.+_spec\.rb$})
|
14
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
15
|
+
watch('spec/spec_helper.rb') { "spec" }
|
16
|
+
end
|
17
|
+
end
|
data/README.textile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
h1. Intro
|
2
|
+
|
3
|
+
A ruby implementation of the Line Thermal Printer ESC/POS Command Specifications.
|
4
|
+
|
5
|
+
h1. Install
|
6
|
+
|
7
|
+
gem install rescpos
|
8
|
+
|
9
|
+
h1. Usage
|
10
|
+
|
11
|
+
See "examples":https://github.com/towerhe/rescpos/tree/develop/examples
|
12
|
+
|
13
|
+
Specify the template path:
|
14
|
+
|
15
|
+
bc. Rescpos.configure do |config|
|
16
|
+
config.template_path = "path/to/your/escpos/templates"
|
17
|
+
end
|
18
|
+
|
19
|
+
For rails application, the default template path is set to app/escposes. So you should create the templates under app/escposes directory. But if you want to save the templates into another place, you should config the template path in a rails initializer.
|
20
|
+
|
21
|
+
h2. Reports
|
22
|
+
|
23
|
+
Reports manage the objects that will be used in the templates. You should subclass the Rescpos::Report class to define your own reports. For example:
|
24
|
+
|
25
|
+
<pre>
|
26
|
+
class SimpleTitledReport < Rescpos::Report
|
27
|
+
attr_reader :title
|
28
|
+
|
29
|
+
def initialize(title = '')
|
30
|
+
@title = title
|
31
|
+
end
|
32
|
+
end
|
33
|
+
</pre>
|
34
|
+
|
35
|
+
To render the report, you just need to call the render method of the report.
|
36
|
+
|
37
|
+
bc. report = SimpleTitledReport.new('A report with a title')
|
38
|
+
report.render(:template => "<%= @title %>")
|
39
|
+
# A report with a title
|
40
|
+
|
41
|
+
You can also create a pre-defined template for the report.
|
42
|
+
|
43
|
+
bc. report.render(:file => 'path/to/the/template')
|
44
|
+
|
45
|
+
Rescpos will search for the template with the underscore name of the report under the template path if you do not provide any params when rendering the report.
|
46
|
+
|
47
|
+
bc. report.render
|
48
|
+
|
49
|
+
This will cause rescpos to search for a template named simple_titled.escpos.erb under app/escposes.
|
50
|
+
|
51
|
+
h2. Templates
|
52
|
+
|
53
|
+
Rescpos use erb to define the templates. So any valid erb templates are valid rescpos templates.
|
54
|
+
|
55
|
+
Template's extension name is set to 'escpos.erb'.
|
56
|
+
|
57
|
+
bc. # dish_item.escpos.erb
|
58
|
+
<%= text(@dish[:dish_category], :font_size => FONT_BIG) %>
|
59
|
+
<%= text(key_value("餐桌", @bill[:table]), :font_size => FONT_BIG) %>
|
60
|
+
<%= text(key_value("人数", @bill[:num_of_people]), :font_size => FONT_BIG) %>
|
61
|
+
<%= single_splitline %>
|
62
|
+
<%= text(key_value("菜品", @dish[:name]), :font_size => FONT_BIG) %>
|
63
|
+
<%= text(key_value("数量", "#{@dish[:quantity]} [根]"), :font_size => FONT_BIG) %>
|
64
|
+
<%= single_splitline %>
|
65
|
+
<%= text(key_value("单号", @bill[:id])) %>
|
66
|
+
<%= text(key_value("操作员", @bill[:waiter])) %>
|
67
|
+
<%= text(key_value("下单时间", @bill[:created_at])) %>
|
68
|
+
|
69
|
+
h2. Printers
|
70
|
+
|
71
|
+
In order to print the report, you should create a Rescpos::Printer object and then print the report.
|
72
|
+
|
73
|
+
bc. printer = Rescpos::Printer.open('192.168.1.250', 9100)
|
74
|
+
printer.print(report)
|
75
|
+
printer.close
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
<%= text(@dish[:dish_category], :font_size => FONT_BIG) %>
|
2
|
+
<%= text(key_value("餐桌", @bill[:table]), :font_size => FONT_BIG) %>
|
3
|
+
<%= text(key_value("人数", @bill[:num_of_people]), :font_size => FONT_BIG) %>
|
4
|
+
<%= single_splitline %>
|
5
|
+
<%= text(key_value("菜品", @dish[:name]), :font_size => FONT_BIG) %>
|
6
|
+
<%= text(key_value("数量", "#{@dish[:quantity]} [根]"), :font_size => FONT_BIG) %>
|
7
|
+
<%= single_splitline %>
|
8
|
+
<%= text(key_value("单号", @bill[:id])) %>
|
9
|
+
<%= text(key_value("操作员", @bill[:waiter])) %>
|
10
|
+
<%= text(key_value("下单时间", @bill[:created_at])) %>
|
data/examples/print.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.unshift(File.join(File.dirname(__FILE__), "../lib"))
|
3
|
+
$:.unshift(File.join(File.dirname(__FILE__)))
|
4
|
+
|
5
|
+
require 'rescpos'
|
6
|
+
require 'reports/dish_item_report'
|
7
|
+
|
8
|
+
Rescpos.configure do |config|
|
9
|
+
config.template_path = File.expand_path(File.join(File.dirname(__FILE__), 'escposes'))
|
10
|
+
end
|
11
|
+
|
12
|
+
report = DishItemReport.new
|
13
|
+
|
14
|
+
printer = Rescpos::Printer.open("192.168.1.3", 9100)
|
15
|
+
printer.print_report(report, :encoding => 'GBK')
|
16
|
+
printer.close
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.unshift(File.join(File.dirname(__FILE__), "../../lib"))
|
3
|
+
|
4
|
+
require 'rescpos'
|
5
|
+
|
6
|
+
class DishItemReport < Rescpos::Report
|
7
|
+
attr_reader :bill, :dish
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@bill = {
|
11
|
+
:id => "20110819000002",
|
12
|
+
:waiter => '000',
|
13
|
+
:created_at => '2011-08-18 13:11:03',
|
14
|
+
:table => '01B[大堂]',
|
15
|
+
:num_of_people => 2
|
16
|
+
}
|
17
|
+
|
18
|
+
@dish = {
|
19
|
+
:name => '广式靓油条',
|
20
|
+
:quantity => 1,
|
21
|
+
:dish_category => '面组'
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
Feature: Render a well-formatted report
|
2
|
+
In order to print a well-formatted report through WINPOS WP-800 printer
|
3
|
+
I want to render the report with erb templates
|
4
|
+
|
5
|
+
Scenario: create a titled report
|
6
|
+
Given a ESC/POS template:
|
7
|
+
"""
|
8
|
+
<%= @title %>
|
9
|
+
"""
|
10
|
+
And a titled report:
|
11
|
+
"""
|
12
|
+
class TitledReport < Rescpos::Report
|
13
|
+
attr_reader :title, :str_template
|
14
|
+
|
15
|
+
def initialize(title = '')
|
16
|
+
@title = title
|
17
|
+
end
|
18
|
+
end
|
19
|
+
@report = TitledReport.new('A report with a title')
|
20
|
+
"""
|
21
|
+
When I render the titled report with the given template
|
22
|
+
Then I will see "A report with a title"
|
23
|
+
|
24
|
+
Scenario: render a titled report with a template
|
25
|
+
Given "spec/reports" is a template directory
|
26
|
+
And a titled report:
|
27
|
+
"""
|
28
|
+
class TitledReport < Rescpos::Report
|
29
|
+
attr_reader :title, :str_template
|
30
|
+
|
31
|
+
def initialize(title = '')
|
32
|
+
@title = title
|
33
|
+
end
|
34
|
+
end
|
35
|
+
@report = TitledReport.new('A report with a title')
|
36
|
+
"""
|
37
|
+
When I render the report
|
38
|
+
Then I will see "A report with a title"
|
File without changes
|
@@ -0,0 +1,25 @@
|
|
1
|
+
Given /^a ESC\/POS template:$/ do |string|
|
2
|
+
@str_template = string
|
3
|
+
end
|
4
|
+
|
5
|
+
Given /^a titled report:$/ do |string|
|
6
|
+
eval string
|
7
|
+
end
|
8
|
+
|
9
|
+
When /^I render the titled report with the given template$/ do
|
10
|
+
@output = @report.render(:template => @str_template)
|
11
|
+
end
|
12
|
+
|
13
|
+
Then /^I will see "([^"]*)"$/ do |title|
|
14
|
+
@output.should == title
|
15
|
+
end
|
16
|
+
|
17
|
+
Given /^"([^"]*)" is a template directory$/ do |path|
|
18
|
+
Rescpos.configure do |config|
|
19
|
+
config.template_path = path
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
When /^I render the report$/ do
|
24
|
+
@output = @report.render
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rescpos'
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Rescpos
|
2
|
+
class Printer
|
3
|
+
attr_reader :socket
|
4
|
+
|
5
|
+
def initialize(ip, port)
|
6
|
+
@socket = TCPSocket.open(ip, port)
|
7
|
+
@socket.send("\x1b\x40", Socket::MSG_OOB)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.open(ip, port)
|
11
|
+
printer = new(ip, port)
|
12
|
+
end
|
13
|
+
|
14
|
+
def close
|
15
|
+
@socket.close
|
16
|
+
end
|
17
|
+
|
18
|
+
def print(content, opts={})
|
19
|
+
if opts[:encoding]
|
20
|
+
content = Iconv.iconv("#{opts[:encoding]}//IGNORE","UTF-8//IGNORE", content)[0]
|
21
|
+
end
|
22
|
+
@socket.send(content, Socket::MSG_OOB)
|
23
|
+
cut
|
24
|
+
end
|
25
|
+
|
26
|
+
def print_report(report, opts={})
|
27
|
+
print(report.render, opts)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def send_command(command)
|
32
|
+
@socket.send(command, Socket::MSG_OOB)
|
33
|
+
end
|
34
|
+
|
35
|
+
def cut
|
36
|
+
#TODO
|
37
|
+
send_command("\n\n\n\n\x1b\x69")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Rescpos
|
2
|
+
class Report
|
3
|
+
include ERB::Util
|
4
|
+
include ReportUtil
|
5
|
+
|
6
|
+
def render(opts = {})
|
7
|
+
template = opts[:file] ? File.read(opts[:file]) : opts[:template]
|
8
|
+
unless template
|
9
|
+
template_filename = self.class.to_s.gsub(/Report/, '').underscore
|
10
|
+
template = File.read(File.join(Rescpos.configuration.template_path, "#{template_filename}.escpos.erb"))
|
11
|
+
end
|
12
|
+
erb = ERB.new(template, 0)
|
13
|
+
erb.result(binding)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Rescpos
|
2
|
+
module ReportUtil
|
3
|
+
FONT_NORMAL = "\x00"
|
4
|
+
FONT_BIG = "\x11"
|
5
|
+
|
6
|
+
def single_splitline
|
7
|
+
text("-" * 42, :font_size => FONT_NORMAL)
|
8
|
+
end
|
9
|
+
|
10
|
+
def double_splitline
|
11
|
+
text("=" * 42, :font_size => FONT_NORMAL)
|
12
|
+
end
|
13
|
+
|
14
|
+
def underline(number)
|
15
|
+
text("_" * number, :font_size => FONT_NORMAL)
|
16
|
+
end
|
17
|
+
|
18
|
+
def chinese(chinese)
|
19
|
+
text = Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",chinese)
|
20
|
+
text[0]
|
21
|
+
end
|
22
|
+
|
23
|
+
def text(txt, options = {})
|
24
|
+
font_size = options[:font_size] || FONT_NORMAL
|
25
|
+
formatted_text = ''
|
26
|
+
formatted_text << fontsize(font_size)
|
27
|
+
formatted_text << grayscale(options[:gray]) if options[:gray]
|
28
|
+
formatted_text << txt if txt
|
29
|
+
end
|
30
|
+
|
31
|
+
def fontsize(size)
|
32
|
+
"\x1d\x21" << size.to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
def grayscale(value)
|
36
|
+
"\x1b\x6d" << ascii(value)
|
37
|
+
end
|
38
|
+
|
39
|
+
def key_value(label, value)
|
40
|
+
"#{label}: #{value}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def align(format)
|
44
|
+
if format == 'C'
|
45
|
+
return "\x1b\x61\x01"
|
46
|
+
elsif format == 'L'
|
47
|
+
return "\x1b\x61\x00"
|
48
|
+
elsif format == 'R'
|
49
|
+
return "\x1b\x61\x02"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def table(positions)
|
54
|
+
command = "\x1b\x44"
|
55
|
+
for position in positions
|
56
|
+
command << ascii(position)
|
57
|
+
end
|
58
|
+
command << "\x00"
|
59
|
+
end
|
60
|
+
|
61
|
+
def horizontal_tab
|
62
|
+
"\x09"
|
63
|
+
end
|
64
|
+
|
65
|
+
def ascii(value)
|
66
|
+
value.to_s.unpack('U')[0].to_s(16)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/rescpos.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "erb"
|
2
|
+
require "socket"
|
3
|
+
|
4
|
+
require "core_ext/string"
|
5
|
+
|
6
|
+
require "rescpos/version"
|
7
|
+
require "rescpos/printer"
|
8
|
+
require "rescpos/report_util"
|
9
|
+
require "iconv"
|
10
|
+
require "rescpos/configuration"
|
11
|
+
require "rescpos/report"
|
12
|
+
|
13
|
+
module Rescpos
|
14
|
+
def self.configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configure
|
19
|
+
yield configuration if block_given?
|
20
|
+
end
|
21
|
+
end
|
data/rescpos.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rescpos/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rescpos"
|
7
|
+
s.version = Rescpos::VERSION
|
8
|
+
s.authors = ["Tower He"]
|
9
|
+
s.email = ["towerhe@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A ruby implementation of the Line Thermal Printer ESC/POS Command Specifications.}
|
12
|
+
s.description = %q{Print formatted docs with Winpos WP-800}
|
13
|
+
|
14
|
+
s.rubyforge_project = "rescpos"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
File without changes
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class Rails
|
4
|
+
def self.root
|
5
|
+
'/path/to/rails'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Rescpos::Configuration do
|
10
|
+
it "has a default template path app/escposes" do
|
11
|
+
Rescpos.configuration.template_path.should == File.join(Rails.root, "app/escposes")
|
12
|
+
end
|
13
|
+
|
14
|
+
it "could set the path of templates" do
|
15
|
+
Rescpos.configure do |config|
|
16
|
+
config.template_path = "templates"
|
17
|
+
end
|
18
|
+
|
19
|
+
Rescpos.configuration.template_path.should == "templates"
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
class TitledReport < Rescpos::Report
|
5
|
+
attr_reader :title
|
6
|
+
|
7
|
+
def initialize(title = "")
|
8
|
+
@title = title
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module Rescpos
|
13
|
+
describe Printer do
|
14
|
+
before(:each) do
|
15
|
+
TCPSocket.should_receive(:open).with("192.168.1.3", 9100).and_return(socket)
|
16
|
+
socket.should_receive(:send).with("\x1b\x40", Socket::MSG_OOB)
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:socket) { Object.new }
|
20
|
+
|
21
|
+
it "connects to the printer server" do
|
22
|
+
Printer.open("192.168.1.3", 9100)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "prints the a rendered report" do
|
26
|
+
printer = Printer.open("192.168.1.3", 9100)
|
27
|
+
|
28
|
+
socket.should_receive(:send).with("A title", Socket::MSG_OOB)
|
29
|
+
printer.should_receive(:cut)
|
30
|
+
printer.print(TitledReport.new("A title").render(:template => "<%= @title %>"))
|
31
|
+
end
|
32
|
+
|
33
|
+
it "prints the report" do
|
34
|
+
Rescpos.configure do |config|
|
35
|
+
config.template_path = File.join(File.dirname(__FILE__), "../../reports")
|
36
|
+
end
|
37
|
+
printer = Printer.open("192.168.1.3", 9100)
|
38
|
+
|
39
|
+
socket.should_receive(:send).with("A title\n", Socket::MSG_OOB)
|
40
|
+
printer.should_receive(:cut)
|
41
|
+
printer.print_report(TitledReport.new("A title"))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TitledReport < Rescpos::Report
|
4
|
+
attr_reader :title
|
5
|
+
|
6
|
+
def initialize(title = '')
|
7
|
+
@title = title
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe Rescpos::Report do
|
12
|
+
before(:all) do
|
13
|
+
Rescpos.configure do |config|
|
14
|
+
config.template_path = File.join(File.dirname(__FILE__), "../../reports")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
after(:all) do
|
19
|
+
Rescpos.instance_variable_set(:@configuration, nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:report) { report = TitledReport.new('A title') }
|
23
|
+
|
24
|
+
it 'renders the report with a string template' do
|
25
|
+
report.render(:template => '<%= @title %>').should == 'A title'
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'renders the report using default template file' do
|
29
|
+
report.render.should == "A title\n"
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
class ReportUtilTest
|
5
|
+
include Rescpos::ReportUtil
|
6
|
+
end
|
7
|
+
|
8
|
+
describe ReportUtilTest do
|
9
|
+
before :each do
|
10
|
+
@report_util = ReportUtilTest.new
|
11
|
+
end
|
12
|
+
it "should return a single split line" do
|
13
|
+
@report_util.single_splitline.should == "\x1d\x21\x00" + "-" * 42
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return a doubleline" do
|
17
|
+
@report_util.double_splitline.should == "\x1d\x21\x00" + "=" * 42
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should print underline" do
|
21
|
+
@report_util.underline(13).should == "\x1d\x21\x00" + "_" * 13
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return chinese" do
|
25
|
+
# TODO
|
26
|
+
# @report_util.chinese('菜').should == '\\x{B2CB}'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return a ascii" do
|
30
|
+
@report_util.ascii(4).should == "34"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return a font size" do
|
34
|
+
@report_util.fontsize(ReportUtilTest::FONT_NORMAL).should == "\x1d\x21\x00"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return a gray scale" do
|
38
|
+
@report_util.grayscale(4).should == "\x1b\x6d34"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return a formmat text" do
|
42
|
+
@report_util.text('abc', {:font_size => ReportUtilTest::FONT_BIG, :gray => 4}).should == "\x1d\x21\x11\x1b\x6d34abc"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should return a formatted key and value pair" do
|
46
|
+
@report_util.key_value('ab', 4).should == "ab: 4"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return a align" do
|
50
|
+
@report_util.align('C').should == "\x1b\x61\x01"
|
51
|
+
@report_util.align('L').should == "\x1b\x61\x00"
|
52
|
+
@report_util.align('R').should == "\x1b\x61\x02"
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @title %>
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rescpos
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tower He
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-28 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Print formatted docs with Winpos WP-800
|
15
|
+
email:
|
16
|
+
- towerhe@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .rspec
|
23
|
+
- .rvmrc
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- Guardfile
|
27
|
+
- README.textile
|
28
|
+
- Rakefile
|
29
|
+
- examples/escposes/dish_item.escpos.erb
|
30
|
+
- examples/print.rb
|
31
|
+
- examples/reports/dish_item_report.rb
|
32
|
+
- features/printer.feature
|
33
|
+
- features/render_report.feature
|
34
|
+
- features/step_definitions/.gitkeep
|
35
|
+
- features/step_definitions/common_steps.rb
|
36
|
+
- features/step_definitions/printer_steps.rb
|
37
|
+
- features/step_definitions/report_steps.rb
|
38
|
+
- features/support/env.rb
|
39
|
+
- lib/core_ext/string.rb
|
40
|
+
- lib/rescpos.rb
|
41
|
+
- lib/rescpos/configuration.rb
|
42
|
+
- lib/rescpos/printer.rb
|
43
|
+
- lib/rescpos/report.rb
|
44
|
+
- lib/rescpos/report_util.rb
|
45
|
+
- lib/rescpos/version.rb
|
46
|
+
- rescpos.gemspec
|
47
|
+
- spec/lib/core_ext/string_spec.rb
|
48
|
+
- spec/lib/rescpos/.gitkeep
|
49
|
+
- spec/lib/rescpos/configuration_spec.rb
|
50
|
+
- spec/lib/rescpos/printer_spec.rb
|
51
|
+
- spec/lib/rescpos/report_spec.rb
|
52
|
+
- spec/lib/rescpos/report_util_spec.rb
|
53
|
+
- spec/reports/titled.escpos.erb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
homepage: ''
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project: rescpos
|
75
|
+
rubygems_version: 1.8.10
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: A ruby implementation of the Line Thermal Printer ESC/POS Command Specifications.
|
79
|
+
test_files: []
|