Skip to content

Custom errors #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/spec/reports/
/tmp/
*.gem
.ruby-version
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: ruby
rvm:
- 2.0.0
- 2.1.6
- 2.2.2
- 2.2.7
- 2.3.4
- 2.4.1
3 changes: 3 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
==== Version 0.1.5
* Allow setting of PageSize and Page

==== Version 0.1.4
Features:
* Saasu::User.reset_password – Allows a user to request that a password reset email be generated and sent to them.
Expand Down
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in saasu2.gemspec
gemspec

# make rails 4+ deep hash functions available to rails <4
gem 'deep_hash_transform', :git => "https://github.com/basecamp/deep_hash_transform"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ And then execute:
Create an initializer file with your Saasu configuration eg config/initilizers/saasu.rb
```ruby
require 'saasu'
require 'deep_hash_transform' #required if using Rails <4

Saasu::Config.configure do |c|
c.username = '[email protected]'
Expand Down
2 changes: 1 addition & 1 deletion lib/saasu/account.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Saasu::Account < Saasu::Base
allowed_methods :show, :index, :destroy, :update, :create
filter_by %W(IsActive IsBankAccount AccountType IncludeBuiltIn)
filter_by %W(IsActive IsBankAccount AccountType IncludeBuiltIn HeaderAccountId AccountLevel PageSize Page)
end
2 changes: 1 addition & 1 deletion lib/saasu/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def to_s
end

def id
@attributes['Id']
@attributes.has_key?('Id') ? @attributes['Id'] : @attributes['TransactionId']
end

protected
Expand Down
21 changes: 19 additions & 2 deletions lib/saasu/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def request(method, url, params = {})
if response.status == 200
response.body
elsif response.status == 404
raise "Resource not found."
raise ResourceNotFoundError.new()
else
raise "Server did not return a valid response. Response status: #{response.status}. Response body: #{response.body}"
raise InvalidResponseError.new(response.status, response.reason_phrase, response.body)
end
end

Expand Down Expand Up @@ -44,4 +44,21 @@ def api_url
end
end
end

class ResourceNotFoundError < StandardError
def initialize(msg="Resource not found.")
super
end
end

class InvalidResponseError < StandardError
attr_reader :status, :reason, :body
def initialize(status="",reason="",body="")
@status = status
@reason = reason
@body = body
super("Server did not return a valid response. Response status: #{status}. Reason phrase: #{reason}. Response body: #{body}")
end
end

end
2 changes: 1 addition & 1 deletion lib/saasu/company.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Saasu::Company < Saasu::Base
allowed_methods :show, :index, :destroy, :update, :create
filter_by %W(LastModifiedFromDate LastModifiedToDate CompanyName=)
filter_by %W(LastModifiedFromDate LastModifiedToDate CompanyName PageSize Page)
end
2 changes: 1 addition & 1 deletion lib/saasu/contact.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Saasu::Contact < Saasu::Base
allowed_methods :show, :index, :destroy, :update, :create
filter_by %W(LastModifiedFromDate LastModifiedToDate GivenName FamilyName IsActive IsCustomer IsSupplier IsContractor IsPartner Tags TagSelection Email ContactId)
filter_by %W(LastModifiedFromDate LastModifiedToDate GivenName FamilyName CompanyName CompanyId IsActive IsCustomer IsSupplier IsContractor IsPartner Tags TagSelection Email ContactId PageSize Page)
end
23 changes: 22 additions & 1 deletion lib/saasu/invoice.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Saasu::Invoice < Saasu::Base
allowed_methods :show, :index, :destroy, :update, :create
filter_by %W(InvoiceNumber LastModifiedFromDate LastModifiedToDate TransactionType Tags TagSelection InvoiceFromDate InvoiceToDate InvoiceStatus PaymentStatus ContactId)
filter_by %W(InvoiceNumber PurchaseOrderNumber LastModifiedFromDate LastModifiedToDate TransactionType Tags TagSelection InvoiceFromDate InvoiceToDate InvoiceStatus PaymentStatus BillingContactId PageSize Page)

def email(email_address = nil)
if email_address.present?
Expand All @@ -13,4 +13,25 @@ def email(email_address = nil)

Saasu::Client.request(:post, url, params)
end

# Returns the pdf file as raw binary data in a String object
#
# if there is problem getting the PDF you will get a runtime error
# e.g RuntimeError (Server did not return a valid response. URL: Invoice/9999/generate-pdf?FileId=9999. Response status: 400. Response body: Unable to perform the request.):
#
# this can happen if the tempate_id is invalid
def generate_pdf(template_id = nil)
if template_id.present?
url = ['Invoice', id, 'generate-pdf'].join('/')
params = { TemplateId: template_id }
else
url = ['Invoice', id, 'generate-pdf'].join('/')
params = {}
end
begin
Saasu::Client.request(:get, url, params)
rescue Faraday::Error::ParsingError => e
raise Saasu::InvalidResponseError.new("784", e.message, "")
end
end
end
2 changes: 1 addition & 1 deletion lib/saasu/item.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Saasu::Item < Saasu::Base
allowed_methods :show, :index, :destroy, :update, :create
filter_by %W(ItemType SearchMethod SearchText)
filter_by %W(LastModifiedFromDate LastModifiedToDate IsActive ItemType SearchMethod PageSize)
end
2 changes: 1 addition & 1 deletion lib/saasu/payment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Saasu::Payment < Saasu::Base
allowed_methods :show, :index, :destroy, :update, :create
filter_by %W(LastModifiedFromDate LastModifiedToDate ForInvoiceId ClearedFromDate ClearedToDate TransactionType PaymentFromDate PaymentToDate PaymentAccountId)
filter_by %W(LastModifiedFromDate LastModifiedToDate ForInvoiceId ClearedFromDate ClearedToDate TransactionType PaymentFromDate PaymentToDate PaymentAccountId PageSize Page)
end
2 changes: 1 addition & 1 deletion lib/saasu/tax_code.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Saasu::TaxCode < Saasu::Base
allowed_methods :show, :index
filter_by %W(IsActive)
filter_by %W(IsActive Page PageSize)
end
2 changes: 2 additions & 0 deletions saasu2.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ Gem::Specification.new do |spec|
spec.add_dependency "faraday_middleware"
spec.add_dependency "webmock"
spec.add_dependency 'activesupport'
# make rails 4+ deep hash functions available to rails <4
spec.add_dependency 'deep_hash_transform'
end
6 changes: 3 additions & 3 deletions spec/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
expect(query.perform).to eq({ contacts: 9, invoices: 8, items: 7 })
expect(a_request(:get, "https://api.saasu.com/search?FileId=777&IncludeSearchTermHighlights=false&Keywords=Customer&Scope=All&TransactionType=Transactions.Sale"))
.to have_been_made
end
end
end

private
Expand All @@ -33,11 +33,11 @@ def mock_api_requests
to_return(status: 200, body: { access_token: '12345', refresh_token: '67890', expires_in: 1000 }.to_json, headers: {'Content-Type'=>'application/json'})

stub_request(:get, "https://api.saasu.com/search?FileId=777&IncludeSearchTermHighlights=false&Keywords=Customer&Scope=All").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer 12345', 'User-Agent'=>'Faraday v0.9.1', 'X-Api-Version'=>'1.0'}).
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer 12345', 'X-Api-Version'=>'1.0'}).
to_return(:status => 200, body: search_results.to_json, :headers => {'Content-Type'=>'application/json'})

stub_request(:get, "https://api.saasu.com/search?FileId=777&IncludeSearchTermHighlights=false&Keywords=Customer&Scope=All&TransactionType=Transactions.Sale").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer 12345', 'User-Agent'=>'Faraday v0.9.1', 'X-Api-Version'=>'1.0'}).
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer 12345', 'X-Api-Version'=>'1.0'}).
to_return(:status => 200, body: search_results_with_transaction_type.to_json, :headers => {'Content-Type'=>'application/json'})
end

Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
end

config.mock_with :rspec
config.treat_symbols_as_metadata_keys_with_true_values = true
config.color = :enabled
config.order = :random
end

RSpec::Expectations.configuration.on_potential_false_positives = :nothing

Dir[File.dirname(__FILE__) + '*.rb'].each { |file| require_relative file }
5 changes: 3 additions & 2 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def mock_api_requests
to_return(status: 200, body: { access_token: '12345', refresh_token: '67890', expires_in: 1000 }.to_json, headers: {'Content-Type'=>'application/json'})

stub_request(:post, "https://api.saasu.com/User/reset-password?FileId=777").
with(:body => "{\"Username\":\"[email protected]\"}", :headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer 12345', 'User-Agent'=>'Faraday v0.9.1', 'X-Api-Version'=>'1.0'}).
to_return(:status => 200, body: {}.to_json, :headers => {'Content-Type'=>'application/json'})
with(:body => "{\"Username\":\"[email protected]\"}",
headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Bearer 12345', 'Content-Type'=>'application/json', 'X-Api-Version'=>'1.0'})
.to_return(:status => 200, body: {}.to_json, :headers => {'Content-Type'=>'application/json'})
end
end