Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cleaning up unneeded structure

  • Loading branch information...
commit b63246855af6cbde025aa7cbf327e87e82532450 1 parent 71e51b1
Jack Danger Canty authored
100  lib/barcode_service.rb
... ...
@@ -1,3 +1,99 @@
1  
-module BarcodeService
  1
+## Resources
  2
+require 'rubygems'
  3
+gem 'sinatra', '~> 1.0.0'
  4
+require 'sinatra'
  5
+gem 'gbarcode'
  6
+require 'gbarcode'
  7
+
  8
+
  9
+
  10
+## Application
  11
+
  12
+
  13
+get '/:code.:extension' do
  14
+  # begin
  15
+    options = request.env['rack.request.query_hash']
  16
+    options = default_options.merge(options)
  17
+
  18
+    content_type "image/#{params[:extension]}"
  19
+
  20
+    Converter.encode params[:code], params[:extension], options
  21
+  # rescue => error
  22
+  #   puts error.inspect
  23
+  #   status 400
  24
+  # end
2 25
 end
3  
-require 'barcode_service_client'
  26
+
  27
+get '/' do
  28
+  %Q{
  29
+    <body style='line-height: 1.8em; font-family: Archer, Museo, Helvetica, Georgia; font-size 25px; text-align: center; padding-top: 20%;'>
  30
+      Get a barcode image by crafting a url at this domain.
  31
+      <pre style='font-family: Iconsolata, monospace;background-color:#EEE'><img src="http://#{request.host}/12345632323.jpg?type=isbn</pre>
  32
+      <br />
  33
+      Also, try <a href='http://github.com/JackDanger/barcodeservice'>the official Ruby client</a>
  34
+    </body
  35
+}
  36
+end
  37
+
  38
+## HELPERS
  39
+
  40
+def default_options
  41
+  {
  42
+    :type     => '93',
  43
+    :output   => 'png',
  44
+    :width    => 100,
  45
+    :height   => 50,
  46
+    :x        => 0,
  47
+    :y        => 0,
  48
+    :margin   => 0,
  49
+    :scale    => 0
  50
+  }
  51
+end
  52
+
  53
+TYPES = {
  54
+  'ean'     => 'BARCODE_EAN',
  55
+	'upc'     => 'BARCODE_UPC',
  56
+	'isbn'    => 'BARCODE_ISBN',
  57
+	'128b'    => 'BARCODE_128B',
  58
+	'128C'    => 'BARCODE_128C',
  59
+	'128'     => 'BARCODE_128',
  60
+	'128raw'  => 'BARCODE_128RAW',
  61
+	'39'      => 'BARCODE_39',
  62
+	'I25'     => 'BARCODE_I25',
  63
+	'cbr'     => 'BARCODE_CBR',
  64
+	'msi'     => 'BARCODE_MSI',
  65
+	'pls'     => 'BARCODE_PLS',
  66
+	'93'      => 'BARCODE_93'
  67
+}
  68
+
  69
+module Converter
  70
+  include Gbarcode
  71
+  extend Gbarcode
  72
+  def self.encode(code, format, options)
  73
+    require 'stringio'
  74
+
  75
+    barcode = barcode_create(code)
  76
+    barcode_encode barcode, BARCODE_NO_CHECKSUM | const_get(TYPES[options[:type]])
  77
+    file = Tempfile.new('barcode')
  78
+    File.open(file.path, 'w') do |f|
  79
+      barcode_print(barcode, f, BARCODE_OUT_EPS)
  80
+    end
  81
+
  82
+    output = case format
  83
+    when 'eps'
  84
+      file.read
  85
+    when "png"
  86
