diff --git a/.gitignore b/.gitignore index b7e7725..b26b591 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /spec/reports/ /tmp/ *.gem +.ruby-version diff --git a/.ruby-version b/.ruby-version deleted file mode 100644 index cd57a8b..0000000 --- a/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -2.1.5 diff --git a/.travis.yml b/.travis.yml index ce0c05a..0f2bd9d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 \ No newline at end of file diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 6608ac9..d3fd73d 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -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. diff --git a/Gemfile b/Gemfile index 6bec79d..8d9d1d4 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/README.md b/README.md index 3a6f7d4..612e7fe 100644 --- a/README.md +++ b/README.md @@ -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 = 'username@email.com' diff --git a/lib/saasu/account.rb b/lib/saasu/account.rb index eef667f..b504033 100644 --- a/lib/saasu/account.rb +++ b/lib/saasu/account.rb @@ -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 diff --git a/lib/saasu/base.rb b/lib/saasu/base.rb index fdb29c3..99818ab 100644 --- a/lib/saasu/base.rb +++ b/lib/saasu/base.rb @@ -113,7 +113,7 @@ def to_s end def id - @attributes['Id'] + @attributes.has_key?('Id') ? @attributes['Id'] : @attributes['TransactionId'] end protected diff --git a/lib/saasu/client.rb b/lib/saasu/client.rb index e5ec26b..67ff533 100644 --- a/lib/saasu/client.rb +++ b/lib/saasu/client.rb @@ -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 @@ -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 diff --git a/lib/saasu/company.rb b/lib/saasu/company.rb index e136511..82fc399 100644 --- a/lib/saasu/company.rb +++ b/lib/saasu/company.rb @@ -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 diff --git a/lib/saasu/contact.rb b/lib/saasu/contact.rb index b00d9d9..a1cdd90 100644 --- a/lib/saasu/contact.rb +++ b/lib/saasu/contact.rb @@ -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 \ No newline at end of file diff --git a/lib/saasu/invoice.rb b/lib/saasu/invoice.rb index fd561cd..d616312 100644 --- a/lib/saasu/invoice.rb +++ b/lib/saasu/invoice.rb @@ -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? @@ -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 diff --git a/lib/saasu/item.rb b/lib/saasu/item.rb index 9dd3112..5535ad0 100644 --- a/lib/saasu/item.rb +++ b/lib/saasu/item.rb @@ -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 diff --git a/lib/saasu/payment.rb b/lib/saasu/payment.rb index 8a16fcf..2b1ba59 100644 --- a/lib/saasu/payment.rb +++ b/lib/saasu/payment.rb @@ -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 diff --git a/lib/saasu/tax_code.rb b/lib/saasu/tax_code.rb index 97764c6..a3816cb 100644 --- a/lib/saasu/tax_code.rb +++ b/lib/saasu/tax_code.rb @@ -1,4 +1,4 @@ class Saasu::TaxCode < Saasu::Base allowed_methods :show, :index - filter_by %W(IsActive) + filter_by %W(IsActive Page PageSize) end diff --git a/saasu2.gemspec b/saasu2.gemspec index 1e2a5c2..3eaa889 100644 --- a/saasu2.gemspec +++ b/saasu2.gemspec @@ -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 diff --git a/spec/search_spec.rb b/spec/search_spec.rb index 1a01144..2d60451 100644 --- a/spec/search_spec.rb +++ b/spec/search_spec.rb @@ -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 @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0fa4edb..7af592f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 } diff --git a/spec/user_spec.rb b/spec/user_spec.rb index 6463b49..18a13d7 100644 --- a/spec/user_spec.rb +++ b/spec/user_spec.rb @@ -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\":\"user@saasu.com\"}", :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\":\"user@saasu.com\"}", + 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