receipt 0.1.0

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
+ SHA1:
3
+ metadata.gz: d4c0420932892a7173d88048aed6a0cdddbbfae5
4
+ data.tar.gz: 869caaabf30b5028a12fbf083ae3041877a8a2e6
5
+ SHA512:
6
+ metadata.gz: 9952a5ca12bca8480590aa40315c447c974a994f72362cb60f2bc9e2458b93cafc62cb50e0d08ad502278b49c7a79796090a6f5418fd9fee1a75e6e730665e3b
7
+ data.tar.gz: 025365f9c35c03830c3df0bab0eb87783f22f36accae81d122d08f25ee4fa103dda795c50f6ef3bc68ab76a1c1d4b961b800cc730e024c70e8ee918c8cf87c7a
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.ruby-gemset
11
+ /.ruby-version
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --order random
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Fernando Almeida
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Receipt
2
+
3
+ PDF Receipts generator for any ruby application.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'receipt'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install receipt
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ receipt = Receipt::Pdf.new(
25
+ id: 1,
26
+ date: Time.now.strftime('%d/%m/%Y'),
27
+ amount: 100.0,
28
+ currency: '$',
29
+ payer: 'Chucky Norris',
30
+ receiver: 'Fernando Almeida',
31
+ description: 'Transaction #123',
32
+ logo: 'logo.png'
33
+ ).generate
34
+
35
+ # send to email
36
+ receipt.send_to('payer@email.com')
37
+
38
+ # send to download
39
+ send_data(receipt.data, filename: receipt.filename, type: receipt.type)
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies.
45
+ Then, run `bin/console` for an interactive prompt that will allow you to use it.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`.
48
+
49
+ To release a new version, update the version number in `version.rb`, and then
50
+ run `bundle exec rake release` to create a git tag for the version,
51
+ push git commits and tags, and push the `.gem` file to
52
+ [rubygems.org](https://rubygems.org).
53
+
54
+ ## Maintainer
55
+
56
+ [Fernando Almeida](http://fernandoalmeida.net)
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it ( https://github.com/fernandoalmeida/receipt/fork )
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'receipt'
5
+
6
+ require 'pry'
7
+ Pry.start
8
+
9
+ require 'irb'
10
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,61 @@
1
+ ---
2
+ en:
3
+ receipt: Receipt
4
+ number: Number
5
+ amount: Amount
6
+ received_from: Received from
7
+ the_amount_of: the amount of
8
+ relating_to: relating to
9
+ date:
10
+ abbr_day_names:
11
+ - Sun
12
+ - Mon
13
+ - Tue
14
+ - Wed
15
+ - Thu
16
+ - Fri
17
+ - Sat
18
+ abbr_month_names:
19
+ -
20
+ - Jan
21
+ - Feb
22
+ - Mar
23
+ - Apr
24
+ - May
25
+ - Jun
26
+ - Jul
27
+ - Aug
28
+ - Sep
29
+ - Oct
30
+ - Nov
31
+ - Dec
32
+ day_names:
33
+ - Sunday
34
+ - Monday
35
+ - Tuesday
36
+ - Wednesday
37
+ - Thursday
38
+ - Friday
39
+ - Saturday
40
+ formats:
41
+ default: "%m/%d/%Y"
42
+ long: "%B %d, %Y"
43
+ short: "%b %d"
44
+ month_names:
45
+ -
46
+ - January
47
+ - February
48
+ - March
49
+ - April
50
+ - May
51
+ - June
52
+ - July
53
+ - August
54
+ - September
55
+ - October
56
+ - November
57
+ - December
58
+ order:
59
+ - :year
60
+ - :month
61
+ - :day
@@ -0,0 +1,61 @@
1
+ ---
2
+ pt:
3
+ receipt: Recibo
4
+ number: Numero
5
+ amount: Valor
6
+ received_from: Recebi de
7
+ the_amount_of: a quantia de
8
+ relating_to: referente a
9
+ date:
10
+ abbr_day_names:
11
+ - Dom
12
+ - Seg
13
+ - Ter
14
+ - Qua
15
+ - Qui
16
+ - Sex
17
+ - Sáb
18
+ abbr_month_names:
19
+ -
20
+ - Jan
21
+ - Fev
22
+ - Mar
23
+ - Abr
24
+ - Mai
25
+ - Jun
26
+ - Jul
27
+ - Ago
28
+ - Set
29
+ - Out
30
+ - Nov
31
+ - Dez
32
+ day_names:
33
+ - Domingo
34
+ - Segunda
35
+ - Terça
36
+ - Quarta
37
+ - Quinta
38
+ - Sexta
39
+ - Sábado
40
+ formats:
41
+ default: "%d/%m/%Y"
42
+ long: "%d de %B de %Y"
43
+ short: "%d de %B"
44
+ month_names:
45
+ -
46
+ - Janeiro
47
+ - Fevereiro
48
+ - Março
49
+ - Abril
50
+ - Maio
51
+ - Junho
52
+ - Julho
53
+ - Agosto
54
+ - Setembro
55
+ - Outubro
56
+ - Novembro
57
+ - Dezembro
58
+ order:
59
+ - :day
60
+ - :month
61
+ - :year
@@ -0,0 +1,205 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'ostruct'
3
+ require 'i18n'
4
+
5
+ module Receipt
6
+ class MissingRequiredParamError < StandardError; end
7
+ class Pdf
8
+ extend Forwardable
9
+
10
+ include Prawn::View
11
+
12
+ attr_reader :params, :errors
13
+
14
+ def_delegators :I18n, :t, :l
15
+
16
+ def_delegators :params, *[
17
+ :id,
18
+ :payer,
19
+ :receiver,
20
+ :amount,
21
+ :date,
22
+ :currency,
23
+ :description,
24
+ :logo,
25
+ :location,
26
+ :locale,
27
+ :filepath
28
+ ]
29
+
30
+ def initialize(params)
31
+ @params = OpenStruct.new(params)
32
+ setup_i18n
33
+
34
+ @errors = {}
35
+ end
36
+
37
+ def data
38
+ return unless valid?
39
+
40
+ generate
41
+ render
42
+ end
43
+
44
+ def file
45
+ return unless valid?
46
+
47
+ @file ||= lambda do
48
+ generate
49
+ save_as(path)
50
+ path
51
+ end.call
52
+ end
53
+
54
+ def before_receipt_box(&block)
55
+ @before ||= block
56
+ end
57
+
58
+ def after_receipt_box(&block)
59
+ @after ||= block
60
+ end
61
+
62
+ def valid?
63
+ [
64
+ :id,
65
+ :amount,
66
+ :payer,
67
+ :receiver,
68
+ :description
69
+ ].each do |p|
70
+ @errors[p] = 'required param not found' if params.send(p).to_s.empty?
71
+ end
72
+
73
+ @errors.size == 0
74
+ end
75
+
76
+ private
77
+
78
+ def generate
79
+ valid?
80
+
81
+ before_receipt_box.call if before_receipt_box
82
+
83
+ move_down 20
84
+ receipt_box
85
+
86
+ move_down 20
87
+ after_receipt_box.call if after_receipt_box
88
+ end
89
+
90
+ def header
91
+ pad(10) do
92
+ text(t(:receipt).upcase, align: :center, style: :bold, size: 20)
93
+
94
+ move_up 20
95
+ text("#{t(:number)}: <b>#{id}</b>", inline_format: true)
96
+
97
+ move_up 15
98
+ text("#{t(:amount)}: <b>#{formated_amount}</b>",
99
+ inline_format: true, align: :right)
100
+ pad(10) { stroke_horizontal_rule }
101
+ end
102
+ end
103
+
104
+ def body
105
+ text(
106
+ [
107
+ "#{t(:received_from)} <b>#{payer}</b>",
108
+ "#{t(:the_amount_of)} <b>#{formated_amount}</b>",
109
+ "#{t(:relating_to)} <b>#{description}</b>."
110
+ ].join(' '),
111
+ inline_format: true
112
+ )
113
+ end
114
+
115
+ def footer
116
+ move_cursor_to 70
117
+ stroke_horizontal_rule
118
+
119
+ pad(35) do
120
+ position = cursor
121
+ width = bounds.width / 2
122
+
123
+ date_box(0, position, width)
124
+ signature_box(width, position, width)
125
+ end
126
+ end
127
+
128
+ def formated_amount
129
+ [
130
+ currency,
131
+ amount
132
+ ].join(' ')
133
+ end
134
+
135
+ def date_box(x, y, width)
136
+ bounding_box [x, y], width: width do
137
+ text(
138
+ [
139
+ location,
140
+ l(date, format: :long)
141
+ ].join(', ')
142
+ )
143
+ end
144
+ end
145
+
146
+ def receipt_box
147
+ padded_bounding_box(10, [30, cursor], width: 500, height: 250) do
148
+ header
149
+ body
150
+ footer
151
+ end
152
+ end
153
+
154
+ def signature_box(x, y, width)
155
+ bounding_box [x, y], width: width do
156
+ stroke_horizontal_rule
157
+ move_down 10
158
+ centered_text(receiver)
159
+ end
160
+ end
161
+
162
+ def centered_text(s)
163
+ text(s, align: :center)
164
+ end
165
+
166
+ def padded_bounding_box(padding, *args)
167
+ bounding_box(*args) do
168
+ stroke_bounds
169
+
170
+ bounding_box(
171
+ [padding, bounds.height - padding],
172
+ width: bounds.width - (2 * padding),
173
+ height: bounds.height - (2 * padding)
174
+ ) do
175
+ yield
176
+ end
177
+ end
178
+ end
179
+
180
+ def setup_i18n
181
+ load_translations
182
+ set_locale
183
+ end
184
+
185
+ def load_translations
186
+ I18n.load_path += Dir[File.expand_path('config/locale/*.yml')]
187
+ I18n.backend.load_translations
188
+ end
189
+
190
+ def set_locale
191
+ I18n.locale = (locale || I18n.default_locale)
192
+ end
193
+
194
+ def path
195
+ @path ||= (params.filepath || tempfilepath)
196
+ end
197
+
198
+ def tempfilepath
199
+ @tempfilepath ||= File.join(
200
+ Dir.tmpdir,
201
+ Dir::Tmpname.make_tmpname("#{t(:receipt)}", "#{id}.pdf")
202
+ )
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,3 @@
1
+ module Receipt
2
+ VERSION = '0.1.0'
3
+ end
data/lib/receipt.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'prawn'
2
+ require 'receipt/version'
3
+ require 'receipt/pdf'
4
+
5
+ module Receipt
6
+ end
data/receipt.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'receipt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'receipt'
8
+ spec.version = Receipt::VERSION
9
+ spec.authors = ['Fernando Almeida']
10
+ spec.email = ['fernando@fernandoalmeida.net']
11
+ spec.summary = 'Receipt generator for ruby application.'
12
+ spec.description = 'Generates receipts on PDF format.'
13
+ spec.homepage = 'http://github.com/fernandoalmeida/receipt'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(/^(test|spec|features)\//)
18
+ end
19
+ spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'prawn', '~> 2.0'
23
+ spec.add_dependency 'i18n', '~> 0.7'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.8'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'pry', '~> 0.10'
28
+ spec.add_development_dependency 'rspec', '~> 3.2'
29
+ spec.add_development_dependency 'pdf-inspector', '~> 1.2'
30
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: receipt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fernando Almeida
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: prawn
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pdf-inspector
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
111
+ description: Generates receipts on PDF format.
112
+ email:
113
+ - fernando@fernandoalmeida.net
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - CODE_OF_CONDUCT.md
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - config/locale/en.yml
128
+ - config/locale/pt.yml
129
+ - lib/receipt.rb
130
+ - lib/receipt/pdf.rb
131
+ - lib/receipt/version.rb
132
+ - receipt.gemspec
133
+ homepage: http://github.com/fernandoalmeida/receipt
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.2.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Receipt generator for ruby application.
157
+ test_files: []