Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PATH
xpm_ruby (0.1.0)
activesupport
builder
dry-types
faraday
ox (~> 2.13)

Expand All @@ -22,6 +23,28 @@ GEM
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 (0.6.1)
concurrent-ruby (~> 1.0)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
dry-types (0.15.0)
concurrent-ruby (~> 1.0)
dry-container (~> 0.3)
dry-core (~> 0.4, >= 0.4.4)
dry-equalizer (~> 0.2, >= 0.2.2)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 0.5, >= 0.5)
faraday (1.0.1)
multipart-post (>= 1.2, < 3)
i18n (1.8.2)
Expand Down
1 change: 1 addition & 0 deletions lib/xpm_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Unauthorized < Error; end
require "xpm_ruby/client"
require "xpm_ruby/connection"
require "xpm_ruby/job"
require "xpm_ruby/schema/job/add"
require "xpm_ruby/staff"
require "xpm_ruby/template"
require "xpm_ruby/version"
10 changes: 10 additions & 0 deletions lib/xpm_ruby/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,15 @@ def current(access_token:, xero_tenant_id:)

response["Jobs"]["Job"]
end

def add(access_token:, xero_tenant_id:, job:)
validated_job = XpmRuby::Schema::Job::Add[job]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder how we could drop all the :: operations? Maybe something like:

# lib/xpm_ruby/job.rb

require_relative "job/add_schema"

module XpmRuby
  module Job
    def add(...)
      validated_job = AddSchema[job]
    end
  end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I will have a think about it. I was following the structure from the client success gem. I think if we keep them in the same relative place in all our gems it might make it easier for our devs to work across gems.

But then again I am not 100% why the client success gem was structured this way.
;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose one argument for a dedicated schema directory is that theoretically a data structure could be independent of the actions? So xpm_ruby/job.rb is a module containing job related functions, but it is not a class per se. Theoretically any module could access any schema (so one of the calls on client, might use the job schema).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge this for now, we can always move it later (pending a discussion).


response = Connection
.new(access_token: access_token, xero_tenant_id: xero_tenant_id)
.post(endpoint: "job.api/add", data: validated_job.to_xml(root: "Job"))

response["Job"]
end
end
end
21 changes: 21 additions & 0 deletions lib/xpm_ruby/schema/job/add.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative "../../types"

module XpmRuby
module Schema
module Job
Add = Types::Hash.schema(
"Name" => Types::String,
"Description" => Types::String,
"ClientID" => Types::Coercible::String,
"ContactID?" => Types::Coercible::String,
"StartDate" => Types::Coercible::String,
"DueDate" => Types::Coercible::String,
"ClientNumber?" => Types::Coercible::String,
"ID?" => Types::Coercible::String,
"TemplateID?" => Types::Coercible::String,
"CategoryID?" => Types::Coercible::String,
"Budget?" => Types::Coercible::String
)
end
end
end
7 changes: 7 additions & 0 deletions lib/xpm_ruby/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "dry-types"

module XpmRuby
module Types
include Dry.Types()
end
end
65 changes: 65 additions & 0 deletions spec/vcr_cassettes/xpm_ruby/job/add.yml

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

22 changes: 22 additions & 0 deletions spec/xpm_ruby/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,27 @@ module XpmRuby
expect(first_job["CompletedDate"]).to be_nil
end
end

