From 4e46189a1fd79c1e90fe3b62a6a9f8c89eae0646 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Fri, 24 Apr 2020 15:25:27 +1000 Subject: [PATCH 01/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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 05de0bcc6e5d8e1fab952c7cd1d477f73520be97 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 17:27:01 +1000 Subject: [PATCH 08/11] add dry typesa --- Gemfile.lock | 31 +++++++++++++++++++++++++++++++ lib/xpm_ruby/models/staff.rb | 26 -------------------------- lib/xpm_ruby/staff.rb | 9 ++++++--- lib/xpm_ruby/types/staff.rb | 18 ++++++++++++++++++ xpm_ruby.gemspec | 2 ++ 5 files changed, 57 insertions(+), 29 deletions(-) delete mode 100644 lib/xpm_ruby/models/staff.rb create mode 100644 lib/xpm_ruby/types/staff.rb diff --git a/Gemfile.lock b/Gemfile.lock index bdeb076..5b766bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,8 @@ PATH remote: . specs: xpm_ruby (0.1.0) + dry-struct (~> 1.3) + dry-types (~> 1.4) faraday (~> 1) ox (~> 2.13) @@ -11,9 +13,38 @@ GEM ast (2.4.0) byebug (11.1.3) coderay (1.1.2) + concurrent-ruby (1.1.6) diff-lcs (1.3) + dry-configurable (0.11.5) + concurrent-ruby (~> 1.0) + dry-core (~> 0.4, >= 0.4.7) + dry-equalizer (~> 0.2) + dry-container (0.7.2) + concurrent-ruby (~> 1.0) + dry-configurable (~> 0.1, >= 0.1.3) + dry-core (0.4.9) + concurrent-ruby (~> 1.0) + dry-equalizer (0.3.0) + dry-inflector (0.2.0) + dry-logic (1.0.6) + concurrent-ruby (~> 1.0) + dry-core (~> 0.2) + dry-equalizer (~> 0.2) + dry-struct (1.3.0) + dry-core (~> 0.4, >= 0.4.4) + dry-equalizer (~> 0.3) + dry-types (~> 1.3) + ice_nine (~> 0.11) + dry-types (1.4.0) + concurrent-ruby (~> 1.0) + dry-container (~> 0.3) + dry-core (~> 0.4, >= 0.4.4) + dry-equalizer (~> 0.3) + dry-inflector (~> 0.1, >= 0.1.2) + dry-logic (~> 1.0, >= 1.0.2) faraday (1.0.1) multipart-post (>= 1.2, < 3) + ice_nine (0.11.2) jaro_winkler (1.5.4) method_source (1.0.0) multipart-post (2.1.1) diff --git a/lib/xpm_ruby/models/staff.rb b/lib/xpm_ruby/models/staff.rb deleted file mode 100644 index 32a3bf9..0000000 --- a/lib/xpm_ruby/models/staff.rb +++ /dev/null @@ -1,26 +0,0 @@ -module XpmRuby - 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, - 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/lib/xpm_ruby/staff.rb b/lib/xpm_ruby/staff.rb index 6641347..5dabbb2 100644 --- a/lib/xpm_ruby/staff.rb +++ b/lib/xpm_ruby/staff.rb @@ -2,17 +2,20 @@ require "base64" require "ox" -require_relative "models/staff" +require_relative "types/staff" module XpmRuby module Staff extend self class Error < StandardError; end + class TypeError < Error; end class Unauthorized < Error; end def build(**args) - Models::Staff.new(args) + Types::Staff.new(args) + rescue Dry::Struct::Error => error + raise TypeError.new(error.message) end def list(api_key:, account_key:) @@ -35,7 +38,7 @@ def list(api_key:, account_key:) case hash["Response"]["Status"] when "OK" hash["Response"]["StaffList"]["Staff"].map do |staff| - Models::Staff.new( + build( uuid: staff["UUID"], name: staff["Name"], email: staff["Email"], diff --git a/lib/xpm_ruby/types/staff.rb b/lib/xpm_ruby/types/staff.rb new file mode 100644 index 0000000..66051d2 --- /dev/null +++ b/lib/xpm_ruby/types/staff.rb @@ -0,0 +1,18 @@ +require "dry-types" +require "dry-struct" + +module XpmRuby + module Types + include Dry.Types + + class Staff < Dry::Struct + attribute :uuid, Types::Strict::String + attribute :name, Types::Strict::String + attribute :email, Types::Strict::String + attribute :phone, Types::Strict::String.optional.default(nil) + attribute :mobile, Types::Strict::String.optional.default(nil) + attribute :address, Types::Strict::String.optional.default(nil) + attribute :payroll_code, Types::Strict::String.optional.default(nil) + end + end +end diff --git a/xpm_ruby.gemspec b/xpm_ruby.gemspec index 526c780..edef141 100644 --- a/xpm_ruby.gemspec +++ b/xpm_ruby.gemspec @@ -27,6 +27,8 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency("faraday", "~> 1") spec.add_runtime_dependency("ox", "~> 2.13") + spec.add_runtime_dependency("dry-types", "~> 1.4") + spec.add_runtime_dependency("dry-struct", "~> 1.3") spec.add_development_dependency("bundler", "~> 2.0") spec.add_development_dependency("rake", ">= 12.3.3") From 9ec09871adb13d6553ddec5981d3891f2246e4c5 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 19:35:33 +1000 Subject: [PATCH 09/11] Revert "add dry typesa" This reverts commit 05de0bcc6e5d8e1fab952c7cd1d477f73520be97. --- Gemfile.lock | 31 ------------------------------- lib/xpm_ruby/models/staff.rb | 26 ++++++++++++++++++++++++++ lib/xpm_ruby/staff.rb | 9 +++------ lib/xpm_ruby/types/staff.rb | 18 ------------------ xpm_ruby.gemspec | 2 -- 5 files changed, 29 insertions(+), 57 deletions(-) create mode 100644 lib/xpm_ruby/models/staff.rb delete mode 100644 lib/xpm_ruby/types/staff.rb diff --git a/Gemfile.lock b/Gemfile.lock index 5b766bc..bdeb076 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,8 +2,6 @@ PATH remote: . specs: xpm_ruby (0.1.0) - dry-struct (~> 1.3) - dry-types (~> 1.4) faraday (~> 1) ox (~> 2.13) @@ -13,38 +11,9 @@ GEM ast (2.4.0) byebug (11.1.3) coderay (1.1.2) - concurrent-ruby (1.1.6) diff-lcs (1.3) - dry-configurable (0.11.5) - concurrent-ruby (~> 1.0) - dry-core (~> 0.4, >= 0.4.7) - dry-equalizer (~> 0.2) - dry-container (0.7.2) - concurrent-ruby (~> 1.0) - dry-configurable (~> 0.1, >= 0.1.3) - dry-core (0.4.9) - concurrent-ruby (~> 1.0) - dry-equalizer (0.3.0) - dry-inflector (0.2.0) - dry-logic (1.0.6) - concurrent-ruby (~> 1.0) - dry-core (~> 0.2) - dry-equalizer (~> 0.2) - dry-struct (1.3.0) - dry-core (~> 0.4, >= 0.4.4) - dry-equalizer (~> 0.3) - dry-types (~> 1.3) - ice_nine (~> 0.11) - dry-types (1.4.0) - concurrent-ruby (~> 1.0) - dry-container (~> 0.3) - dry-core (~> 0.4, >= 0.4.4) - dry-equalizer (~> 0.3) - dry-inflector (~> 0.1, >= 0.1.2) - dry-logic (~> 1.0, >= 1.0.2) faraday (1.0.1) multipart-post (>= 1.2, < 3) - ice_nine (0.11.2) jaro_winkler (1.5.4) method_source (1.0.0) multipart-post (2.1.1) diff --git a/lib/xpm_ruby/models/staff.rb b/lib/xpm_ruby/models/staff.rb new file mode 100644 index 0000000..32a3bf9 --- /dev/null +++ b/lib/xpm_ruby/models/staff.rb @@ -0,0 +1,26 @@ +module XpmRuby + 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, + 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/lib/xpm_ruby/staff.rb b/lib/xpm_ruby/staff.rb index 5dabbb2..6641347 100644 --- a/lib/xpm_ruby/staff.rb +++ b/lib/xpm_ruby/staff.rb @@ -2,20 +2,17 @@ require "base64" require "ox" -require_relative "types/staff" +require_relative "models/staff" module XpmRuby module Staff extend self class Error < StandardError; end - class TypeError < Error; end class Unauthorized < Error; end def build(**args) - Types::Staff.new(args) - rescue Dry::Struct::Error => error - raise TypeError.new(error.message) + Models::Staff.new(args) end def list(api_key:, account_key:) @@ -38,7 +35,7 @@ def list(api_key:, account_key:) case hash["Response"]["Status"] when "OK" hash["Response"]["StaffList"]["Staff"].map do |staff| - build( + Models::Staff.new( uuid: staff["UUID"], name: staff["Name"], email: staff["Email"], diff --git a/lib/xpm_ruby/types/staff.rb b/lib/xpm_ruby/types/staff.rb deleted file mode 100644 index 66051d2..0000000 --- a/lib/xpm_ruby/types/staff.rb +++ /dev/null @@ -1,18 +0,0 @@ -require "dry-types" -require "dry-struct" - -module XpmRuby - module Types - include Dry.Types - - class Staff < Dry::Struct - attribute :uuid, Types::Strict::String - attribute :name, Types::Strict::String - attribute :email, Types::Strict::String - attribute :phone, Types::Strict::String.optional.default(nil) - attribute :mobile, Types::Strict::String.optional.default(nil) - attribute :address, Types::Strict::String.optional.default(nil) - attribute :payroll_code, Types::Strict::String.optional.default(nil) - end - end -end diff --git a/xpm_ruby.gemspec b/xpm_ruby.gemspec index edef141..526c780 100644 --- a/xpm_ruby.gemspec +++ b/xpm_ruby.gemspec @@ -27,8 +27,6 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency("faraday", "~> 1") spec.add_runtime_dependency("ox", "~> 2.13") - spec.add_runtime_dependency("dry-types", "~> 1.4") - spec.add_runtime_dependency("dry-struct", "~> 1.3") spec.add_development_dependency("bundler", "~> 2.0") spec.add_development_dependency("rake", ">= 12.3.3") From e5f222506b446facaa8d8c9f9f2502ba3919a7b1 Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 20:47:12 +1000 Subject: [PATCH 10/11] superclass errors + add api_url --- lib/xpm_ruby.rb | 8 +++++--- lib/xpm_ruby/staff.rb | 7 +++---- spec/staff_spec.rb | 8 +++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/xpm_ruby.rb b/lib/xpm_ruby.rb index a4307e4..342f299 100644 --- a/lib/xpm_ruby.rb +++ b/lib/xpm_ruby.rb @@ -1,5 +1,7 @@ -require "xpm_ruby/version" -require "xpm_ruby/staff" - module XpmRuby + class Error < StandardError; end + class Unauthorized < Error; end end + +require "xpm_ruby/version" +require "xpm_ruby/staff" diff --git a/lib/xpm_ruby/staff.rb b/lib/xpm_ruby/staff.rb index 6641347..224efd1 100644 --- a/lib/xpm_ruby/staff.rb +++ b/lib/xpm_ruby/staff.rb @@ -8,19 +8,18 @@ module XpmRuby module Staff extend self - class Error < StandardError; end - class Unauthorized < Error; end + class Error < Error; end def build(**args) Models::Staff.new(args) end - def list(api_key:, account_key:) + def list(api_key:, account_key:, api_url:) key = Base64.strict_encode64("#{api_key}:#{account_key}") response = Faraday .new( - url: "https://api.workflowmax.com/v3", + url: "https://#{api_url}/v3", headers: { "Authorization" => "Basic #{key}" }) .get("staff.api/list") diff --git a/spec/staff_spec.rb b/spec/staff_spec.rb index c304e9b..81a702b 100644 --- a/spec/staff_spec.rb +++ b/spec/staff_spec.rb @@ -6,21 +6,23 @@ module XpmRuby context "when keys invalid" do let(:api_key) { "" } let(:account_key) { "" } + let(:api_url) { "api.workflowmax.com" } xit "raises unauthorized error" do expect do - Staff.list(api_key: api_key, account_key: account_key) - end.to raise_error(Staff::Unauthorized) + Staff.list(api_key: api_key, account_key: account_key, api_url: api_url) + end.to raise_error(Unauthorized) end end context "when keys valid" do let(:api_key) { "" } let(:account_key) { "" } + let(:api_url) { "api.workflowmax.com" } 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, api_url: api_url) ).to include( Staff.build( name: "Dev Testing", From 0273143b7b0e5f1ae0bc8442230a67f51d12f23c Mon Sep 17 00:00:00 2001 From: Adam Mikulasev Date: Sat, 25 Apr 2020 20:53:29 +1000 Subject: [PATCH 11/11] update to use Connection --- lib/xpm_ruby/staff.rb | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/xpm_ruby/staff.rb b/lib/xpm_ruby/staff.rb index 224efd1..0084f6f 100644 --- a/lib/xpm_ruby/staff.rb +++ b/lib/xpm_ruby/staff.rb @@ -1,5 +1,3 @@ -require "faraday" -require "base64" require "ox" require_relative "models/staff" @@ -15,13 +13,9 @@ def build(**args) end def list(api_key:, account_key:, api_url:) - key = Base64.strict_encode64("#{api_key}:#{account_key}") - - response = Faraday - .new( - url: "https://#{api_url}/v3", - headers: { "Authorization" => "Basic #{key}" }) - .get("staff.api/list") + response = Connection + .new(api_key: api_key, account_key: account_key, api_url: api_url) + .get(endpoint: "staff.api/list") case response.status when 401