barcoder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ .DS_Store
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ Barcoder, v 1.0
2
+ ===============
3
+
4
+ ### Author : Derek Perez
5
+
6
+ inspired (heavily) by the work of Author: Anuj Luthra. He originally wrote the library barcode_generator (<http://github.com/anujluthra/barcode-generator/>), that this library is based on.
7
+
8
+ This library is designed to support streaming barcode information, from GBarcode, straight to the web browser using data urls (<http://en.wikipedia.org/wiki/Data_URI_scheme>). This is ideal for no-write filesystem scenarios. It also supports persisting the barcodes to disk, but this is an optional function. By default, data urls are used. Barcode generator makes generating/displaying barcodes for certain alphanumeric ids a piece of cake. This way we can generate any barcode type which Gbarcode -> Gnome Barcode project supports.
9
+
10
+ ### FAQ
11
+
12
+ #### Why did you create this plugin?
13
+
14
+ barcode_generator is an awesome plugin, however, it does not interact very well with non-write filesystem style servers, ie: Heroku, or EC2. In the cloud, you can't be writing files to the filesystem (usually) directly, so I thought it was necessary to write a plugin that could use data urls (<http://en.wikipedia.org/wiki/Data_URI_scheme>) to stream the barcode to the browser, __with no filesystem writes whatsoever.__
15
+
16
+ #### Why didn't you just fork his plugin?
17
+
18
+ I substantially re-wrote how the plugin displays barcodes, and I didn't want to have that break any pre-existing uses he was implementing it with.
19
+
20
+ #### Is your API compatible with barcode_generator?
21
+
22
+ Yes, it should work identically.
23
+
24
+ ### USAGE:
25
+ its as simple as saying:
26
+ `<%= to_barcode 'FJJ4JD' %> `
27
+
28
+ This will generate a barcode for FJJ4JD in BARCODE_39 format with default width
29
+ and height and include it in the view.
30
+
31
+ ### Options Options Options:
32
+ To customize your barcodes, you can optionally pass following information in your views
33
+
34
+ + encoding_format (Gbarcode constants for eg. Gbarcode::BARCODE_128 etc..)
35
+ + width
36
+ + height
37
+ + scaling_factor
38
+ + xoff
39
+ + yoff
40
+ + margin
41
+ + no_ascii (accepts boolean true or false, prevents the ascii string from printing at the bottom of the barcode)
42
+ + output_type (accepts :disk or :stream. :disk will print the image to disk, and serve it regularly.)
43
+
44
+ in this case your view will look like:
45
+
46
+ `<%= to_barcode 'ANUJ', :height => 100, :width => 400, :margin => 100, :xoff => 20, :yoff => 40 %>`
47
+
48
+
49
+ ### Installation:
50
+ First install these requirements:
51
+
52
+ 1. gem for gbarcode
53
+ 2. install native ImageMagick library (RMagick not necessary)
54
+
55
+ Next, install Barcoder:
56
+
57
+ `install from git : git://github.com/perezd/barcoder.git`
58
+
59
+ ### Supported Barcode Formats:
60
+ Gbarcode as of now allows us to generate barcodes in following formats:
61
+ BARCODE_EAN
62
+ BARCODE_UPC
63
+ BARCODE_ISBN
64
+ BARCODE_128B
65
+ BARCODE_128C
66
+ BARCODE_128
67
+ BARCODE_128RAW
68
+ BARCODE_39
69
+ BARCODE_I25
70
+ BARCODE_CBR
71
+ BARCODE_MSI
72
+ BARCODE_PLS
73
+ BARCODE_93
74
+ BARCODE_ANY
75
+ BARCODE_NO_CHECKSUM
76
+
77
+ for more information on Gbarcode visit <http://gbarcode.rubyforge.org/rdoc/index.html><br/>
78
+ Many many thanks to Anuj Luthra for solving the initial hard work!
79
+
@@ -0,0 +1,32 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "barcoder"
9
+ gem.summary = %Q{Barcode image generation library, for non-write filesystems.}
10
+ gem.description = %Q{This library is designed to support streaming barcode information, from GBarcode, straight to the web browser using data urls. This is ideal for no-write filesystem scenarios.}
11
+ gem.email = "derek@derekperez.com"
12
+ gem.homepage = "http://github.com/perezd/barcoder"
13
+ gem.authors = ["Derek Perez", "Jack Danger Canty"]
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+
22
+ desc 'Default: build the docs.'
23
+ task :default => :rdoc
24
+
25
+ desc 'Generate documentation for the barcoder plugin.'
26
+ Rake::RDocTask.new(:rdoc) do |rdoc|
27
+ rdoc.rdoc_dir = 'rdoc'
28
+ rdoc.title = 'Barcoder'
29
+ rdoc.options << '--line-numbers' << '--inline-source'
30
+ rdoc.rdoc_files.include('README')
31
+ rdoc.rdoc_files.include('lib/**/*.rb')
32
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'base64'
3
+ require 'gbarcode'
4
+ require 'barcoder'
@@ -0,0 +1,91 @@
1
+ # Barcoder, v.1.0
2
+ # written by: Derek Perez (derek@derekperez.com) 2009
3
+ # inspired (heavily) by the work of Author: Anuj Luthra. He originally wrote the library
4
+ # barcode_generator (http://github.com/anujluthra/barcode-generator/), that this library is
5
+ # based on.
6
+ #
7
+ # This library is designed to support streaming barcode information, from GBarcode,
8
+ # straight to the web browser using data urls (http://en.wikipedia.org/wiki/Data_URI_scheme).
9
+ # This is ideal for no-write filesystem scenarios. It also supports persisting the barcodes
10
+ # to disk, but this is an optional function. By default, data urls are used.
11
+ module ActionView
12
+ class Base
13
+
14
+ # important defaults, should not be messed with.
15
+ VALID_BARCODER_OPTIONS = [:encoding_format, :output_format, :width, :height, :scaling_factor, :xoff, :yoff, :margin, :output_type]
16
+ DEFAULT_BARCODER_OUTPUT_FORMAT = 'gif'
17
+ DEFAULT_BARCODER_ENCODING = Gbarcode::BARCODE_39 | Gbarcode::BARCODE_NO_CHECKSUM
18
+ BARCODE_STORAGE_PATH = "public/images/barcodes"
19
+
20
+
21
+ def to_barcode(str, options = {:encoding_format => DEFAULT_BARCODER_ENCODING })
22
+ # verify requirements
23
+ options.assert_valid_keys(VALID_BARCODER_OPTIONS)
24
+ output_format = options[:output_format] ? options[:output_format] : DEFAULT_BARCODER_OUTPUT_FORMAT
25
+ output_type = options[:output_type] ? options[:output_type] : :stream
26
+ # generate the barcode object with all supplied options
27
+ options[:encoding_format] = DEFAULT_BARCODER_ENCODING unless options[:encoding_format]
28
+ bc = Gbarcode.barcode_create(str.to_s)
29
+
30
+ bc.width = options[:width] if options[:width]
31
+ bc.height = options[:height] if options[:height]
32
+ bc.scalef = options[:scaling_factor] if options[:scaling_factor]
33
+ bc.xoff = options[:xoff] if options[:xoff]
34
+ bc.yoff = options[:yoff] if options[:yoff]
35
+ bc.margin = options[:margin] if options[:margin]
36
+
37
+ Gbarcode.barcode_encode(bc, options[:encoding_format])
38
+
39
+ if options[:no_ascii]
40
+ print_options = Gbarcode::BARCODE_OUT_EPS|Gbarcode::BARCODE_NO_ASCII
41
+ else
42
+ print_options = Gbarcode::BARCODE_OUT_EPS
43
+ end
44
+
45
+ # this is where the magic happens.
46
+ data = `echo "#{get_bytes_from_barcode(bc, print_options)}" | convert eps: #{output_format}:`
47
+
48
+ # simple output strategy, define :output_type => :disk in the #to_barcode call if you want
49
+ # it to write out to the disk for you, otherwise it will be a data url stream.
50
+ output_type == :disk ? barcode_to_disk(data, bc, output_format) : barcode_to_stream(data, output_format, str)
51
+ end
52
+
53
+ # support for the original barcode-generator plugin syntax.
54
+ def barcode(str, options = {:encoding_format => DEFAULT_BARCODER_ENCODING })
55
+ to_barcode(str, options)
56
+ end
57
+
58
+ protected
59
+
60
+ # stream the barcode to disk. this may be necessary for some cases, but if you
61
+ # are living on a cluster node like say, heroku, this won't work out well for you.
62
+ def barcode_to_disk(data, barcode, output_format)
63
+ filename = "#{barcode.ascii.gsub(" ", "-")}.#{output_format}"
64
+ Dir.mkdir(BARCODE_STORAGE_PATH) unless File.directory?(BARCODE_STORAGE_PATH)
65
+ File.open("#{BARCODE_STORAGE_PATH}/#{filename}", 'w') do |f|
66
+ f.write(data)
67
+ end
68
+ image_tag("barcodes/#{filename}", :id => "barcode", :class => "barcode")
69
+ end
70
+
71
+ # stream the barcode to the client as a data url. often times, the barcode
72
+ # filesize is so minute, that this is absolutely acceptable. NOTE: I intentionally
73
+ # draw my own img tag for this, image_tag doesn't really like this.
74
+ def barcode_to_stream(data, format, str)
75
+ src = "data:image/#{format};base64,#{Base64.encode64(data)}"
76
+ %Q{<img src="#{src}" alt="#{str}" id="barcode" class="barcode" />}
77
+ end
78
+
79
+ # this method tricks GBarcode into printing the contents of the EPS into
80
+ # a file pipe, allowing us to get at the binary data, without touching the disk.
81
+ def get_bytes_from_barcode(barcode, print_options)
82
+ read, write = IO.pipe
83
+ Gbarcode.barcode_print(barcode, write, print_options)
84
+ write.close
85
+ buffer = read.readlines.join("\n")
86
+ read.close
87
+ return buffer
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,129 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Module: ActionView</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Module</strong></td>
53
+ <td class="class-name-in-header">ActionView</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../files/lib/barcoder_rb.html">
59
+ lib/barcoder.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ </table>
66
+ </div>
67
+ <!-- banner header -->
68
+
69
+ <div id="bodyContent">
70
+
71
+
72
+
73
+ <div id="contextContent">
74
+
75
+ <div id="description">
76
+ <p>
77
+ Barcoder, v.1.0 written by: Derek Perez (derek@derekperez.com) 2009
78
+ inspired (heavily) by the work of Author: Anuj Luthra. He originally wrote
79
+ the library barcode_generator (<a
80
+ href="http://github.com/anujluthra/barcode-generator">github.com/anujluthra/barcode-generator</a>/),
81
+ that this library is based on.
82
+ </p>
83
+ <p>
84
+ This library is designed to support streaming barcode information, from
85
+ GBarcode, straight to the web browser using data urls (<a
86
+ href="http://en.wikipedia.org/wiki/Data_URI_scheme">en.wikipedia.org/wiki/Data_URI_scheme</a>).
87
+ This is ideal for no-write filesystem scenarios. It also supports
88
+ persisting the barcodes to disk, but this is an optional function. By
89
+ default, data urls are used.
90
+ </p>
91
+
92
+ </div>
93
+
94
+
95
+ </div>
96
+
97
+
98
+ </div>
99
+
100
+
101
+ <!-- if includes -->
102
+
103
+ <div id="section">
104
+
105
+ <div id="class-list">
106
+ <h3 class="section-bar">Classes and Modules</h3>
107
+
108
+ Class <a href="ActionView/Base.html" class="link">ActionView::Base</a><br />
109
+
110
+ </div>
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+ <!-- if method_list -->
119
+
120
+
121
+ </div>
122
+
123
+
124
+ <div id="validator-badges">
125
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
126
+ </div>
127
+
128
+ </body>
129
+ </html>
@@ -0,0 +1,338 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
+ <head>
8
+ <title>Class: ActionView::Base</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
+ <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
12
+ <script type="text/javascript">
13
+ // <![CDATA[
14
+
15
+ function popupCode( url ) {
16
+ window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
+ }
18
+
19
+ function toggleCode( id ) {
20
+ if ( document.getElementById )
21
+ elem = document.getElementById( id );
22
+ else if ( document.all )
23
+ elem = eval( "document.all." + id );
24
+ else
25
+ return false;
26
+
27
+ elemStyle = elem.style;
28
+
29
+ if ( elemStyle.display != "block" ) {
30
+ elemStyle.display = "block"
31
+ } else {
32
+ elemStyle.display = "none"
33
+ }
34
+
35
+ return true;
36
+ }
37
+
38
+ // Make codeblocks hidden by default
39
+ document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
+
41
+ // ]]>
42
+ </script>
43
+
44
+ </head>
45
+ <body>
46
+
47
+
48
+
49
+ <div id="classHeader">
50
+ <table class="header-table">
51
+ <tr class="top-aligned-row">
52
+ <td><strong>Class</strong></td>
53
+ <td class="class-name-in-header">ActionView::Base</td>
54
+ </tr>
55
+ <tr class="top-aligned-row">
56
+ <td><strong>In:</strong></td>
57
+ <td>
58
+ <a href="../../files/lib/barcoder_rb.html">
59
+ lib/barcoder.rb
60
+ </a>
61
+ <br />
62
+ </td>
63
+ </tr>
64
+
65
+ <tr class="top-aligned-row">
66
+ <td><strong>Parent:</strong></td>
67
+ <td>
68
+ Object
69
+ </td>
70
+ </tr>
71
+ </table>
72
+ </div>
73
+ <!-- banner header -->
74
+
75
+ <div id="bodyContent">
76
+
77
+
78
+
79
+ <div id="contextContent">
80
+
81
+
82
+
83
+ </div>
84
+
85
+ <div id="method-list">
86
+ <h3 class="section-bar">Methods</h3>
87
+
88
+ <div class="name-list">
89
+ <a href="#M000002">barcode</a>&nbsp;&nbsp;
90
+ <a href="#M000003">barcode_to_disk</a>&nbsp;&nbsp;
91
+ <a href="#M000004">barcode_to_stream</a>&nbsp;&nbsp;
92
+ <a href="#M000005">get_bytes_from_barcode</a>&nbsp;&nbsp;
93
+ <a href="#M000001">to_barcode</a>&nbsp;&nbsp;
94
+ </div>
95
+ </div>
96
+
97
+ </div>
98
+
99
+
100
+ <!-- if includes -->
101
+
102
+ <div id="section">
103
+
104
+
105
+ <div id="constants-list">
106
+ <h3 class="section-bar">Constants</h3>
107
+
108
+ <div class="name-list">
109
+ <table summary="Constants">
110
+ <tr class="top-aligned-row context-row">
111
+ <td class="context-item-name">VALID_BARCODER_OPTIONS</td>
112
+ <td>=</td>
113
+ <td class="context-item-value">[:encoding_format, :output_format, :width, :height, :scaling_factor, :xoff, :yoff, :margin, :output_type]</td>
114
+ <td width="3em">&nbsp;</td>
115
+ <td class="context-item-desc">
116
+ important defaults, should not be messed with.
117
+
118
+ </td>
119
+ </tr>
120
+ <tr class="top-aligned-row context-row">
121
+ <td class="context-item-name">DEFAULT_BARCODER_OUTPUT_FORMAT</td>
122
+ <td>=</td>
123
+ <td class="context-item-value">'gif'</td>
124
+ </tr>
125
+ <tr class="top-aligned-row context-row">
126
+ <td class="context-item-name">DEFAULT_BARCODER_ENCODING</td>
127
+ <td>=</td>
128
+ <td class="context-item-value">Gbarcode::BARCODE_39 | Gbarcode::BARCODE_NO_CHECKSUM</td>
129
+ </tr>
130
+ <tr class="top-aligned-row context-row">
131
+ <td class="context-item-name">BARCODE_STORAGE_PATH</td>
132
+ <td>=</td>
133
+ <td class="context-item-value">&quot;public/images/barcodes&quot;</td>
134
+ </tr>
135
+ </table>
136
+ </div>
137
+ </div>
138
+
139
+
140
+
141
+
142
+
143
+
144
+ <!-- if method_list -->
145
+ <div id="methods">
146
+ <h3 class="section-bar">Public Instance methods</h3>
147
+
148
+ <div id="method-M000002" class="method-detail">
149
+ <a name="M000002"></a>
150
+
151
+ <div class="method-heading">
152
+ <a href="#M000002" class="method-signature">
153
+ <span class="method-name">barcode</span><span class="method-args">(str, options = {:encoding_format =&gt; DEFAULT_BARCODER_ENCODING })</span>
154
+ </a>
155
+ </div>
156
+
157
+ <div class="method-description">
158
+ <p>
159
+ support for the original <a href="Base.html#M000002">barcode</a>-generator
160
+ plugin syntax.
161
+ </p>
162
+ <p><a class="source-toggle" href="#"
163
+ onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
164
+ <div class="method-source-code" id="M000002-source">
165
+ <pre>
166
+ <span class="ruby-comment cmt"># File lib/barcoder.rb, line 56</span>
167
+ 56: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">barcode</span>(<span class="ruby-identifier">str</span>, <span class="ruby-identifier">options</span> = {<span class="ruby-identifier">:encoding_format</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-constant">DEFAULT_BARCODER_ENCODING</span> })
168
+ 57: <span class="ruby-identifier">to_barcode</span>(<span class="ruby-identifier">str</span>, <span class="ruby-identifier">options</span>)
169
+ 58: <span class="ruby-keyword kw">end</span>
170
+ </pre>
171
+ </div>
172
+ </div>
173
+ </div>
174
+
175
+ <div id="method-M000001" class="method-detail">
176
+ <a name="M000001"></a>
177
+
178
+ <div class="method-heading">
179
+ <a href="#M000001" class="method-signature">
180
+ <span class="method-name">to_barcode</span><span class="method-args">(str, options = {:encoding_format =&gt; DEFAULT_BARCODER_ENCODING })</span>
181
+ </a>
182
+ </div>
183
+
184
+ <div class="method-description">
185
+ <p><a class="source-toggle" href="#"
186
+ onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
187
+ <div class="method-source-code" id="M000001-source">
188
+ <pre>
189
+ <span class="ruby-comment cmt"># File lib/barcoder.rb, line 21</span>
190
+ 21: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_barcode</span>(<span class="ruby-identifier">str</span>, <span class="ruby-identifier">options</span> = {<span class="ruby-identifier">:encoding_format</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-constant">DEFAULT_BARCODER_ENCODING</span> })
191
+ 22: <span class="ruby-comment cmt"># verify requirements</span>
192
+ 23: <span class="ruby-identifier">options</span>.<span class="ruby-identifier">assert_valid_keys</span>(<span class="ruby-constant">VALID_BARCODER_OPTIONS</span>)
193
+ 24: <span class="ruby-identifier">output_format</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:output_format</span>] <span class="ruby-operator">?</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:output_format</span>] <span class="ruby-operator">:</span> <span class="ruby-constant">DEFAULT_BARCODER_OUTPUT_FORMAT</span>
194
+ 25: <span class="ruby-identifier">output_type</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:output_type</span>] <span class="ruby-operator">?</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:output_type</span>] <span class="ruby-operator">:</span> <span class="ruby-identifier">:stream</span>
195
+ 26: <span class="ruby-comment cmt"># generate the barcode object with all supplied options</span>
196
+ 27: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:encoding_format</span>] = <span class="ruby-constant">DEFAULT_BARCODER_ENCODING</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:encoding_format</span>]
197
+ 28: <span class="ruby-identifier">bc</span> = <span class="ruby-constant">Gbarcode</span>.<span class="ruby-identifier">barcode_create</span>(<span class="ruby-identifier">str</span>.<span class="ruby-identifier">to_s</span>)
198
+ 29:
199
+ 30: <span class="ruby-identifier">bc</span>.<span class="ruby-identifier">width</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:width</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:width</span>]
200
+ 31: <span class="ruby-identifier">bc</span>.<span class="ruby-identifier">height</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:height</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:height</span>]
201
+ 32: <span class="ruby-identifier">bc</span>.<span class="ruby-identifier">scalef</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:scaling_factor</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:scaling_factor</span>]
202
+ 33: <span class="ruby-identifier">bc</span>.<span class="ruby-identifier">xoff</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:xoff</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:xoff</span>]
203
+ 34: <span class="ruby-identifier">bc</span>.<span class="ruby-identifier">yoff</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:yoff</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:yoff</span>]
204
+ 35: <span class="ruby-identifier">bc</span>.<span class="ruby-identifier">margin</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:margin</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:margin</span>]
205
+ 36:
206
+ 37: <span class="ruby-constant">Gbarcode</span>.<span class="ruby-identifier">barcode_encode</span>(<span class="ruby-identifier">bc</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:encoding_format</span>])
207
+ 38:
208
+ 39: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:no_ascii</span>]
209
+ 40: <span class="ruby-identifier">print_options</span> = <span class="ruby-constant">Gbarcode</span><span class="ruby-operator">::</span><span class="ruby-constant">BARCODE_OUT_EPS</span><span class="ruby-operator">|</span><span class="ruby-constant">Gbarcode</span><span class="ruby-operator">::</span><span class="ruby-constant">BARCODE_NO_ASCII</span>
210
+ 41: <span class="ruby-keyword kw">else</span>
211
+ 42: <span class="ruby-identifier">print_options</span> = <span class="ruby-constant">Gbarcode</span><span class="ruby-operator">::</span><span class="ruby-constant">BARCODE_OUT_EPS</span>
212
+ 43: <span class="ruby-keyword kw">end</span>
213
+ 44:
214
+ 45: <span class="ruby-comment cmt"># this is where the magic happens.</span>
215
+ 46: <span class="ruby-identifier">stream</span> = <span class="ruby-identifier">get_bytes_from_barcode</span>(<span class="ruby-identifier">bc</span>, <span class="ruby-identifier">print_options</span>)
216
+ 47: <span class="ruby-identifier">data</span> = <span class="ruby-constant">Magick</span><span class="ruby-operator">::</span><span class="ruby-constant">Image</span><span class="ruby-operator">::</span><span class="ruby-identifier">read_inline</span>(<span class="ruby-constant">Base64</span>.<span class="ruby-identifier">encode64</span>(<span class="ruby-identifier">stream</span>))[<span class="ruby-value">0</span>]
217
+ 48: <span class="ruby-identifier">data</span>.<span class="ruby-identifier">format</span> = <span class="ruby-identifier">output_format</span>
218
+ 49:
219
+ 50: <span class="ruby-comment cmt"># simple output strategy, define :output_type =&gt; :disk in the #to_barcode call if you want</span>
220
+ 51: <span class="ruby-comment cmt"># it to write out to the disk for you, otherwise it will be a data url stream.</span>
221
+ 52: <span class="ruby-identifier">output_type</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">:disk</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">barcode_to_disk</span>(<span class="ruby-identifier">data</span>, <span class="ruby-identifier">bc</span>, <span class="ruby-identifier">output_format</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">barcode_to_stream</span>(<span class="ruby-identifier">data</span>)
222
+ 53: <span class="ruby-keyword kw">end</span>
223
+ </pre>
224
+ </div>
225
+ </div>
226
+ </div>
227
+
228
+ <h3 class="section-bar">Protected Instance methods</h3>
229
+
230
+ <div id="method-M000003" class="method-detail">
231
+ <a name="M000003"></a>
232
+
233
+ <div class="method-heading">
234
+ <a href="#M000003" class="method-signature">
235
+ <span class="method-name">barcode_to_disk</span><span class="method-args">(data, barcode, output_format)</span>
236
+ </a>
237
+ </div>
238
+
239
+ <div class="method-description">
240
+ <p>
241
+ stream the <a href="Base.html#M000002">barcode</a> to disk. this may be
242
+ necessary for some cases, but if you are living on a cluster node like say,
243
+ heroku, this won&#8216;t work out well for you.
244
+ </p>
245
+ <p><a class="source-toggle" href="#"
246
+ onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
247
+ <div class="method-source-code" id="M000003-source">
248
+ <pre>
249
+ <span class="ruby-comment cmt"># File lib/barcoder.rb, line 64</span>
250
+ 64: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">barcode_to_disk</span>(<span class="ruby-identifier">data</span>, <span class="ruby-identifier">barcode</span>, <span class="ruby-identifier">output_format</span>)
251
+ 65: <span class="ruby-identifier">filename</span> = <span class="ruby-node">&quot;#{barcode.ascii.gsub(&quot; &quot;, &quot;-&quot;)}.#{output_format}&quot;</span>
252
+ 66: <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">mkdir</span>(<span class="ruby-constant">BARCODE_STORAGE_PATH</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">directory?</span>(<span class="ruby-constant">BARCODE_STORAGE_PATH</span>)
253
+ 67: <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-node">&quot;#{BARCODE_STORAGE_PATH}/#{filename}&quot;</span>, <span class="ruby-value str">'w'</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
254
+ 68: <span class="ruby-identifier">f</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">data</span>.<span class="ruby-identifier">to_blob</span>)
255
+ 69: <span class="ruby-keyword kw">end</span>
256
+ 70: <span class="ruby-identifier">image_tag</span>(<span class="ruby-node">&quot;barcodes/#{filename}&quot;</span>)
257
+ 71: <span class="ruby-keyword kw">end</span>
258
+ </pre>
259
+ </div>
260
+ </div>
261
+ </div>
262
+
263
+ <div id="method-M000004" class="method-detail">
264
+ <a name="M000004"></a>
265
+
266
+ <div class="method-heading">
267
+ <a href="#M000004" class="method-signature">
268
+ <span class="method-name">barcode_to_stream</span><span class="method-args">(data)</span>
269
+ </a>
270
+ </div>
271
+
272
+ <div class="method-description">
273
+ <p>
274
+ stream the <a href="Base.html#M000002">barcode</a> to the client as a data
275
+ url. often times, the <a href="Base.html#M000002">barcode</a> filesize is
276
+ so minute, that this is absolutely acceptable. NOTE: I intentionally draw
277
+ my own img tag for this, image_tag doesn&#8216;t really like this.
278
+ </p>
279
+ <p><a class="source-toggle" href="#"
280
+ onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
281
+ <div class="method-source-code" id="M000004-source">
282
+ <pre>
283
+ <span class="ruby-comment cmt"># File lib/barcoder.rb, line 76</span>
284
+ 76: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">barcode_to_stream</span>(<span class="ruby-identifier">data</span>)
285
+ 77: <span class="ruby-identifier">src</span> = <span class="ruby-node">&quot;data:image/#{data.format};base64,#{Base64.encode64(data.to_blob)}&quot;</span>
286
+ 78: <span class="ruby-node">%Q{&lt;img src=&quot;#{src}&quot; /&gt;}</span>
287
+ 79: <span class="ruby-keyword kw">end</span>
288
+ </pre>
289
+ </div>
290
+ </div>
291
+ </div>
292
+
293
+ <div id="method-M000005" class="method-detail">
294
+ <a name="M000005"></a>
295
+
296
+ <div class="method-heading">
297
+ <a href="#M000005" class="method-signature">
298
+ <span class="method-name">get_bytes_from_barcode</span><span class="method-args">(barcode, print_options)</span>
299
+ </a>
300
+ </div>
301
+
302
+ <div class="method-description">
303
+ <p>
304
+ this method tricks GBarcode into printing the contents of the EPS into a
305
+ file pipe, allowing us to get at the binary data, without touching the
306
+ disk.
307
+ </p>
308
+ <p><a class="source-toggle" href="#"
309
+ onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
310
+ <div class="method-source-code" id="M000005-source">
311
+ <pre>
312
+ <span class="ruby-comment cmt"># File lib/barcoder.rb, line 83</span>
313
+ 83: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_bytes_from_barcode</span>(<span class="ruby-identifier">barcode</span>, <span class="ruby-identifier">print_options</span>)
314
+ 84: <span class="ruby-identifier">read</span>, <span class="ruby-identifier">write</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">pipe</span>
315
+ 85: <span class="ruby-constant">Gbarcode</span>.<span class="ruby-identifier">barcode_print</span>(<span class="ruby-identifier">barcode</span>, <span class="ruby-identifier">write</span>, <span class="ruby-identifier">print_options</span>)
316
+ 86: <span class="ruby-identifier">write</span>.<span class="ruby-identifier">close</span>
317
+ 87: <span class="ruby-identifier">buffer</span> = <span class="ruby-identifier">read</span>.<span class="ruby-identifier">readlines</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;\n&quot;</span>)
318
+ 88: <span class="ruby-identifier">read</span>.<span class="ruby-identifier">close</span>
319
+ 89: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">buffer</span>
320
+ 90: <span class="ruby-keyword kw">end</span>
321
+ </pre>
322
+ </div>
323
+ </div>
324
+ </div>
325
+
326
+
327
+ </div>
328
+
329
+
330
+ </div>
331
+
332
+
333
+ <div id="validator-badges">
334
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
335
+ </div>
336
+
337
+ </body>
338
+ </html>