describe ".add" do
let(:xero_tenant_id) { "0791dc22-8611-4c1c-8df7-1c5453d0795b" }
let(:access_token) { "eyJhbGciOiJSUzI1NiIsImtpZCI6IjFDQUY4RTY2NzcyRDZEQzAyOEQ2NzI2RkQwMjYxNTgxNTcwRUZDMTkiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJISy1PWm5jdGJjQW8xbkp2MENZVmdWY09fQmsifQ.eyJuYmYiOjE1ODgyMDg3MjksImV4cCI6MTU4ODIxMDUyOSwiaXNzIjoiaHR0cHM6Ly9pZGVudGl0eS54ZXJvLmNvbSIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHkueGVyby5jb20vcmVzb3VyY2VzIiwiY2xpZW50X2lkIjoiNDkyMjZBNjIzMzY0NDVFM0FGQUM5QTQ4MkJGOUUyN0UiLCJzdWIiOiIwY2FmMWU4MWYyZWE1MzdkYWIxYjYzNTY3NTc2ZDk3ZSIsImF1dGhfdGltZSI6MTU4ODIwODU3NSwieGVyb191c2VyaWQiOiJmYzI5MDBjNy0wNjcyLTQzOGItOTNkMS1hOGMyNTBmZDg5MjkiLCJnbG9iYWxfc2Vzc2lvbl9pZCI6IjZmYWRiYTVmYWI3NTQ0NDliNjkwMGE1NzI3OGVlMjc5IiwianRpIjoiZmU2MjUyNzNhNmEwMTI4YjI2N2IxYmI1YWQzMGVkODgiLCJzY29wZSI6WyJlbWFpbCIsInByb2ZpbGUiLCJvcGVuaWQiLCJwcmFjdGljZW1hbmFnZXIiLCJvZmZsaW5lX2FjY2VzcyJdfQ.iL5C3Lcc1KuxTcIMUNUdKfqRoB13flB46dyQ2fUcQesF_lloofR9qWzt1uijc91qQPnkyrgb_Kv2aGvR3YvXXM1naK2Wy87wfAJwonkAGm6IcIIeukgvvJMt3bNZvow-lrk2eb9Wfcz7xt_nJSqL9lr8YV9T5f56IWJzZJzwAiNYz4F1lmqzKMwX5fUU9gYHucgIgQAwDaD3AGPdB-AC71ZNw2Zwsshebv89CbSbpw7rGU9gEhHnI7PjXXYwRn430Zh9k0ruD7IQWNRMh26oS_PR4zIEFDGBn5xBdnm9jLWl9juQUkn0OPQvAwkEuM54AXSWaAgyeasdOtDNjD8JMA" }
let(:job) { { "Name" => "Joe Bloggs", "Description" => "New Job", "ClientID" => "24097642", "StartDate" => "20091023", "DueDate" => "20091023" } }

around(:each) do |example|
VCR.use_cassette("xpm_ruby/job/add") do
example.run
end
end

it "lists adds a job" do
added_job = service.add(access_token: access_token, xero_tenant_id: xero_tenant_id, job: job)

expect(added_job["Name"]).to eql(job["Name"])
expect(added_job["Description"]).to eql(job["Description"])
expect(added_job["Client"]["ID"]).to eql(job["ClientID"])
expect(added_job["StartDate"]).to eql("2009-10-23T00:00:00")
expect(added_job["DueDate"]).to eql("2009-10-23T00:00:00")
end
end
end
end
27 changes: 27 additions & 0 deletions spec/xpm_ruby/schema/job/add_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "spec_helper"

module XpmRuby
module Schema
RSpec.describe(Job) do
context "with a valid Add schema" do
it "should not raise an error" do
hash = { "Name" => "Joe Bloggs", "Description" => "New Job", "ClientID" => 1234, "StartDate" => 20091023, "DueDate" => 20091023 }
expect { Job::Add[hash] }.not_to raise_error
end

it "should coerce the types" do
hash = { "Name" => "Joe Bloggs", "Description" => "New Job", "ClientID" => 1234, "StartDate" => 20091023, "DueDate" => 20091023 }
add = Job::Add[hash]
expect(add["ClientID"]).to eql("1234")
end
end

context "with an invalid Add schema" do
it "should raise an error" do
hash = { "Name" => "Joe Bloggs" }
expect { Job::Add[hash] }.to raise_error(Dry::Types::ConstraintError, '{"Name"=>"Joe Bloggs"} violates constraints (:Description is missing in Hash input failed)')
end
end
end
end
end
2 changes: 2 additions & 0 deletions xpm_ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency("activesupport")
spec.add_runtime_dependency("builder")

spec.add_runtime_dependency("dry-types")
end