Skip to content

Commit

Permalink
added get_pdf method (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Greyoxide authored Jul 27, 2024
1 parent c672db7 commit 460bb0d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ todo.txt
spec/temp/spec_status.txt
*.swp
scrap.txt
.tool-versions
.tool-versions
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,38 @@ See [docs](https://developer.intuit.com/docs/0100_quickbooks_online/0100_essenti
p qbo_api.is_transaction_entity?(:customer) # => false
p qbo_api.is_name_list_entity?(:vendors) # => true
```

### Download Quickbooks PDF
A quickbooks supplied PDF can be downloaded for api endpoints which offer a PDF.
```ruby
qbo_api.get_pdf(:invoice, 121) # produces a pdf stream.
```
The PDF stream can then be saved using your preferred method.
```ruby
# example using Ruby on Rails with ActiveStorage
class Invoice
has_one_attached :pdf_file
end
invoice_number = 121
pdf_data = qbo_api.get_pdf(:invoice, invoice_numer) # returns raw pdf stream
pdf_io = StringIO.new(pdf_data) # convert to a StringIO object
filename = "invoice_no_#{invoice_number}"

invoice.pdf_file.attach(
io: pdf_io,
filename: filename,
content_type: 'application/pdf'
)
```

```ruby
# plain ruby example
invoice_number = 121
pdf_data = qbo_api.get_pdf(:invoice, invoice_numer) # returns raw pdf stream
filename = "invoice_no_#{invoice_number}.pdf"

File.write(filename, pdf_data)
```
## Spin up an example
- <a href="http://minimul.com/access-the-quickbooks-online-api-with-oauth2.html" target="_blank">Check out this article on spinning up the example</a>.

Expand Down
5 changes: 5 additions & 0 deletions lib/qbo_api/api_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def get_by_query_filter(entity, query_filter_args, params: nil)
end
end

def get_pdf(entity, id)
path = "#{entity_path(entity)}/#{id}/pdf"
request(:get, entity: entity, path: path, headers: { 'Accept' => 'application/pdf' })
end

def create(entity, payload:, params: nil)
request(:post, entity: entity, path: entity_path(entity), payload: payload, params: params)
end
Expand Down
10 changes: 8 additions & 2 deletions lib/qbo_api/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def build_connection(url, headers: nil)

def request(method, path:, entity: nil, payload: nil, params: nil, headers: nil)
raw_response = raw_request(method, conn: connection, path: path, params: params, payload: payload, headers: headers)
response(raw_response, entity: entity)
response(raw_response, entity: entity, headers: headers)
end

def raw_request(method, conn:, path:, payload: nil, params: nil, headers: nil)
Expand All @@ -63,8 +63,14 @@ def raw_request(method, conn:, path:, payload: nil, params: nil, headers: nil)
end
end

def response(resp, entity: nil)
def response(resp, entity: nil, headers: false)
data = resp.body

if headers
content_type = headers['Accept'] || headers['Content-Type']
return data if content_type&.include?('application/pdf')
end

entity ? entity_response(data, entity) : data
rescue => e
QboApi.logger.debug { "#{LOG_TAG} response parsing error: entity=#{entity.inspect} body=#{resp.body.inspect} exception=#{e.inspect}" }
Expand Down
13 changes: 13 additions & 0 deletions spec/api_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,17 @@
end
end
end

describe 'get pdf' do
let(:entity) { :invoice }
let(:id) { '130' }

it 'requests the pdf of an invoice' do
use_cassette('get_pdf/invoice') do
result = api.get_pdf(entity, id)
pdf_starts_correctly = result&.match(/^%PDF-\d+(\.\d+)?/)
expect(pdf_starts_correctly).to be_truthy
end
end
end
end
36 changes: 36 additions & 0 deletions spec/vcr/get_pdf/invoice.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 460bb0d

Please sign in to comment.