From 4e46189a1fd79c1e90fe3b62a6a9f8c89eae0646 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Fri, 24 Apr 2020 15:25:27 +1000 Subject: [PATCH 01/17] add Staff.list --- Gemfile.lock | 17 +++++++++ lib/xpm_ruby.rb | 2 +- lib/xpm_ruby/staff.rb | 32 +++++++++++++++++ lib/xpm_ruby/staff/model.rb | 23 +++++++++++++ lib/xpm_ruby/staff/sax_parser.rb | 59 ++++++++++++++++++++++++++++++++ spec/staff_spec.rb | 21 ++++++++++++ xpm_ruby.gemspec | 6 ++++ 7 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 lib/xpm_ruby/staff.rb create mode 100644 lib/xpm_ruby/staff/model.rb create mode 100644 lib/xpm_ruby/staff/sax_parser.rb create mode 100644 spec/staff_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 2d9773a..bdeb076 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,16 +2,31 @@ PATH remote: . specs: xpm_ruby (0.1.0) + faraday (~> 1) + ox (~> 2.13) GEM remote: https://rubygems.org/ specs: ast (2.4.0) + byebug (11.1.3) + coderay (1.1.2) diff-lcs (1.3) + faraday (1.0.1) + multipart-post (>= 1.2, < 3) jaro_winkler (1.5.4) + method_source (1.0.0) + multipart-post (2.1.1) + ox (2.13.2) parallel (1.19.1) parser (2.7.0.5) ast (~> 2.4.0) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.9.0) + byebug (~> 11.0) + pry (~> 0.13.0) rainbow (3.0.0) rake (13.0.1) rspec (3.9.0) @@ -44,6 +59,8 @@ PLATFORMS DEPENDENCIES bundler (~> 2.0) + byebug (~> 11) + pry-byebug (~> 3) rake (>= 12.3.3) rspec (~> 3.0) rubocop (= 0.77.0) diff --git a/lib/xpm_ruby.rb b/lib/xpm_ruby.rb index a0b5298..3ed809d 100644 --- a/lib/xpm_ruby.rb +++ b/lib/xpm_ruby.rb @@ -1,6 +1,6 @@ require "xpm_ruby/version" +require "xpm_ruby/staff" module XpmRuby class Error < StandardError; end - # Your code goes here... end diff --git a/lib/xpm_ruby/staff.rb b/lib/xpm_ruby/staff.rb new file mode 100644 index 0000000..ce467eb --- /dev/null +++ b/lib/xpm_ruby/staff.rb @@ -0,0 +1,32 @@ +require "faraday" +require "base64" +require "ox" + +require_relative "staff/model" +require_relative "staff/sax_parser" + +module XpmRuby + module Staff + extend self + + def build(**test) + Model.new(test) + end + + def list(api_key:, account_key:) + key = Base64.strict_encode64("#{api_key}:#{account_key}") + + http_response = Faraday + .new( + url: "https://api.workflowmax.com/v3", + headers: { "Authorization" => "Basic #{key}" }) + .get("staff.api/list") + + io = StringIO.new(http_response.body) + sax_parser = SaxParser.new + Ox.sax_parse(sax_parser, io) + + sax_parser.staff_list + end + end +end diff --git a/lib/xpm_ruby/staff/model.rb b/lib/xpm_ruby/staff/model.rb new file mode 100644 index 0000000..0338785 --- /dev/null +++ b/lib/xpm_ruby/staff/model.rb @@ -0,0 +1,23 @@ +class Model + attr_accessor :uuid, :name, :email, :phone, :mobile, :address, :payroll_code + + def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, + address: nil, payroll_code: nil) + @uuid = uuid + @name = name + @email = email + @phone = phone + @mobile = mobile + @address = address + @payroll_code + end + + def ==(other) + uuid == other.uuid + end + + def eql?(other) + self == other + end +end + diff --git a/lib/xpm_ruby/staff/sax_parser.rb b/lib/xpm_ruby/staff/sax_parser.rb new file mode 100644 index 0000000..87099b4 --- /dev/null +++ b/lib/xpm_ruby/staff/sax_parser.rb @@ -0,0 +1,59 @@ +module XpmRuby + module Staff + class SaxParser < Ox::Sax + attr_reader :staff_list + + def initialize + @staff_list = [] + @element_names = [] + + super + end + + def start_element(name) + # puts "start: #{name}" + + @element_names.push(name) + + case name + when :Staff + @staff_list << Model.new + end + end + + def end_element(name) + # puts "end: #{name}" + + @element_names.pop + end + + def attr(name, value) + # puts " #{name} => #{value}" + end + + def text(value) + # puts "text #{value}" + + return if value.empty? + + case @element_names.last + when :UUID + @staff_list.last.uuid = value + when :Name + @staff_list.last.name = value + when :Email + @staff_list.last.email = value + when :Phone + @staff_list.last.phone = value + when :Mobile + @staff_list.last.mobile = value + when :Address + @staff_list.last.address = value + when :PayrollCode + @staff_list.last.payroll_code = value + end + end + end + end +end + diff --git a/spec/staff_spec.rb b/spec/staff_spec.rb new file mode 100644 index 0000000..90e77f3 --- /dev/null +++ b/spec/staff_spec.rb @@ -0,0 +1,21 @@ +require "spec_helper" + +module XpmRuby + RSpec.describe(Staff) do + describe ".list" do + let(:api_key) { "" } + let(:account_key) { "" } + + xit "lists staff" do + expect( + Staff + .list(api_key: api_key, account_key: account_key) + ).to include( + Staff.build( + name: "Dev Testing", + email: "dev@practiceignition.com", + uuid: "8c79202e-caf8-4a12-a3d7-ac338e50741f")) + end + end + end +end diff --git a/xpm_ruby.gemspec b/xpm_ruby.gemspec index 952730d..526c780 100644 --- a/xpm_ruby.gemspec +++ b/xpm_ruby.gemspec @@ -25,10 +25,16 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] + spec.add_runtime_dependency("faraday", "~> 1") + spec.add_runtime_dependency("ox", "~> 2.13") + spec.add_development_dependency("bundler", "~> 2.0") spec.add_development_dependency("rake", ">= 12.3.3") spec.add_development_dependency("rspec", "~> 3.0") spec.add_development_dependency("rubocop", "0.77.0") spec.add_development_dependency("rubocop-rspec") + + spec.add_development_dependency("byebug", "~> 11") + spec.add_development_dependency("pry-byebug", "~> 3") end From b8652e200069728c01ada3290649f8474bcbbd3b Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Fri, 24 Apr 2020 15:44:37 +1000 Subject: [PATCH 02/17] fix model namespace --- lib/xpm_ruby/staff/model.rb | 39 ++++++++++++++++++++----------------- spec/staff_spec.rb | 3 +-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/xpm_ruby/staff/model.rb b/lib/xpm_ruby/staff/model.rb index 0338785..4f41436 100644 --- a/lib/xpm_ruby/staff/model.rb +++ b/lib/xpm_ruby/staff/model.rb @@ -1,23 +1,26 @@ -class Model - attr_accessor :uuid, :name, :email, :phone, :mobile, :address, :payroll_code +module XpmRuby + module Staff + class Model + attr_accessor :uuid, :name, :email, :phone, :mobile, :address, :payroll_code - def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, - address: nil, payroll_code: nil) - @uuid = uuid - @name = name - @email = email - @phone = phone - @mobile = mobile - @address = address - @payroll_code - end + def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, + address: nil, payroll_code: nil) + @uuid = uuid + @name = name + @email = email + @phone = phone + @mobile = mobile + @address = address + @payroll_code + end - def ==(other) - uuid == other.uuid - end + def ==(other) + uuid == other.uuid + end - def eql?(other) - self == other + def eql?(other) + self == other + end + end end end - diff --git a/spec/staff_spec.rb b/spec/staff_spec.rb index 90e77f3..86f925d 100644 --- a/spec/staff_spec.rb +++ b/spec/staff_spec.rb @@ -8,8 +8,7 @@ module XpmRuby xit "lists staff" do expect( - Staff - .list(api_key: api_key, account_key: account_key) + Staff.list(api_key: api_key, account_key: account_key) ).to include( Staff.build( name: "Dev Testing", From dacb708c369bd5a6dd2a05aaeaec5862848c3053 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Fri, 24 Apr 2020 15:50:13 +1000 Subject: [PATCH 03/17] remove extra line --- lib/xpm_ruby/staff/sax_parser.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/xpm_ruby/staff/sax_parser.rb b/lib/xpm_ruby/staff/sax_parser.rb index 87099b4..d6e12ac 100644 --- a/lib/xpm_ruby/staff/sax_parser.rb +++ b/lib/xpm_ruby/staff/sax_parser.rb @@ -56,4 +56,3 @@ def text(value) end end end - From b11ff55f90a275e0f4b3132f5292a2b9cd7ec313 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 15:10:26 +1000 Subject: [PATCH 04/17] drop sax parser --- lib/xpm_ruby.rb | 1 - .../{staff/model.rb => models/staff.rb} | 4 +- lib/xpm_ruby/staff.rb | 42 ++++++++++---- lib/xpm_ruby/staff/sax_parser.rb | 58 ------------------- spec/staff/parser_spec.rb | 25 ++++++++ spec/staff_spec.rb | 33 +++++++---- 6 files changed, 82 insertions(+), 81 deletions(-) rename lib/xpm_ruby/{staff/model.rb => models/staff.rb} (94%) delete mode 100644 lib/xpm_ruby/staff/sax_parser.rb create mode 100644 spec/staff/parser_spec.rb diff --git a/lib/xpm_ruby.rb b/lib/xpm_ruby.rb index 3ed809d..a4307e4 100644 --- a/lib/xpm_ruby.rb +++ b/lib/xpm_ruby.rb @@ -2,5 +2,4 @@ require "xpm_ruby/staff" module XpmRuby - class Error < StandardError; end end diff --git a/lib/xpm_ruby/staff/model.rb b/lib/xpm_ruby/models/staff.rb similarity index 94% rename from lib/xpm_ruby/staff/model.rb rename to lib/xpm_ruby/models/staff.rb index 4f41436..98ebeb1 100644 --- a/lib/xpm_ruby/staff/model.rb +++ b/lib/xpm_ruby/models/staff.rb @@ -1,6 +1,6 @@ module XpmRuby - module Staff - class Model + module Models + class Staff attr_accessor :uuid, :name, :email, :phone, :mobile, :address, :payroll_code def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, diff --git a/lib/xpm_ruby/staff.rb b/lib/xpm_ruby/staff.rb index ce467eb..5c89cf1 100644 --- a/lib/xpm_ruby/staff.rb +++ b/lib/xpm_ruby/staff.rb @@ -2,31 +2,53 @@ require "base64" require "ox" -require_relative "staff/model" -require_relative "staff/sax_parser" +require_relative "models/staff" module XpmRuby module Staff extend self - def build(**test) - Model.new(test) + class Error < StandardError; end + class Unauthorized < Error; end + + def build(**args) + Models::Staff.new(args) end def list(api_key:, account_key:) key = Base64.strict_encode64("#{api_key}:#{account_key}") - http_response = Faraday + response = Faraday .new( url: "https://api.workflowmax.com/v3", - headers: { "Authorization" => "Basic #{key}" }) + headers: { + "Authorization" => "Basic #{key}" }) .get("staff.api/list") - io = StringIO.new(http_response.body) - sax_parser = SaxParser.new - Ox.sax_parse(sax_parser, io) + hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) - sax_parser.staff_list + case response.status + when 401 + raise Unauthorized.new(hash["html"]["head"]["title"]) + when 200 + case hash["Response"]["Status"] + when "OK" + hash["Response"]["StaffList"]["Staff"].map do |staff| + Models::Staff.new( + uuid: staff["UUID"], + name: staff["Name"], + email: staff["Email"], + phone: staff["Phone"], + mobile: staff["Mobile"], + address: staff["Address"], + payroll_code: staff["PayrollCode"]) + end + when "ERROR" + raise Error.new(response["ErrorDescription"]) + end + else + raise Error.new(response.status) + end end end end diff --git a/lib/xpm_ruby/staff/sax_parser.rb b/lib/xpm_ruby/staff/sax_parser.rb deleted file mode 100644 index d6e12ac..0000000 --- a/lib/xpm_ruby/staff/sax_parser.rb +++ /dev/null @@ -1,58 +0,0 @@ -module XpmRuby - module Staff - class SaxParser < Ox::Sax - attr_reader :staff_list - - def initialize - @staff_list = [] - @element_names = [] - - super - end - - def start_element(name) - # puts "start: #{name}" - - @element_names.push(name) - - case name - when :Staff - @staff_list << Model.new - end - end - - def end_element(name) - # puts "end: #{name}" - - @element_names.pop - end - - def attr(name, value) - # puts " #{name} => #{value}" - end - - def text(value) - # puts "text #{value}" - - return if value.empty? - - case @element_names.last - when :UUID - @staff_list.last.uuid = value - when :Name - @staff_list.last.name = value - when :Email - @staff_list.last.email = value - when :Phone - @staff_list.last.phone = value - when :Mobile - @staff_list.last.mobile = value - when :Address - @staff_list.last.address = value - when :PayrollCode - @staff_list.last.payroll_code = value - end - end - end - end -end diff --git a/spec/staff/parser_spec.rb b/spec/staff/parser_spec.rb new file mode 100644 index 0000000..7cdcc61 --- /dev/null +++ b/spec/staff/parser_spec.rb @@ -0,0 +1,25 @@ +require "spec_helper" + +module XpmRuby + module Staff + RSpec.describe(Parser) do + describe "#parse" + context "when response status error" do + let(:io) do StringIO.new(%{ + + ERROR + A detailed explanation of the error + + }) + end + + it "raises error" do + expect(Parser.parse(io: io)) + .to raise_error(Parser::Error, "A detailed explanation of the error") + end + end + + context "when response status ok" + end + end +end diff --git a/spec/staff_spec.rb b/spec/staff_spec.rb index 86f925d..c304e9b 100644 --- a/spec/staff_spec.rb +++ b/spec/staff_spec.rb @@ -3,17 +3,30 @@ module XpmRuby RSpec.describe(Staff) do describe ".list" do - let(:api_key) { "" } - let(:account_key) { "" } + context "when keys invalid" do + let(:api_key) { "" } + let(:account_key) { "" } - xit "lists staff" do - expect( - Staff.list(api_key: api_key, account_key: account_key) - ).to include( - Staff.build( - name: "Dev Testing", - email: "dev@practiceignition.com", - uuid: "8c79202e-caf8-4a12-a3d7-ac338e50741f")) + xit "raises unauthorized error" do + expect do + Staff.list(api_key: api_key, account_key: account_key) + end.to raise_error(Staff::Unauthorized) + end + end + + context "when keys valid" do + let(:api_key) { "" } + let(:account_key) { "" } + + xit "lists staff" do + expect( + Staff.list(api_key: api_key, account_key: account_key) + ).to include( + Staff.build( + name: "Dev Testing", + email: "dev@practiceignition.com", + uuid: "8c79202e-caf8-4a12-a3d7-ac338e50741f")) + end end end end From d8399bf2400d7f974651e3ad9ee6a64e0af16f9d Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 15:13:44 +1000 Subject: [PATCH 05/17] remove parser spec --- spec/staff/parser_spec.rb | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 spec/staff/parser_spec.rb diff --git a/spec/staff/parser_spec.rb b/spec/staff/parser_spec.rb deleted file mode 100644 index 7cdcc61..0000000 --- a/spec/staff/parser_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -require "spec_helper" - -module XpmRuby - module Staff - RSpec.describe(Parser) do - describe "#parse" - context "when response status error" do - let(:io) do StringIO.new(%{ - - ERROR - A detailed explanation of the error - - }) - end - - it "raises error" do - expect(Parser.parse(io: io)) - .to raise_error(Parser::Error, "A detailed explanation of the error") - end - end - - context "when response status ok" - end - end -end From 66282cc26545fc145088019322cc6ecd8e28121d Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 15:15:22 +1000 Subject: [PATCH 06/17] optimise --- lib/xpm_ruby/staff.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/xpm_ruby/staff.rb b/lib/xpm_ruby/staff.rb index 5c89cf1..6641347 100644 --- a/lib/xpm_ruby/staff.rb +++ b/lib/xpm_ruby/staff.rb @@ -21,16 +21,17 @@ def list(api_key:, account_key:) response = Faraday .new( url: "https://api.workflowmax.com/v3", - headers: { - "Authorization" => "Basic #{key}" }) + headers: { "Authorization" => "Basic #{key}" }) .get("staff.api/list") - hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) - case response.status when 401 + hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) + raise Unauthorized.new(hash["html"]["head"]["title"]) when 200 + hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) + case hash["Response"]["Status"] when "OK" hash["Response"]["StaffList"]["Staff"].map do |staff| From d9e61cf98fcc7808b638393ee46857cc458ca8d6 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 15:35:37 +1000 Subject: [PATCH 07/17] fix payroll_code --- lib/xpm_ruby/models/staff.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/xpm_ruby/models/staff.rb b/lib/xpm_ruby/models/staff.rb index 98ebeb1..32a3bf9 100644 --- a/lib/xpm_ruby/models/staff.rb +++ b/lib/xpm_ruby/models/staff.rb @@ -11,7 +11,7 @@ def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, @phone = phone @mobile = mobile @address = address - @payroll_code + @payroll_code = payroll_code end def ==(other) From a02bae0db609f3c935db23a7c03e41c87ce1e8c1 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 16:38:02 +1000 Subject: [PATCH 08/17] commit unfinished changes --- lib/xpm_ruby/models/client.rb | 99 +++++++++++++++++++++++++++++++++++ spec/client_spec.rb | 33 ++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 lib/xpm_ruby/models/client.rb create mode 100644 spec/client_spec.rb diff --git a/lib/xpm_ruby/models/client.rb b/lib/xpm_ruby/models/client.rb new file mode 100644 index 0000000..09d00fd --- /dev/null +++ b/lib/xpm_ruby/models/client.rb @@ -0,0 +1,99 @@ +:name, :email, :address, :city, :region, :post_code, :country, +:postal_address, :postal_city, :postal_region, :postal_post_code, :postal_country +:phone, :fax, :web_site, :referral_source, :export_code, :is_prospect +:account_manager_uuid, :contacts, + +:name, :is_primary, :salutation, :addressee, :phone, :mobile, :email, :position + + + Bloggs Electrical Ltd + +
+ + + + + + + + + + + + + + + No + + + + Jo Bloggs + yes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+module XpmRuby + module Models + class Client + attr_accessor :uuid, :name, :email, :phone, :mobile, :address, :payroll_code + + def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, + address: nil, payroll_code: nil) + @uuid = uuid + @name = name + @email = email + @phone = phone + @mobile = mobile + @address = address + @payroll_code = payroll_code + end + + def ==(other) + uuid == other.uuid + end + + def eql?(other) + self == other + end + end + end +end diff --git a/spec/client_spec.rb b/spec/client_spec.rb new file mode 100644 index 0000000..69a32c6 --- /dev/null +++ b/spec/client_spec.rb @@ -0,0 +1,33 @@ +require "spec_helper" + +module XpmRuby + RSpec.describe(Client) do + describe ".add" do + context "when keys invalid" do + let(:api_key) { "" } + let(:account_key) { "" } + + xit "raises unauthorized error" do + expect do + client.list(api_key: api_key, account_key: account_key) + end.to raise_error(Client::Unauthorized) + end + end + + context "when keys valid" do + let(:api_key) { "" } + let(:account_key) { "" } + + xit "adds client" do + expect( + Client.add(api_key: api_key, account_key: account_key) + ).to include( + Staff.build( + name: "Dev Testing", + email: "dev@practiceignition.com", + uuid: "8c79202e-caf8-4a12-a3d7-ac338e50741f")) + end + end + end + end +end From f107d834454cfe9647c98d05dc42b11a76aecb2f Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 20:27:25 +1000 Subject: [PATCH 09/17] add incomplete spec --- lib/xpm_ruby/client.rb | 55 +++++++++++++++++++++++++++++++++++ lib/xpm_ruby/models/client.rb | 31 ++++++++++++++++++++ spec/client_spec.rb | 38 +++++++++++++++++++----- 3 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 lib/xpm_ruby/client.rb diff --git a/lib/xpm_ruby/client.rb b/lib/xpm_ruby/client.rb new file mode 100644 index 0000000..898218d --- /dev/null +++ b/lib/xpm_ruby/client.rb @@ -0,0 +1,55 @@ +require "faraday" +require "base64" +require "ox" + +require_relative "models/client" + +module XpmRuby + module Client + extend self + + class Error < StandardError; end + class Unauthorized < Error; end + + def build(**args) + Models::Client.new(args) + end + + def list(api_key:, account_key:) + key = Base64.strict_encode64("#{api_key}:#{account_key}") + + response = Faraday + .new( + url: "https://api.workflowmax.com/v3", + headers: { "Authorization" => "Basic #{key}" }) + .get("staff.api/list") + + case response.status + when 401 + hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) + + raise Unauthorized.new(hash["html"]["head"]["title"]) + when 200 + hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) + + case hash["Response"]["Status"] + when "OK" + hash["Response"]["StaffList"]["Staff"].map do |staff| + Models::Staff.new( + uuid: staff["UUID"], + name: staff["Name"], + email: staff["Email"], + phone: staff["Phone"], + mobile: staff["Mobile"], + address: staff["Address"], + payroll_code: staff["PayrollCode"]) + end + when "ERROR" + raise Error.new(response["ErrorDescription"]) + end + else + raise Error.new(response.status) + end + end + end +end diff --git a/lib/xpm_ruby/models/client.rb b/lib/xpm_ruby/models/client.rb index 09d00fd..726a92b 100644 --- a/lib/xpm_ruby/models/client.rb +++ b/lib/xpm_ruby/models/client.rb @@ -5,6 +5,37 @@ :name, :is_primary, :salutation, :addressee, :phone, :mobile, :email, :position +:billing_client_uuid, :first_name, :last_name, :other_name, :date_of_birth +:job_manager_uuid, :tax_number, :company_number, :business_number, :business_structure + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Bloggs Electrical Ltd diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 69a32c6..cbca592 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -9,23 +9,45 @@ module XpmRuby xit "raises unauthorized error" do expect do - client.list(api_key: api_key, account_key: account_key) - end.to raise_error(Client::Unauthorized) + client.add(api_key: api_key, account_key: account_key) + end.to raise_error(Unauthorized) end end context "when keys valid" do let(:api_key) { "" } let(:account_key) { "" } + let(:client) do + Client.build( + name: "Accounting Pty Ltd", + email: "hello@accounting.com", + address: "1 Address Place", + city: "Sydney", + region: "NSW", + post_code: "2000", + country: "Australia", + postal_address: "1 Postal Address", + postal_city: "Melbourne", + postal_region: "VIC", + postal_post_code: "3000", + postal_country: "Australia", + account_manager_uuid: "", + contacts: [ + Client::Contact.build( + name: "Adam", + is_primary: false, + salutation: "MR", + addresse: "?", + phone: + )) + end xit "adds client" do + Client.add(api_key: api_key, account_key: account_key, client: client) + expect( - Client.add(api_key: api_key, account_key: account_key) - ).to include( - Staff.build( - name: "Dev Testing", - email: "dev@practiceignition.com", - uuid: "8c79202e-caf8-4a12-a3d7-ac338e50741f")) + Client.list(api_key: api_key, account_key: account_key) + ).to include(client) end end end From 8d5537574e020fcd812900b2e51072082ae5bbe3 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Mon, 27 Apr 2020 15:56:48 +1000 Subject: [PATCH 10/17] initial commit --- lib/xpm_ruby/client.rb | 16 +- lib/xpm_ruby/models/client.rb | 385 +++++++++++++++++++++++++--------- lib/xpm_ruby/staff.rb | 5 - spec/client_spec.rb | 109 ++++++---- 4 files changed, 360 insertions(+), 155 deletions(-) diff --git a/lib/xpm_ruby/client.rb b/lib/xpm_ruby/client.rb index 898218d..15f926e 100644 --- a/lib/xpm_ruby/client.rb +++ b/lib/xpm_ruby/client.rb @@ -15,14 +15,10 @@ def build(**args) Models::Client.new(args) end - def list(api_key:, account_key:) - key = Base64.strict_encode64("#{api_key}:#{account_key}") - - response = Faraday - .new( - url: "https://api.workflowmax.com/v3", - headers: { "Authorization" => "Basic #{key}" }) - .get("staff.api/list") + def add(api_key:, account_key:) + response = Connection + .new(api_key: api_key, account_key: account_key, api_url: api_url) + .get(endpoint: "client.api/add") case response.status when 401 @@ -34,8 +30,8 @@ def list(api_key:, account_key:) case hash["Response"]["Status"] when "OK" - hash["Response"]["StaffList"]["Staff"].map do |staff| - Models::Staff.new( + hash["Response"]["Clients"]["Client"].map do |client| + Models::Client.new( uuid: staff["UUID"], name: staff["Name"], email: staff["Email"], diff --git a/lib/xpm_ruby/models/client.rb b/lib/xpm_ruby/models/client.rb index 726a92b..3c723e8 100644 --- a/lib/xpm_ruby/models/client.rb +++ b/lib/xpm_ruby/models/client.rb @@ -1,107 +1,302 @@ +# client.api/add +# +# +# Bloggs Electrical Ltd +# +#
+# +# +# +# +# +# +# +# +# +# +# +# +# +# +# No +# +# +# +# Jo Bloggs +# yes +# +# +# +# +# +# +# +# +# + +# + +# +# +# +# + + +# +# +# +# +# +# +# +# +# +# +# +# + +# +# +# +# +# + +# +# +# +#
+ +# client.api/get + +# +# OK +# +# c257b062-fbc9-4dc7-9132-f18ee956c4f9 +# Acmer Pty Ltd +# someone@example.com +# 1970-11-26 +#
+# +# +# +# +# +# Level 32, PWC Building +# 188 Quay Street +# Auckland Central +# +# Auckland +# +# 1001 +# +# (02) 1723 5265 +# +# +# +# +# No +# +# ab3d6db0-f0be-4a3e-bd68-8fad1c6b40bb +# Jo Blogs +# +# +# 20th of Month +# 30.00 +# DayOfMonth +# 20 +# +# +# +# f1ec264f-d269-4232-9615-c167583fa7da +# yes +# Wyett E Coyote +# +# +# +# +# +# +# +# +# +# +# note title +# subject of the note +# +# 2008-09-12T13:00:00 +# Jo Bloggs +# +# +# +# e41ecd7a-ac85-4473-a7b3-052bd670f01e +# Billing Client +# +# +# +# ebc0e2ae-3d3e-4546-9787-8c659e445d31 +# John Smith +# +# +# +# +# +# ******123 +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# +# 9c3abe09-6df2-4f18-8090-36a44e91a1c3 +# Bloggs Family +# +# +# +# +# 9c3abe09-6df2-4f18-8090-36a44e91a1c3 +# Shareholder +# +# 651ec79a-93cb-458c-b5ca-e123c92192b6 +# Bloggs Ltd +# +# 1000 +# 2011-01-01 +# 2013-03-31 +# +# +# +# + :name, :email, :address, :city, :region, :post_code, :country, :postal_address, :postal_city, :postal_region, :postal_post_code, :postal_country :phone, :fax, :web_site, :referral_source, :export_code, :is_prospect -:account_manager_uuid, :contacts, +:account_manager, :account_manager, +:type +:contacts, + +:name, :is_primary, :salutation, :addressee, :phone, :mobile, :email, :position + +:billing_client_uuid, :first_name, :last_name, :other_name, :date_of_birth +:job_manager_uuid, :tax_number, :company_number, :business_number, :business_structure + +:uuid, :name, :email, :address, :city, :region, :post_code, :country, +:postal_address, :postal_city, :postal_region, :postal_post_code, :postal_country +:phone, :fax, :web_site, :referral_source, :export_code, :is_prospect + +:account_manager +:type +:contacts, :name, :is_primary, :salutation, :addressee, :phone, :mobile, :email, :position :billing_client_uuid, :first_name, :last_name, :other_name, :date_of_birth :job_manager_uuid, :tax_number, :company_number, :business_number, :business_structure - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Bloggs Electrical Ltd - -
- - - - - - - - - - - - - - - No - - - - Jo Bloggs - yes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+module XpmRuby + module Models + class Client + class AccountManager + attr_reader :uuid, :name + + def initialize(uuid: nil, name: nil) + @uuid = uuid + @name = name + end + + def ==(other) + uuid == other.uuid + end + + def eql?(other) + self == other + end + end + + class Type + attr_reader :name, :cost_markup, :payment_term, :payment_day + + def initialize(name: nil, cost_markup: nil, payment_term: nil, payment_day: nil) + @name = name + @cost_markup = cost_markup + @payment_term = payment_term + @payment_day = payment_day + end + + def ==(other) + name == other.name && + cost_markup == other.cost_markup && + payment_term == other.payment_term && + payment_day = other.payment_day + end + + def eql?(other) + self == other + end + end + + class Contact + def initialize(uuid: nil, is_primary: nil, name: nil, salutation: nil, + addressee: nil, mobile: nil, email: nil, phone: nil, position: nil) + @uuid = uuid + @is_primary = is_primary + @name = name + @salutation = salutation + @addressee = addressee + @mobile = mobile + @email = email + @phone = phone + @position = position + end + + def ==(other) + uuid == other.uuid + end + + def eql?(other) + self == other + end + end + + attr_accessor :uuid, :name, :email, :phone, :mobile, :address, :payroll_code + + def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, + address: nil, payroll_code: nil) + @uuid = uuid + @name = name + @email = email + @phone = phone + @mobile = mobile + @address = address + @payroll_code = payroll_code + end + + def ==(other) + uuid == other.uuid + end + + def eql?(other) + self == other + end + end + end +end + module XpmRuby module Models class Client diff --git a/lib/xpm_ruby/staff.rb b/lib/xpm_ruby/staff.rb index 2b99884..0084f6f 100644 --- a/lib/xpm_ruby/staff.rb +++ b/lib/xpm_ruby/staff.rb @@ -1,8 +1,3 @@ -<<<<<<< HEAD -require "faraday" -require "base64" -======= ->>>>>>> 9655211ed0a4752d21e701d6f1be0566a1f4ec3b require "ox" require_relative "models/staff" diff --git a/spec/client_spec.rb b/spec/client_spec.rb index cbca592..eba78d8 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -3,53 +3,72 @@ module XpmRuby RSpec.describe(Client) do describe ".add" do - context "when keys invalid" do - let(:api_key) { "" } - let(:account_key) { "" } + let(:api_key) { "" } + let(:account_key) { "" } + let(:api_url) { "api.workflowmax.com" } - xit "raises unauthorized error" do - expect do - client.add(api_key: api_key, account_key: account_key) - end.to raise_error(Unauthorized) - end - end - - context "when keys valid" do - let(:api_key) { "" } - let(:account_key) { "" } - let(:client) do - Client.build( - name: "Accounting Pty Ltd", - email: "hello@accounting.com", - address: "1 Address Place", - city: "Sydney", - region: "NSW", - post_code: "2000", - country: "Australia", - postal_address: "1 Postal Address", - postal_city: "Melbourne", - postal_region: "VIC", - postal_post_code: "3000", - postal_country: "Australia", - account_manager_uuid: "", - contacts: [ - Client::Contact.build( - name: "Adam", - is_primary: false, - salutation: "MR", - addresse: "?", - phone: - )) - end - - xit "adds client" do - Client.add(api_key: api_key, account_key: account_key, client: client) + let(:client) do + Client.build( + name: "Acmer Pty Ltd", + email: "someone@example.com", + address: "1 Address Place", + city: "Sydney", + region: "NSW", + post_code: "2000", + country: "Australia", + postal_address: " + Level 32, PWC Building + 188 Quay Street + Auckland Central", + postal_city: "Auckland", + postal_region: "North Island", + postal_post_code: "1001", + postal_country: "New Zealand", + phone: "00 0000 0000", + fax: "00 1000 0000", + web_site: "http://example.com", + referral_source: "referrer", + export_code: "EXPORT_CODE", + is_prospect: false, + account_manager: "", + contacts: [ + Client::Contact.build( + name: "Someone else", + is_primary: true, + salutation: "MR", + addressee: "?", + phone: "02 0000 0000", + mobile: "0411 0000 0000", + email: "someone.else@example.com", + position: "Manager")], + billing_client_uuid: "", + first_name: "Someone", + last_name: "Person", + date_of_birth: Date.parse("1987/05/11"), + job_manager_uuid: "" + tax_number: "11 1111 1111", + company_number: "02 1000 1000", + business_number: "02 1000 2000", + business_structure: "Corporate", + balance_month: "Jan", + prepare_gst: false, + gst_registered: true, + gst_period: 6, + gst_basis: "Invoice", + provisional_tax_basis: Client::ProvisionalTaxBasis::STANDARD, + provisional_tax_ratio: "1", + signed_tax_authority: true, + tax_agent: "Mr Tax Agent", + agency_status: Client::AgencyStatus::WITH_EOT, + return_type: Client::ReturnType::IR3, + prepare_activity_statement: true, + prepare_tax_return: true) + end - expect( - Client.list(api_key: api_key, account_key: account_key) - ).to include(client) - end - end + it "adds client" do + expect( + Client.add(api_key: api_key, account_key: account_key, api_url: api_url) + ).to include(client) end end end From 68a1c4df97051a197b4c403f8527dcc0e2839cc9 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Mon, 27 Apr 2020 16:25:18 +1000 Subject: [PATCH 11/17] update spec --- spec/client_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index eba78d8..5c9ded4 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -67,8 +67,12 @@ module XpmRuby it "adds client" do expect( - Client.add(api_key: api_key, account_key: account_key, api_url: api_url) - ).to include(client) + Client.add( + api_key: api_key, + account_key: account_key, + api_url: api_url, + client: client) + ).to eql(client) end end end From bdd04f0eead83ca53a5604d94911fba29908d875 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Mon, 27 Apr 2020 19:03:00 +1000 Subject: [PATCH 12/17] commit changes --- lib/xpm_ruby.rb | 1 + lib/xpm_ruby/client.rb | 48 +++++++++++- lib/xpm_ruby/connection.rb | 4 + lib/xpm_ruby/models/client.rb | 142 +++++++++++++++++----------------- spec/client_spec.rb | 78 ------------------- spec/xpm_ruby/client_spec.rb | 78 +++++++++++++++++++ 6 files changed, 198 insertions(+), 153 deletions(-) delete mode 100644 spec/client_spec.rb create mode 100644 spec/xpm_ruby/client_spec.rb diff --git a/lib/xpm_ruby.rb b/lib/xpm_ruby.rb index edeb489..8c09e88 100644 --- a/lib/xpm_ruby.rb +++ b/lib/xpm_ruby.rb @@ -6,3 +6,4 @@ class Unauthorized < Error; end require "xpm_ruby/version" require "xpm_ruby/connection" require "xpm_ruby/staff" +require "xpm_ruby/client" diff --git a/lib/xpm_ruby/client.rb b/lib/xpm_ruby/client.rb index 15f926e..47adc67 100644 --- a/lib/xpm_ruby/client.rb +++ b/lib/xpm_ruby/client.rb @@ -8,6 +8,48 @@ module XpmRuby module Client extend self + module GstPeriod + ONE = "1" + TWO = "2" + SIX = "6" + end + + module GstBasis + INVOICE = "Invoice" + PAYMENT = "Payment" + HYBRID = "Hybrid" + end + + module ProvisionalTaxBasis + STANDARD_OPTION = "Standard Option" + ESTIMATE_OPTION = "Estimate Option" + RATIO_OPTION = "Ratio Option" + end + + module AgencyStatus + WITH_EOT = "With EOT" + WITHOUT_EOT = "Without EOT" + UNLINKED = "Unlinked" + end + + module ReturnType + IR3 = "IR3" + IR3NR = "IR3NR" + IR4 = "IR4" + IR6 = "IR6" + IR7 = "IR7" + IR9 = "IR9" + PTS = "PTS" + end + + module Contact + extend self + + def build(**args) + Models::Client::Contact.new(args) + end + end + class Error < StandardError; end class Unauthorized < Error; end @@ -15,10 +57,12 @@ def build(**args) Models::Client.new(args) end - def add(api_key:, account_key:) + def add(api_key:, account_key:, api_url:, client:) + # xml = Ox.dump(client, indent: 2, effort: :strict) + response = Connection .new(api_key: api_key, account_key: account_key, api_url: api_url) - .get(endpoint: "client.api/add") + .post(endpoint: "client.api/add", body: xml) case response.status when 401 diff --git a/lib/xpm_ruby/connection.rb b/lib/xpm_ruby/connection.rb index 9176617..24a841c 100644 --- a/lib/xpm_ruby/connection.rb +++ b/lib/xpm_ruby/connection.rb @@ -16,6 +16,10 @@ def get(endpoint:) Faraday.new(url(endpoint: endpoint), headers: headers).get end + def post(endpoint:, body:) + Faraday.new(url(endpoint: endpoint), headers: headers).post(body) + end + private def headers diff --git a/lib/xpm_ruby/models/client.rb b/lib/xpm_ruby/models/client.rb index 3c723e8..b17848f 100644 --- a/lib/xpm_ruby/models/client.rb +++ b/lib/xpm_ruby/models/client.rb @@ -183,34 +183,78 @@ # # -:name, :email, :address, :city, :region, :post_code, :country, -:postal_address, :postal_city, :postal_region, :postal_post_code, :postal_country -:phone, :fax, :web_site, :referral_source, :export_code, :is_prospect -:account_manager, :account_manager, -:type -:contacts, - -:name, :is_primary, :salutation, :addressee, :phone, :mobile, :email, :position - -:billing_client_uuid, :first_name, :last_name, :other_name, :date_of_birth -:job_manager_uuid, :tax_number, :company_number, :business_number, :business_structure - -:uuid, :name, :email, :address, :city, :region, :post_code, :country, -:postal_address, :postal_city, :postal_region, :postal_post_code, :postal_country -:phone, :fax, :web_site, :referral_source, :export_code, :is_prospect - -:account_manager -:type -:contacts, - -:name, :is_primary, :salutation, :addressee, :phone, :mobile, :email, :position - -:billing_client_uuid, :first_name, :last_name, :other_name, :date_of_birth -:job_manager_uuid, :tax_number, :company_number, :business_number, :business_structure - module XpmRuby module Models class Client + attr_accessor :name, :email, :address, :city, :region, :post_code, :country, + :postal_address, :postal_city, :postal_region, :postal_post_code, :postal_country, + :phone, :fax, :web_site, :referral_source, :export_code, :is_prospect, + :account_manager_uuid, :contacts, :billing_client_uuid, + :first_name, :last_name, :other_name, :date_of_birth, :job_manager_uuid, + :tax_number, :company_number, :business_number, :business_structure, :balance_month, + :prepare_gst, :gst_registered, :gst_period, :gst_basis, + :provisional_tax_basis, :provisional_tax_ratio, + :signed_tax_authority, :tax_agent, :agency_status, :return_type, + :prepare_activity_statement, :prepare_tax_return + + def initialize(name:, email: nil, + address: nil, city: nil, region: nil, post_code: nil, country: nil, + postal_address: nil, postal_city: nil, postal_region: nil, + postal_post_code: nil, postal_country: nil, + phone: nil, fax: nil, web_site: nil, + referral_source: nil, export_code: nil, is_prospect: nil, + account_manager_uuid: nil, contacts: nil, billing_client_uuid: nil, + first_name: nil, last_name: nil, other_name: nil, date_of_birth: nil, + job_manager_uuid: nil, tax_number: nil, company_number: nil, + business_number: nil, business_structure: nil, balance_month: nil, + prepare_gst: nil, gst_registered: nil, gst_period: nil, gst_basis: nil, + provisional_tax_basis: nil, provisional_tax_ratio: nil, + signed_tax_authority: nil, tax_agent: nil, agency_status: nil, + return_type: nil, prepare_activity_statement: nil, prepare_tax_return: nil) + @name = email + @address = address + @city = city + @region = region + @post_code = post_code + @country = country + @postal_address = postal_address + @postal_city = postal_city + @postal_region = postal_region + @postal_post_code = postal_post_code + @postal_country = postal_country + @phone = phone + @fax = fax + @web_site = web_site + @referral_source = referral_source + @export_code = export_code + @is_prospect = is_prospect + @account_manager_uuid = account_manager_uuid + @contacts = contacts + @billing_client_uuid = billing_client_uuid + @first_name = first_name + @last_name = last_name + @other_name = other_name + @date_of_birth = date_of_birth + @job_manager_uuid = job_manager_uuid + @tax_number = tax_number + @company_number = company_number + @business_number = business_number + @business_structure = business_structure + @balance_month = balance_month + @prepare_gst = prepare_gst + @gst_registered = gst_registered + @gst_period = gst_period + @gst_basis = gst_basis + @provisional_tax_basis = provisional_tax_basis + @provisional_tax_rate = provisional_tax_ratio + @signed_tax_authority = signed_tax_authority + @tax_agent = tax_agent + @agency_status = agency_status + @return_type = return_type + @prepare_activity_statement = prepare_activity_statement + @prepare_tax_return = prepare_tax_return + end + class AccountManager attr_reader :uuid, :name @@ -272,54 +316,6 @@ def eql?(other) self == other end end - - attr_accessor :uuid, :name, :email, :phone, :mobile, :address, :payroll_code - - def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, - address: nil, payroll_code: nil) - @uuid = uuid - @name = name - @email = email - @phone = phone - @mobile = mobile - @address = address - @payroll_code = payroll_code - end - - def ==(other) - uuid == other.uuid - end - - def eql?(other) - self == other - end - end - end -end - -module XpmRuby - module Models - class Client - attr_accessor :uuid, :name, :email, :phone, :mobile, :address, :payroll_code - - def initialize(uuid: nil, name: nil, email: nil, phone: nil, mobile: nil, - address: nil, payroll_code: nil) - @uuid = uuid - @name = name - @email = email - @phone = phone - @mobile = mobile - @address = address - @payroll_code = payroll_code - end - - def ==(other) - uuid == other.uuid - end - - def eql?(other) - self == other - end end end end diff --git a/spec/client_spec.rb b/spec/client_spec.rb deleted file mode 100644 index 5c9ded4..0000000 --- a/spec/client_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -require "spec_helper" - -module XpmRuby - RSpec.describe(Client) do - describe ".add" do - let(:api_key) { "" } - let(:account_key) { "" } - let(:api_url) { "api.workflowmax.com" } - - let(:client) do - Client.build( - name: "Acmer Pty Ltd", - email: "someone@example.com", - address: "1 Address Place", - city: "Sydney", - region: "NSW", - post_code: "2000", - country: "Australia", - postal_address: " - Level 32, PWC Building - 188 Quay Street - Auckland Central", - postal_city: "Auckland", - postal_region: "North Island", - postal_post_code: "1001", - postal_country: "New Zealand", - phone: "00 0000 0000", - fax: "00 1000 0000", - web_site: "http://example.com", - referral_source: "referrer", - export_code: "EXPORT_CODE", - is_prospect: false, - account_manager: "", - contacts: [ - Client::Contact.build( - name: "Someone else", - is_primary: true, - salutation: "MR", - addressee: "?", - phone: "02 0000 0000", - mobile: "0411 0000 0000", - email: "someone.else@example.com", - position: "Manager")], - billing_client_uuid: "", - first_name: "Someone", - last_name: "Person", - date_of_birth: Date.parse("1987/05/11"), - job_manager_uuid: "" - tax_number: "11 1111 1111", - company_number: "02 1000 1000", - business_number: "02 1000 2000", - business_structure: "Corporate", - balance_month: "Jan", - prepare_gst: false, - gst_registered: true, - gst_period: 6, - gst_basis: "Invoice", - provisional_tax_basis: Client::ProvisionalTaxBasis::STANDARD, - provisional_tax_ratio: "1", - signed_tax_authority: true, - tax_agent: "Mr Tax Agent", - agency_status: Client::AgencyStatus::WITH_EOT, - return_type: Client::ReturnType::IR3, - prepare_activity_statement: true, - prepare_tax_return: true) - end - - it "adds client" do - expect( - Client.add( - api_key: api_key, - account_key: account_key, - api_url: api_url, - client: client) - ).to eql(client) - end - end -end diff --git a/spec/xpm_ruby/client_spec.rb b/spec/xpm_ruby/client_spec.rb new file mode 100644 index 0000000..5562cf8 --- /dev/null +++ b/spec/xpm_ruby/client_spec.rb @@ -0,0 +1,78 @@ +require "spec_helper" + +module XpmRuby + RSpec.describe(Client) do + describe ".add" do + let(:api_key) { "" } + let(:account_key) { "" } + let(:api_url) { "api.workflowmax.com" } + + let(:client) do + Client.build( + name: "Acmer Pty Ltd", + email: "someone@example.com", + address: "1 Address Place", + city: "Sydney", + region: "NSW", + post_code: "2000", + country: "Australia", + postal_address: "Level 32, PWC Building\n188 Quay Street\nAuckland Central", + postal_city: "Auckland", + postal_region: "North Island", + postal_post_code: "1001", + postal_country: "New Zealand", + phone: "00 0000 0000", + fax: "00 1000 0000", + web_site: "http://example.com", + referral_source: "referrer", + export_code: "EXPORT_CODE", + is_prospect: false, + account_manager_uuid: nil, + contacts: [ + Client::Contact.build( + name: "Someone else", + is_primary: true, + salutation: "MR", + addressee: "Addressee", + phone: "02 0000 0000", + mobile: "0411 0000 0000", + email: "someone.else@example.com", + position: "Manager")], + billing_client_uuid: nil, + first_name: "Someone", + last_name: "Person", + date_of_birth: Date.parse("1987/05/11"), + job_manager_uuid: nil, + tax_number: "11 1111 1111", + company_number: "02 1000 1000", + business_number: "02 1000 2000", + business_structure: "Corporate", + balance_month: "Jan", + prepare_gst: false, + gst_registered: true, + gst_period: Client::GstPeriod::ONE, + gst_basis: Client::GstBasis::INVOICE, + provisional_tax_basis: Client::ProvisionalTaxBasis::STANDARD_OPTION, + provisional_tax_ratio: "1", + signed_tax_authority: true, + tax_agent: "Mr Tax Agent", + agency_status: Client::AgencyStatus::WITH_EOT, + return_type: Client::ReturnType::IR3, + prepare_activity_statement: true, + prepare_tax_return: true) + end + + xit "adds client" do + created_client = + Client.add( + api_key: api_key, + account_key: account_key, + api_url: api_url, + client: client) + + expect(created_client.uuid).to be_present + expect(created_client.name).to client.name + end + end + end +end From 0c98dda9b8f6a89f045638716b13707ad448d33a Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Tue, 28 Apr 2020 10:34:13 +1000 Subject: [PATCH 13/17] Add module --- lib/xpm_ruby/client.rb | 37 +---- lib/xpm_ruby/client/add.rb | 242 +++++++++++++++++++++++++++++++ lib/xpm_ruby/models/client.rb | 201 +------------------------ spec/xpm_ruby/client/add_spec.rb | 80 ++++++++++ spec/xpm_ruby/client_spec.rb | 78 ---------- 5 files changed, 329 insertions(+), 309 deletions(-) create mode 100644 lib/xpm_ruby/client/add.rb create mode 100644 spec/xpm_ruby/client/add_spec.rb delete mode 100644 spec/xpm_ruby/client_spec.rb diff --git a/lib/xpm_ruby/client.rb b/lib/xpm_ruby/client.rb index 47adc67..2d2cb87 100644 --- a/lib/xpm_ruby/client.rb +++ b/lib/xpm_ruby/client.rb @@ -4,6 +4,8 @@ require_relative "models/client" +require_relative "client/add" + module XpmRuby module Client extend self @@ -56,40 +58,5 @@ class Unauthorized < Error; end def build(**args) Models::Client.new(args) end - - def add(api_key:, account_key:, api_url:, client:) - # xml = Ox.dump(client, indent: 2, effort: :strict) - - response = Connection - .new(api_key: api_key, account_key: account_key, api_url: api_url) - .post(endpoint: "client.api/add", body: xml) - - case response.status - when 401 - hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) - - raise Unauthorized.new(hash["html"]["head"]["title"]) - when 200 - hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) - - case hash["Response"]["Status"] - when "OK" - hash["Response"]["Clients"]["Client"].map do |client| - Models::Client.new( - uuid: staff["UUID"], - name: staff["Name"], - email: staff["Email"], - phone: staff["Phone"], - mobile: staff["Mobile"], - address: staff["Address"], - payroll_code: staff["PayrollCode"]) - end - when "ERROR" - raise Error.new(response["ErrorDescription"]) - end - else - raise Error.new(response.status) - end - end end end diff --git a/lib/xpm_ruby/client/add.rb b/lib/xpm_ruby/client/add.rb new file mode 100644 index 0000000..bb71370 --- /dev/null +++ b/lib/xpm_ruby/client/add.rb @@ -0,0 +1,242 @@ +# client.api/add +# +# +# Bloggs Electrical Ltd +# +#
+# +# +# +# +# +# +# +# +# +# +# +# +# +# +# No +# +# +# +# Jo Bloggs +# yes +# +# +# +# +# +# +# +# +# + +# + +# +# +# +# + + +# +# +# +# +# +# +# +# +# +# +# +# + +# +# +# +# +# + +# +# +# +#
+ +module XpmRuby + module Client + module Add + extend self + + def call(api_key:, account_key:, api_url:, client:) + xml = "" # use XML builder + + response = Connection + .new(api_key: api_key, account_key: account_key, api_url: api_url) + .post(endpoint: "client.api/add", body: xml) + + case response.status + when 401 + hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) + + raise Unauthorized.new(hash["html"]["head"]["title"]) + when 200 + hash = Ox.load(response.body, mode: :hash_no_attrs, symbolize_keys: false) + + case hash["Response"]["Status"] + when "OK" + hash["Response"]["Clients"]["Client"].map do |client| + # Models::Client.new( + # uuid: client["UUID"], + # name: client["Name"]) + + Models::Client.new( + uuid: client["UUID"], + name: client["Name"]) + end + when "ERROR" + raise Error.new(response["ErrorDescription"]) + end + else + raise Error.new(response.status) + end + end + + class Client + attr_accessor :name, :email, :address, :city, :region, :post_code, :country, + :postal_address, :postal_city, :postal_region, :postal_post_code, :postal_country, + :phone, :fax, :web_site, :referral_source, :export_code, :is_prospect, + :account_manager_uuid, :contacts, :billing_client_uuid, + :first_name, :last_name, :other_name, :date_of_birth, :job_manager_uuid, + :tax_number, :company_number, :business_number, :business_structure, :balance_month, + :prepare_gst, :gst_registered, :gst_period, :gst_basis, + :provisional_tax_basis, :provisional_tax_ratio, + :signed_tax_authority, :tax_agent, :agency_status, :return_type, + :prepare_activity_statement, :prepare_tax_return + + def initialize(name:, email: nil, + address: nil, city: nil, region: nil, post_code: nil, country: nil, + postal_address: nil, postal_city: nil, postal_region: nil, + postal_post_code: nil, postal_country: nil, + phone: nil, fax: nil, web_site: nil, + referral_source: nil, export_code: nil, is_prospect: nil, + account_manager_uuid: nil, contacts: nil, billing_client_uuid: nil, + first_name: nil, last_name: nil, other_name: nil, date_of_birth: nil, + job_manager_uuid: nil, tax_number: nil, company_number: nil, + business_number: nil, business_structure: nil, balance_month: nil, + prepare_gst: nil, gst_registered: nil, gst_period: nil, gst_basis: nil, + provisional_tax_basis: nil, provisional_tax_ratio: nil, + signed_tax_authority: nil, tax_agent: nil, agency_status: nil, + return_type: nil, prepare_activity_statement: nil, prepare_tax_return: nil) + @name = email + @address = address + @city = city + @region = region + @post_code = post_code + @country = country + @postal_address = postal_address + @postal_city = postal_city + @postal_region = postal_region + @postal_post_code = postal_post_code + @postal_country = postal_country + @phone = phone + @fax = fax + @web_site = web_site + @referral_source = referral_source + @export_code = export_code + @is_prospect = is_prospect + @account_manager_uuid = account_manager_uuid + @contacts = contacts + @billing_client_uuid = billing_client_uuid + @first_name = first_name + @last_name = last_name + @other_name = other_name + @date_of_birth = date_of_birth + @job_manager_uuid = job_manager_uuid + @tax_number = tax_number + @company_number = company_number + @business_number = business_number + @business_structure = business_structure + @balance_month = balance_month + @prepare_gst = prepare_gst + @gst_registered = gst_registered + @gst_period = gst_period + @gst_basis = gst_basis + @provisional_tax_basis = provisional_tax_basis + @provisional_tax_rate = provisional_tax_ratio + @signed_tax_authority = signed_tax_authority + @tax_agent = tax_agent + @agency_status = agency_status + @return_type = return_type + @prepare_activity_statement = prepare_activity_statement + @prepare_tax_return = prepare_tax_return + end + + class AccountManager + attr_reader :uuid, :name + + def initialize(uuid: nil, name: nil) + @uuid = uuid + @name = name + end + + def ==(other) + uuid == other.uuid + end + + def eql?(other) + self == other + end + end + + class Type + attr_reader :name, :cost_markup, :payment_term, :payment_day + + def initialize(name: nil, cost_markup: nil, payment_term: nil, payment_day: nil) + @name = name + @cost_markup = cost_markup + @payment_term = payment_term + @payment_day = payment_day + end + + def ==(other) + name == other.name && + cost_markup == other.cost_markup && + payment_term == other.payment_term && + payment_day = other.payment_day + end + + def eql?(other) + self == other + end + end + + class Contact + def initialize(uuid: nil, is_primary: nil, name: nil, salutation: nil, + addressee: nil, mobile: nil, email: nil, phone: nil, position: nil) + @uuid = uuid + @is_primary = is_primary + @name = name + @salutation = salutation + @addressee = addressee + @mobile = mobile + @email = email + @phone = phone + @position = position + end + + def ==(other) + uuid == other.uuid + end + + def eql?(other) + self == other + end + end + end + end + end +end diff --git a/lib/xpm_ruby/models/client.rb b/lib/xpm_ruby/models/client.rb index b17848f..034d6b4 100644 --- a/lib/xpm_ruby/models/client.rb +++ b/lib/xpm_ruby/models/client.rb @@ -1,71 +1,3 @@ -# client.api/add -# -# -# Bloggs Electrical Ltd -# -#
-# -# -# -# -# -# -# -# -# -# -# -# -# -# -# No -# -# -# -# Jo Bloggs -# yes -# -# -# -# -# -# -# -# -# - -# - -# -# -# -# - - -# -# -# -# -# -# -# -# -# -# -# -# - -# -# -# -# -# - -# -# -# -#
- # client.api/get # @@ -186,135 +118,12 @@ module XpmRuby module Models class Client - attr_accessor :name, :email, :address, :city, :region, :post_code, :country, - :postal_address, :postal_city, :postal_region, :postal_post_code, :postal_country, - :phone, :fax, :web_site, :referral_source, :export_code, :is_prospect, - :account_manager_uuid, :contacts, :billing_client_uuid, - :first_name, :last_name, :other_name, :date_of_birth, :job_manager_uuid, - :tax_number, :company_number, :business_number, :business_structure, :balance_month, - :prepare_gst, :gst_registered, :gst_period, :gst_basis, - :provisional_tax_basis, :provisional_tax_ratio, - :signed_tax_authority, :tax_agent, :agency_status, :return_type, - :prepare_activity_statement, :prepare_tax_return - - def initialize(name:, email: nil, - address: nil, city: nil, region: nil, post_code: nil, country: nil, - postal_address: nil, postal_city: nil, postal_region: nil, - postal_post_code: nil, postal_country: nil, - phone: nil, fax: nil, web_site: nil, - referral_source: nil, export_code: nil, is_prospect: nil, - account_manager_uuid: nil, contacts: nil, billing_client_uuid: nil, - first_name: nil, last_name: nil, other_name: nil, date_of_birth: nil, - job_manager_uuid: nil, tax_number: nil, company_number: nil, - business_number: nil, business_structure: nil, balance_month: nil, - prepare_gst: nil, gst_registered: nil, gst_period: nil, gst_basis: nil, - provisional_tax_basis: nil, provisional_tax_ratio: nil, - signed_tax_authority: nil, tax_agent: nil, agency_status: nil, - return_type: nil, prepare_activity_statement: nil, prepare_tax_return: nil) - @name = email - @address = address - @city = city - @region = region - @post_code = post_code - @country = country - @postal_address = postal_address - @postal_city = postal_city - @postal_region = postal_region - @postal_post_code = postal_post_code - @postal_country = postal_country - @phone = phone - @fax = fax - @web_site = web_site - @referral_source = referral_source - @export_code = export_code - @is_prospect = is_prospect - @account_manager_uuid = account_manager_uuid - @contacts = contacts - @billing_client_uuid = billing_client_uuid - @first_name = first_name - @last_name = last_name - @other_name = other_name - @date_of_birth = date_of_birth - @job_manager_uuid = job_manager_uuid - @tax_number = tax_number - @company_number = company_number - @business_number = business_number - @business_structure = business_structure - @balance_month = balance_month - @prepare_gst = prepare_gst - @gst_registered = gst_registered - @gst_period = gst_period - @gst_basis = gst_basis - @provisional_tax_basis = provisional_tax_basis - @provisional_tax_rate = provisional_tax_ratio - @signed_tax_authority = signed_tax_authority - @tax_agent = tax_agent - @agency_status = agency_status - @return_type = return_type - @prepare_activity_statement = prepare_activity_statement - @prepare_tax_return = prepare_tax_return - end - - class AccountManager - attr_reader :uuid, :name - - def initialize(uuid: nil, name: nil) - @uuid = uuid - @name = name - end - - def ==(other) - uuid == other.uuid - end - - def eql?(other) - self == other - end - end - - class Type - attr_reader :name, :cost_markup, :payment_term, :payment_day - - def initialize(name: nil, cost_markup: nil, payment_term: nil, payment_day: nil) - @name = name - @cost_markup = cost_markup - @payment_term = payment_term - @payment_day = payment_day - end - - def ==(other) - name == other.name && - cost_markup == other.cost_markup && - payment_term == other.payment_term && - payment_day = other.payment_day - end - - def eql?(other) - self == other - end - end - - class Contact - def initialize(uuid: nil, is_primary: nil, name: nil, salutation: nil, - addressee: nil, mobile: nil, email: nil, phone: nil, position: nil) - @uuid = uuid - @is_primary = is_primary - @name = name - @salutation = salutation - @addressee = addressee - @mobile = mobile - @email = email - @phone = phone - @position = position - end - - def ==(other) - uuid == other.uuid - end + attr_accessor :uuid, :name, :email - def eql?(other) - self == other - end + def initialize(uuid:, name:, email: nil) + @uuid = uuid + @name = name + @email = email end end end diff --git a/spec/xpm_ruby/client/add_spec.rb b/spec/xpm_ruby/client/add_spec.rb new file mode 100644 index 0000000..7acc9b4 --- /dev/null +++ b/spec/xpm_ruby/client/add_spec.rb @@ -0,0 +1,80 @@ +require "spec_helper" + +module XpmRuby + module Client + RSpec.describe(Add) do + describe ".call" do + let(:api_key) { "" } + let(:account_key) { "" } + let(:api_url) { "api.workflowmax.com" } + + let(:client) do + Client::Add.build( + name: "Acmer Pty Ltd", + email: "someone@example.com", + address: "1 Address Place", + city: "Sydney", + region: "NSW", + post_code: "2000", + country: "Australia", + postal_address: "Level 32, PWC Building\n188 Quay Street\nAuckland Central", + postal_city: "Auckland", + postal_region: "North Island", + postal_post_code: "1001", + postal_country: "New Zealand", + phone: "00 0000 0000", + fax: "00 1000 0000", + web_site: "http://example.com", + referral_source: "referrer", + export_code: "EXPORT_CODE", + is_prospect: false, + account_manager_uuid: nil, + contacts: [ + Client::Contact.build( + name: "Someone else", + is_primary: true, + salutation: "MR", + addressee: "Addressee", + phone: "02 0000 0000", + mobile: "0411 0000 0000", + email: "someone.else@example.com", + position: "Manager")], + billing_client_uuid: nil, + first_name: "Someone", + last_name: "Person", + date_of_birth: Date.parse("1987/05/11"), + job_manager_uuid: nil, + tax_number: "11 1111 1111", + company_number: "02 1000 1000", + business_number: "02 1000 2000", + business_structure: "Corporate", + balance_month: "Jan", + prepare_gst: false, + gst_registered: true, + gst_period: Client::GstPeriod::ONE, + gst_basis: Client::GstBasis::INVOICE, + provisional_tax_basis: Client::ProvisionalTaxBasis::STANDARD_OPTION, + provisional_tax_ratio: "1", + signed_tax_authority: true, + tax_agent: "Mr Tax Agent", + agency_status: Client::AgencyStatus::WITH_EOT, + return_type: Client::ReturnType::IR3, + prepare_activity_statement: true, + prepare_tax_return: true) + end + + it "adds client" do + client = + Client::Add.call( + api_key: api_key, + account_key: account_key, + api_url: api_url, + client: client) + + expect(created_client.uuid).to be_present + expect(created_client.name).to client.name + end + end + end + end +end diff --git a/spec/xpm_ruby/client_spec.rb b/spec/xpm_ruby/client_spec.rb deleted file mode 100644 index 5562cf8..0000000 --- a/spec/xpm_ruby/client_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -require "spec_helper" - -module XpmRuby - RSpec.describe(Client) do - describe ".add" do - let(:api_key) { "" } - let(:account_key) { "" } - let(:api_url) { "api.workflowmax.com" } - - let(:client) do - Client.build( - name: "Acmer Pty Ltd", - email: "someone@example.com", - address: "1 Address Place", - city: "Sydney", - region: "NSW", - post_code: "2000", - country: "Australia", - postal_address: "Level 32, PWC Building\n188 Quay Street\nAuckland Central", - postal_city: "Auckland", - postal_region: "North Island", - postal_post_code: "1001", - postal_country: "New Zealand", - phone: "00 0000 0000", - fax: "00 1000 0000", - web_site: "http://example.com", - referral_source: "referrer", - export_code: "EXPORT_CODE", - is_prospect: false, - account_manager_uuid: nil, - contacts: [ - Client::Contact.build( - name: "Someone else", - is_primary: true, - salutation: "MR", - addressee: "Addressee", - phone: "02 0000 0000", - mobile: "0411 0000 0000", - email: "someone.else@example.com", - position: "Manager")], - billing_client_uuid: nil, - first_name: "Someone", - last_name: "Person", - date_of_birth: Date.parse("1987/05/11"), - job_manager_uuid: nil, - tax_number: "11 1111 1111", - company_number: "02 1000 1000", - business_number: "02 1000 2000", - business_structure: "Corporate", - balance_month: "Jan", - prepare_gst: false, - gst_registered: true, - gst_period: Client::GstPeriod::ONE, - gst_basis: Client::GstBasis::INVOICE, - provisional_tax_basis: Client::ProvisionalTaxBasis::STANDARD_OPTION, - provisional_tax_ratio: "1", - signed_tax_authority: true, - tax_agent: "Mr Tax Agent", - agency_status: Client::AgencyStatus::WITH_EOT, - return_type: Client::ReturnType::IR3, - prepare_activity_statement: true, - prepare_tax_return: true) - end - - xit "adds client" do - created_client = - Client.add( - api_key: api_key, - account_key: account_key, - api_url: api_url, - client: client) - - expect(created_client.uuid).to be_present - expect(created_client.name).to client.name - end - end - end -end From 4598ec31b1f855a5ec5453fcdc16f46fe2b0a80d Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Tue, 28 Apr 2020 11:42:59 +1000 Subject: [PATCH 14/17] add Nokogiri::Xml::Builder --- Gemfile.lock | 4 ++++ lib/xpm_ruby/client.rb | 15 +------------ lib/xpm_ruby/client/add.rb | 21 ++++++++++++------- .../{models/client.rb => client/get.rb} | 18 +++++++++------- spec/xpm_ruby/client/add_spec.rb | 15 +++++++------ xpm_ruby.gemspec | 2 ++ 6 files changed, 38 insertions(+), 37 deletions(-) rename lib/xpm_ruby/{models/client.rb => client/get.rb} (93%) diff --git a/Gemfile.lock b/Gemfile.lock index 1e675d5..6c6f6af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ PATH specs: xpm_ruby (0.1.0) faraday (~> 1) + nokogiri ox (~> 2.13) GEM @@ -16,7 +17,10 @@ GEM multipart-post (>= 1.2, < 3) jaro_winkler (1.5.4) method_source (1.0.0) + mini_portile2 (2.4.0) multipart-post (2.1.1) + nokogiri (1.10.9) + mini_portile2 (~> 2.4.0) ox (2.13.2) parallel (1.19.1) parser (2.7.1.1) diff --git a/lib/xpm_ruby/client.rb b/lib/xpm_ruby/client.rb index 2d2cb87..9e31bbc 100644 --- a/lib/xpm_ruby/client.rb +++ b/lib/xpm_ruby/client.rb @@ -2,8 +2,7 @@ require "base64" require "ox" -require_relative "models/client" - +require_relative "client/get" require_relative "client/add" module XpmRuby @@ -44,19 +43,7 @@ module ReturnType PTS = "PTS" end - module Contact - extend self - - def build(**args) - Models::Client::Contact.new(args) - end - end - class Error < StandardError; end class Unauthorized < Error; end - - def build(**args) - Models::Client.new(args) - end end end diff --git a/lib/xpm_ruby/client/add.rb b/lib/xpm_ruby/client/add.rb index bb71370..f158700 100644 --- a/lib/xpm_ruby/client/add.rb +++ b/lib/xpm_ruby/client/add.rb @@ -72,11 +72,19 @@ module Add extend self def call(api_key:, account_key:, api_url:, client:) - xml = "" # use XML builder + require "nokogiri" + + builder = ::Nokogiri::XML::Builder.new do |xml| + xml.client { + xml.name { + xml.text(client.name) + } + } + end response = Connection .new(api_key: api_key, account_key: account_key, api_url: api_url) - .post(endpoint: "client.api/add", body: xml) + .post(endpoint: "client.api/add", data: builder.doc.root.to_xml) case response.status when 401 @@ -89,13 +97,10 @@ def call(api_key:, account_key:, api_url:, client:) case hash["Response"]["Status"] when "OK" hash["Response"]["Clients"]["Client"].map do |client| - # Models::Client.new( - # uuid: client["UUID"], - # name: client["Name"]) - - Models::Client.new( + Get::Client.new( uuid: client["UUID"], - name: client["Name"]) + name: client["Name"], + email: client["Email"]) end when "ERROR" raise Error.new(response["ErrorDescription"]) diff --git a/lib/xpm_ruby/models/client.rb b/lib/xpm_ruby/client/get.rb similarity index 93% rename from lib/xpm_ruby/models/client.rb rename to lib/xpm_ruby/client/get.rb index 034d6b4..18ca2c0 100644 --- a/lib/xpm_ruby/models/client.rb +++ b/lib/xpm_ruby/client/get.rb @@ -116,14 +116,18 @@ # module XpmRuby - module Models - class Client - attr_accessor :uuid, :name, :email + module Client + module Get + extend self - def initialize(uuid:, name:, email: nil) - @uuid = uuid - @name = name - @email = email + class Client + attr_accessor :uuid, :name, :email + + def initialize(uuid:, name:, email: nil) + @uuid = uuid + @name = name + @email = email + end end end end diff --git a/spec/xpm_ruby/client/add_spec.rb b/spec/xpm_ruby/client/add_spec.rb index 7acc9b4..3d6d328 100644 --- a/spec/xpm_ruby/client/add_spec.rb +++ b/spec/xpm_ruby/client/add_spec.rb @@ -9,7 +9,7 @@ module Client let(:api_url) { "api.workflowmax.com" } let(:client) do - Client::Add.build( + Client::Add::Client.new( name: "Acmer Pty Ltd", email: "someone@example.com", address: "1 Address Place", @@ -30,7 +30,7 @@ module Client is_prospect: false, account_manager_uuid: nil, contacts: [ - Client::Contact.build( + Client::Add::Client::Contact.new( name: "Someone else", is_primary: true, salutation: "MR", @@ -64,12 +64,11 @@ module Client end it "adds client" do - client = - Client::Add.call( - api_key: api_key, - account_key: account_key, - api_url: api_url, - client: client) + created_client = Client::Add.call( + api_key: api_key, + account_key: account_key, + api_url: api_url, + client: client) expect(created_client.uuid).to be_present expect(created_client.name).to client.name diff --git a/xpm_ruby.gemspec b/xpm_ruby.gemspec index a8ea1c9..0ccf0e9 100644 --- a/xpm_ruby.gemspec +++ b/xpm_ruby.gemspec @@ -35,6 +35,8 @@ Gem::Specification.new do |spec| spec.add_development_dependency("byebug", "~> 11") spec.add_development_dependency("pry-byebug", "~> 3") + spec.add_runtime_dependency("nokogiri") + spec.add_runtime_dependency("faraday", "~> 1") spec.add_runtime_dependency("ox", "~> 2.13") From 01a2f74abc52aedcc141b29ce5b6d52fbcefa861 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Tue, 28 Apr 2020 11:45:07 +1000 Subject: [PATCH 15/17] fix require --- lib/xpm_ruby.rb | 2 ++ lib/xpm_ruby/client/add.rb | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/xpm_ruby.rb b/lib/xpm_ruby.rb index 8c09e88..32fad9d 100644 --- a/lib/xpm_ruby.rb +++ b/lib/xpm_ruby.rb @@ -3,6 +3,8 @@ class Error < StandardError; end class Unauthorized < Error; end end +require "nokogiri" + require "xpm_ruby/version" require "xpm_ruby/connection" require "xpm_ruby/staff" diff --git a/lib/xpm_ruby/client/add.rb b/lib/xpm_ruby/client/add.rb index f158700..d559187 100644 --- a/lib/xpm_ruby/client/add.rb +++ b/lib/xpm_ruby/client/add.rb @@ -72,8 +72,6 @@ module Add extend self def call(api_key:, account_key:, api_url:, client:) - require "nokogiri" - builder = ::Nokogiri::XML::Builder.new do |xml| xml.client { xml.name { From 8a3b8c48511f08b8ab2923564fe6b7dc223cb677 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Tue, 28 Apr 2020 12:00:28 +1000 Subject: [PATCH 16/17] pass spec --- lib/xpm_ruby/client/add.rb | 17 ++++---- spec/vcr_cassettes/xpm_ruby/client/add.yml | 51 ++++++++++++++++++++++ spec/xpm_ruby/client/add_spec.rb | 10 ++++- 3 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 spec/vcr_cassettes/xpm_ruby/client/add.yml diff --git a/lib/xpm_ruby/client/add.rb b/lib/xpm_ruby/client/add.rb index d559187..2943f6b 100644 --- a/lib/xpm_ruby/client/add.rb +++ b/lib/xpm_ruby/client/add.rb @@ -73,8 +73,8 @@ module Add def call(api_key:, account_key:, api_url:, client:) builder = ::Nokogiri::XML::Builder.new do |xml| - xml.client { - xml.name { + xml.Client { + xml.Name { xml.text(client.name) } } @@ -94,12 +94,10 @@ def call(api_key:, account_key:, api_url:, client:) case hash["Response"]["Status"] when "OK" - hash["Response"]["Clients"]["Client"].map do |client| - Get::Client.new( - uuid: client["UUID"], - name: client["Name"], - email: client["Email"]) - end + Get::Client.new( + uuid: hash["Response"]["Client"]["UUID"], + name: hash["Response"]["Client"]["Name"], + email: hash["Response"]["Client"]["Email"]) when "ERROR" raise Error.new(response["ErrorDescription"]) end @@ -134,7 +132,8 @@ def initialize(name:, email: nil, provisional_tax_basis: nil, provisional_tax_ratio: nil, signed_tax_authority: nil, tax_agent: nil, agency_status: nil, return_type: nil, prepare_activity_statement: nil, prepare_tax_return: nil) - @name = email + @name = name + @email = email @address = address @city = city @region = region diff --git a/spec/vcr_cassettes/xpm_ruby/client/add.yml b/spec/vcr_cassettes/xpm_ruby/client/add.yml new file mode 100644 index 0000000..94cbdbe --- /dev/null +++ b/spec/vcr_cassettes/xpm_ruby/client/add.yml @@ -0,0 +1,51 @@ +--- +http_interactions: +- request: + method: post + uri: https://api.workflowmax.com/v3/client.api/add + body: + encoding: UTF-8 + string: |- + + Acmer Pty Ltd + + headers: + User-Agent: + - Faraday v1.0.1 + Authorization: + - Basic + Content-Type: + - application/x-www-form-urlencoded + response: + status: + code: 200 + message: OK + headers: + access-control-allow-origin: + - "*" + cache-control: + - private + content-type: + - text/xml; charset=utf-8 + date: + - Tue, 28 Apr 2020 01:52:47 GMT + server: + - Microsoft-IIS/10.0 + vary: + - Accept-Encoding + x-aspnet-version: + - 4.0.30319 + x-aspnetmvc-version: + - '4.0' + content-length: + - '449' + connection: + - keep-alive + body: + encoding: ASCII-8BIT + string: OK9f868ba7-437c-4574-9a17-8c6f4890545bAcmer + Pty Ltd
NoNoNo
+ http_version: null + recorded_at: Tue, 28 Apr 2020 01:52:48 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/xpm_ruby/client/add_spec.rb b/spec/xpm_ruby/client/add_spec.rb index 3d6d328..903d579 100644 --- a/spec/xpm_ruby/client/add_spec.rb +++ b/spec/xpm_ruby/client/add_spec.rb @@ -3,6 +3,12 @@ module XpmRuby module Client RSpec.describe(Add) do + around(:each) do |example| + VCR.use_cassette("xpm_ruby/client/add") do + example.run + end + end + describe ".call" do let(:api_key) { "" } let(:account_key) { "" } @@ -70,8 +76,8 @@ module Client api_url: api_url, client: client) - expect(created_client.uuid).to be_present - expect(created_client.name).to client.name + expect(created_client.uuid).not_to be_nil + expect(created_client.name).to eql(client.name) end end end From ef18b39c57b4ec33d62ed0f9d2324502b28a4ab4 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Tue, 28 Apr 2020 12:22:36 +1000 Subject: [PATCH 17/17] fix cops --- .rubocop.yml | 3 +++ lib/xpm_ruby/client/add.rb | 13 +++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index adfe750..9059597 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -452,3 +452,6 @@ Style/SymbolProc: Style/ZeroLengthPredicate: Enabled: false + +Style/MutableConstant: + Enabled: false diff --git a/lib/xpm_ruby/client/add.rb b/lib/xpm_ruby/client/add.rb index 2943f6b..16c9cfc 100644 --- a/lib/xpm_ruby/client/add.rb +++ b/lib/xpm_ruby/client/add.rb @@ -41,7 +41,6 @@ # # - # # # @@ -73,11 +72,11 @@ module Add def call(api_key:, account_key:, api_url:, client:) builder = ::Nokogiri::XML::Builder.new do |xml| - xml.Client { - xml.Name { + xml.Client do + xml.Name do xml.text(client.name) - } - } + end + end end response = Connection @@ -118,6 +117,7 @@ class Client :signed_tax_authority, :tax_agent, :agency_status, :return_type, :prepare_activity_statement, :prepare_tax_return + # rubocop:disable Metrics/AbcSize def initialize(name:, email: nil, address: nil, city: nil, region: nil, post_code: nil, country: nil, postal_address: nil, postal_city: nil, postal_region: nil, @@ -176,6 +176,7 @@ def initialize(name:, email: nil, @prepare_activity_statement = prepare_activity_statement @prepare_tax_return = prepare_tax_return end + # rubocop:enable Metrics/AbcSize class AccountManager attr_reader :uuid, :name @@ -208,7 +209,7 @@ def ==(other) name == other.name && cost_markup == other.cost_markup && payment_term == other.payment_term && - payment_day = other.payment_day + payment_day == other.payment_day end def eql?(other)