Skip to content

Commit

Permalink
Support for Reports
Browse files Browse the repository at this point in the history
  • Loading branch information
minimul committed Sep 6, 2016
1 parent 9624de9 commit d2a9c81
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ QboApi.request_id = true
expect(batch_response.detect{ |b| b["bId"] == "bid1" }["Vendor"]["DisplayName"]).to eq "Smith Family Store"
```

### Reports

```ruby
params = { start_date: '2015-01-01', end_date: '2015-07-31', customer: 1, summarize_column_by: 'Customers' }
response = api.reports(name: 'ProfitAndLoss', params: params)
p response["Header"]["ReportName"]) #=> 'ProfitAndLoss'
```

### Respond to an error
```ruby
customer = { DisplayName: 'Weiskopf Consulting' }
Expand Down
2 changes: 1 addition & 1 deletion lib/qbo_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def update(entity, id:, payload:)

def delete(entity, id:)
raise QboApi::NotImplementedError unless is_transaction_entity?(entity)
path = add_param_to_path(path: entity_path(entity), param: [:operation, :delete])
path = add_params_to_path(path: entity_path(entity), params: { operation: :delete })
payload = set_update(entity, id)
request(:post, entity: entity, path: path, payload: payload)
end
Expand Down
6 changes: 6 additions & 0 deletions lib/qbo_api/supporting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ def batch(payload)
request(:post, path: path, payload: payload)
end

def reports(name:, params: nil)
path = "#{realm_id}/reports/#{name}"
path = add_params_to_path(path: path, params: params) if params
request(:get, path: path)
end

end
end
10 changes: 6 additions & 4 deletions lib/qbo_api/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ def uuid

def add_request_id_to(path)
if QboApi.request_id
add_param_to_path(path: path, param: ["requestid", uuid])
add_params_to_path(path: path, params: { "requestid" => uuid })
else
path
end
end

def add_param_to_path(path:, param:)
def add_params_to_path(path:, params:)
uri = URI.parse(path)
new_query_ar = URI.decode_www_form(uri.query || '') << param
uri.query = URI.encode_www_form(new_query_ar)
params.each do |p|
new_query_ar = URI.decode_www_form(uri.query || '') << p.to_a
uri.query = URI.encode_www_form(new_query_ar)
end
uri.to_s
end

Expand Down
1 change: 1 addition & 0 deletions qbo_api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'omniauth-quickbooks'
spec.add_development_dependency 'dotenv'
spec.add_development_dependency 'vcr'
spec.add_development_dependency 'awesome_print'
spec.add_runtime_dependency 'faraday'
spec.add_runtime_dependency 'faraday_middleware'
spec.add_runtime_dependency 'faraday-detailed_logger'
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'webmock/rspec'
require 'vcr'
require 'dotenv'
require 'awesome_print'
Dotenv.load

def creds
Expand Down
21 changes: 21 additions & 0 deletions spec/supporting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@
end
end

context ".reports" do
it 'for Profit and Loss with query params' do
api = QboApi.new(creds.to_h)
VCR.use_cassette("qbo_api/reports/profit_and_loss", record: :none) do
params = { start_date: '2015-01-01', end_date: '2015-07-31', customer: 1, summarize_column_by: 'Customers' }
name = 'ProfitAndLoss'
response = api.reports(name: name, params: params)
expect(response["Header"]["ReportName"]).to eq name
end
end

it 'for General Ledger with no query params' do
api = QboApi.new(creds.to_h)
VCR.use_cassette("qbo_api/reports/gl", record: :none) do
name = 'GeneralLedger'
response = api.reports(name: name)
expect(response["Header"]["ReportName"]).to eq name
end
end
end

def batch_payload
{
"BatchItemRequest":
Expand Down
4 changes: 2 additions & 2 deletions spec/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
QboApi.request_id = true
api = QboApi.new(creds.to_h)
path = api.entity_path(:sales_receipt)
path = api.add_param_to_path(path: path, param: [:operation, :delete])
path = api.add_param_to_path(path: path, param: [:test, :true])
path = api.add_params_to_path(path: path, params: { operation: :delete, test: :true})
path = api.add_request_id_to(path)
expect(path).to match /&requestid=.*$/
end

end
end
101 changes: 101 additions & 0 deletions spec/vcr/qbo_api/reports/gl.yml

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

64 changes: 64 additions & 0 deletions spec/vcr/qbo_api/reports/profit_and_loss.yml

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

0 comments on commit d2a9c81

Please sign in to comment.