Class: InvoiceMaker::Invoice
- Inherits:
-
Object
- Object
- InvoiceMaker::Invoice
- Defined in:
- lib/invoice_maker/invoice.rb
Constant Summary collapse
- PDF_CONTENT_TYPE =
'application/pdf'
- JSON_CONTENT_TYPE =
'application/json'
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#dest_dir ⇒ Object
Returns the value of attribute dest_dir.
Instance Method Summary collapse
- #append_item(item) ⇒ Object
-
#generate ⇒ Object
Let’s generate a pdf.
-
#initialize ⇒ Invoice
constructor
A new instance of Invoice.
-
#set(options = {}) ⇒ Object
set invoice data.
Constructor Details
#initialize ⇒ Invoice
Returns a new instance of Invoice.
9 10 11 12 13 |
# File 'lib/invoice_maker/invoice.rb', line 9 def initialize @dest_dir = '/tmp' @data = Hash.new @base_uri = 'https://invoice-generator.com' end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/invoice_maker/invoice.rb', line 7 def data @data end |
#dest_dir ⇒ Object
Returns the value of attribute dest_dir.
6 7 8 |
# File 'lib/invoice_maker/invoice.rb', line 6 def dest_dir @dest_dir end |
Instance Method Details
#append_item(item) ⇒ Object
24 25 26 27 28 |
# File 'lib/invoice_maker/invoice.rb', line 24 def append_item(item) raise "Unable to add items to an empty invoice data. Did you 'set' first?" if @data.empty? @data['items'] = [] unless @data.has_key?('items') @data['items'].push item unless @data.empty? end |
#generate ⇒ Object
Let’s generate a pdf
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/invoice_maker/invoice.rb', line 31 def generate response = HTTParty.post(@base_uri, body: @data.to_json, headers: { "Content-Type" => JSON_CONTENT_TYPE }) if response.success? && response.content_type == PDF_CONTENT_TYPE filename = File.join(@dest_dir, SecureRandom.urlsafe_base64(16)) + '.pdf' save_pdf(filename, response.parsed_response) else return nil end return filename end |
#set(options = {}) ⇒ Object
set invoice data
16 17 18 |
# File 'lib/invoice_maker/invoice.rb', line 16 def set( = {}) @data = end |