+      %x{convert eps:#{file.path} png:-}
  87
+    when "jpg", "jpeg"
  88
+      %x{convert eps:#{file.path} jpg:-}
  89
+    when "gif"
  90
+      %x{convert eps:#{file.path} gif:-}
  91
+    end
  92
+
  93
+    file.unlink
  94
+
  95
+    output
  96
+  end
  97
+end
  98
+
  99
+
29  lib/barcode_service_client.rb
... ...
@@ -1,29 +0,0 @@
1  
-#
2  
-# HowTo:
3  
-#
4  
-#  client = BarcodeService::Client.new 'http://my-barcodeservice-app.com'
5  
-#  pdf_content = client.render '<html></html>'
6  
-#
7  
-module BarcodeService
8  
-  class Client
9  
-    # @server: "http://myserver.com"
10  
-    def initialize(server)
11  
-      require 'net/http'
12  
-      @server = URI.parse server
13  
-    end
14  
-
15  
-    def render code
16  
-      start do |http|
17  
-        http.get("/?code=#{code}").read_body
18  
-      end
19  
-    end
20  
-
21  
-    protected
22  
-
23  
-      def start
24  
-        Net::HTTP.start @server.host, @server.port do |http|
25  
-          yield http
26  
-        end
27  
-      end
28  
-  end
29  
-end
99  lib/barcode_service_server.rb
... ...
@@ -1,99 +0,0 @@
1  
-## Resources
2  
-require 'rubygems'
3  
-gem 'sinatra', '~> 1.0.0'
4  
-require 'sinatra'
5  
-gem 'gbarcode'
6  
-require 'gbarcode'
7  
-
8  
-
9  
-
10  
-## Application
11  
-
12  
-
13  
-get '/:code.:extension' do
14  
-  # begin
15  
-    options = request.env['rack.request.query_hash']
16  
-    options = default_options.merge(options)
17  
-
18  
-    content_type "image/#{params[:extension]}"
19  
-
20  
-    Converter.encode params[:code], params[:extension], options
21  
-  # rescue => error
22  
-  #   puts error.inspect
23  
-  #   status 400
24  
-  # end
25  
-end
26  
-
27  
-get '/' do
28  
-  %Q{
29  
-    <body style='line-height: 1.8em; font-family: Archer, Museo, Helvetica, Georgia; font-size 25px; text-align: center; padding-top: 20%;'>
30  
-      Get a barcode image by crafting a url at this domain.
31  
-      <pre style='font-family: Iconsolata, monospace;background-color:#EEE'><img src="http://#{request.host}/12345632323.jpg?type=isbn</pre>
32  
-      <br />
33  
-      Also, try <a href='http://github.com/JackDanger/barcodeservice'>the official Ruby client</a>
34  
-    </body
35  
-}
36  
-end
37  
-
38  
-## HELPERS
39  
-
40  
-def default_options
41  
-  {
42  
-    :type     => '93',
43  
-    :output   => 'png',
44  
-    :width    => 100,
45  
-    :height   => 50,
46  
-    :x        => 0,
47  
-    :y        => 0,
48  
-    :margin   => 0,
49  
-    :scale    => 0
50  
-  }
51  
-end
52  
-
53  
-TYPES = {
54  
-  'ean'     => 'BARCODE_EAN',
55  
-	'upc'     => 'BARCODE_UPC',
56  
-	'isbn'    => 'BARCODE_ISBN',
57  
-	'128b'    => 'BARCODE_128B',
58  
-	'128C'    => 'BARCODE_128C',
59  
-	'128'     => 'BARCODE_128',
60  
-	'128raw'  => 'BARCODE_128RAW',
61  
-	'39'      => 'BARCODE_39',
62  
-	'I25'     => 'BARCODE_I25',
63  
-	'cbr'     => 'BARCODE_CBR',
64  
-	'msi'     => 'BARCODE_MSI',
65  
-	'pls'     => 'BARCODE_PLS',
66  
-	'93'      => 'BARCODE_93'
67  
-}
68  
-
69  
-module Converter
70  
-  include Gbarcode
71  
-  extend Gbarcode
72  
-  def self.encode(code, format, options)
73  
-    require 'stringio'
74  
-
75  
-    barcode = barcode_create(code)
76  
-    barcode_encode barcode, BARCODE_NO_CHECKSUM | const_get(TYPES[options[:type]])
77  
-    file = Tempfile.new('barcode')
78  
-    File.open(file.path, 'w') do |f|
79  
-      barcode_print(barcode, f, BARCODE_OUT_EPS)
80  
-    end
81  
-
82  
-    output = case format
83  
-    when 'eps'
84  
-      file.read
85  
-    when "png"
86  
-      %x{convert eps:#{file.path} png:-}
87  
-    when "jpg", "jpeg"
88  
-      %x{convert eps:#{file.path} jpg:-}
89  
-    when "gif"
90  
-      %x{convert eps:#{file.path} gif:-}
91  
-    end
92  
-
93  
-    file.unlink
94  
-
95  
-    output
96  
-  end
97  
-end
98  
-
99  
-
2  test.rb
@@ -4,7 +4,7 @@
4 4
 require 'test/unit'
5 5
 require 'shoulda'
6 6
 require 'rack/test'
7  
-require 'lib/barcode_service_server'
  7
+require 'lib/barcode_service'
8 8
 
9 9
 class BarcodeServiceTest < Test::Unit::TestCase
10 10
   include Rack::Test::Methods

0 notes on commit b632468

Please sign in to comment.
Something went wrong with that request. Please try again.