invoice_printer_server 2.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a0ff1de59a0faaa03ec5b8713dceb066590cb27981b509b289d2e838f5e1dd68
4
+ data.tar.gz: b24e759907d4010526fbafa90bfb1bfee7f2c0aa1f54b5ee754eda55f9201143
5
+ SHA512:
6
+ metadata.gz: 869995621f683dc022bf6fe26540f93ffe9e20a0d408dcf9bc5c3e37ac061e82f41eb370aa8f558004cd6c5b628d8a7fddb0d36dc9a2e904a264493835c48d39
7
+ data.tar.gz: 9de382e4925f18e704cbfcfe862cebc90d6d75b3b60de3d92fb90262607559da91704670b39aba3c652e99d8b3e8a8f6812919cfc081b479b6431bb750775de8
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH << File.expand_path('lib')
3
+
4
+ require 'optparse'
5
+ require 'rack/handler/puma'
6
+ require 'invoice_printer/version'
7
+ require 'invoice_printer/server'
8
+
9
+ def show_version
10
+ puts "InvoicePrinter v#{InvoicePrinter::VERSION}"
11
+
12
+ exit 0
13
+ end
14
+
15
+ def show_help
16
+ puts <<~HELP
17
+ Usage: invoice_printer_server [options]
18
+
19
+ Options:
20
+
21
+ -h, --host hostname to listen on (default is 0.0.0.0)
22
+ -p, --port port to listen on (default is 9393)
23
+ -w, --workers number of Puma workers (default is 2)
24
+ -v, --version show version
25
+
26
+ HELP
27
+
28
+ exit 0
29
+ end
30
+
31
+ options = {}
32
+
33
+ parser = OptionParser.new do|opts|
34
+ opts.on('-h', '--hostname ADDRESS') do |address|
35
+ options[:Host] = address
36
+ end
37
+
38
+ opts.on('-p', '--port NUMBER') do |number|
39
+ options[:Port] = number
40
+ end
41
+
42
+ opts.on('-w', '--workers NUMBER') do |number|
43
+ options[:workers] = number.to_i
44
+ end
45
+
46
+ opts.on('-v', '--version') do
47
+ show_version
48
+ end
49
+
50
+ opts.on('--debug') do
51
+ options[:debug] = true
52
+ end
53
+
54
+ opts.on('-h', '--help') do
55
+ show_help
56
+ end
57
+ end
58
+
59
+ parser.parse!
60
+
61
+ puma_options = { :Host => '0.0.0.0', :Port => 9393, :workers => 2 }.merge(options)
62
+
63
+ begin
64
+ puts 'Starting InvoicePrinter Server...'
65
+ Rack::Handler::Puma.run(InvoicePrinter::Server.freeze.app, puma_options)
66
+ rescue => e
67
+ STDERR.puts "ERROR: #{e.message}"
68
+
69
+ if options[:debug]
70
+ STDERR.puts
71
+ STDERR.puts e.backtrace
72
+ end
73
+
74
+ exit 1
75
+ end
@@ -0,0 +1,90 @@
1
+ require 'base64'
2
+ require 'roda'
3
+ require 'invoice_printer'
4
+
5
+ class InvoicePrinter::Server < Roda
6
+ route do |r|
7
+ response['Content-Type'] = 'application/json'
8
+
9
+ # Assign params
10
+ if r.content_type == 'application/json'
11
+ begin
12
+ params = JSON.parse(r.body.read, symbolize_names: true)
13
+ rescue => e
14
+ response.status = 400
15
+ response.write({ result: 'error', error: 'Invalid JSON.' }.to_json)
16
+ r.halt
17
+ end
18
+
19
+ document = params[:document]
20
+ labels = params[:labels]
21
+ font = params[:font]
22
+ stamp = params[:stamp]
23
+ logo = params[:logo]
24
+ background = params[:background]
25
+ page_size = params[:page_size]
26
+ else
27
+ response.status = 400
28
+ response.write({ result: 'error', error: 'No JSON. Did you set Content-Type to application/json?' }.to_json)
29
+ r.halt
30
+ end
31
+
32
+ # Initialize InvoicePrinter::Document from given JSON
33
+ begin
34
+ document[:items] ||= []
35
+ items = document[:items].map { |item| InvoicePrinter::Document::Item.new(**item) }
36
+ document[:items] = items
37
+
38
+ document = InvoicePrinter::Document.new(**document)
39
+ rescue => e
40
+ response.status = 400
41
+ response.write({ result: 'error', error: 'Invalid JSON document.' }.to_json)
42
+ r.halt
43
+ end
44
+
45
+ # POST /print
46
+ r.post 'print' do
47
+ filename = params[:filename] || 'document.pdf'
48
+
49
+ begin
50
+ InvoicePrinter.print(
51
+ document: document,
52
+ labels: labels,
53
+ font: font,
54
+ stamp: stamp,
55
+ logo: logo,
56
+ background: background,
57
+ page_size: page_size,
58
+ file_name: filename
59
+ )
60
+
61
+ { result: 'ok', path: filename }.to_json
62
+ rescue => e
63
+ response.status = 400
64
+ response.write({ result: 'error', error: e.message }.to_json)
65
+ r.halt
66
+ end
67
+ end
68
+
69
+ # POST /render
70
+ r.post 'render' do
71
+ begin
72
+ stream = InvoicePrinter.render(
73
+ document: document,
74
+ labels: labels,
75
+ font: font,
76
+ stamp: stamp,
77
+ logo: logo,
78
+ background: background,
79
+ page_size: page_size
80
+ )
81
+
82
+ { result: 'ok', data: Base64.encode64(stream) }.to_json
83
+ rescue => e
84
+ response.status = 400
85
+ response.write({ result: 'error', error: e.message }.to_json)
86
+ r.halt
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,3 @@
1
+ module InvoicePrinter
2
+ VERSION = '2.0.0.alpha1'
3
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: invoice_printer_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0.alpha1
5
+ platform: ruby
6
+ authors:
7
+ - Josef Strzibny
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: invoice_printer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0.alpha1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0.alpha1
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: roda
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: puma
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.9.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.9.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ description: Super simple and fast PDF invoicing in pure Ruby (based on Prawn library).
98
+ email:
99
+ - strzibny@strzibny.name
100
+ executables:
101
+ - invoice_printer_server
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - bin/invoice_printer_server
106
+ - lib/invoice_printer/server.rb
107
+ - lib/invoice_printer/version.rb
108
+ homepage: https://github.com/strzibny/invoice_printer
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">"
124
+ - !ruby/object:Gem::Version
125
+ version: 1.3.1
126
+ requirements: []
127
+ rubygems_version: 3.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Super simple PDF invoicing in pure Ruby
131
+ test_files: []