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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/bambooing/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,19 @@ def handler(response)
client_error_handler(response)
when Net::HTTPServerError
server_error_handler(response)
else
unknown_handler(response)
end
end

def success_handler(response)
return EMTPY_BODY if verb == :post

payload = JSON.parse(response.body, symbolize_names: true)

unknown_handler(response) unless payload[:success]
payload = JSON.parse(response.body, symbolize_names: true)

if payload.key?(:success)
unknown_handler(response) unless payload[:success]
end

payload
end
Expand Down
22 changes: 22 additions & 0 deletions lib/bambooing/time_off/employee.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Bambooing
module TimeOff
class Employee

PATH = '/time_off/employee'.freeze

class << self

def pto_types(employee_id)
payload = Client.get(path: PATH, params: { employeeId: employee_id }, headers: {})

pto_types = payload[:policies]
pto_types.map { |policy| policy[:categoryId] }

rescue Client::Redirection, Client::ClientError, Client::ServerError, Client::UnknownResponse
[]
end
end

end
end
end
21 changes: 17 additions & 4 deletions lib/bambooing/time_off/table/pto.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
require 'bambooing/time_off/employee'

module Bambooing
module TimeOff
module Table
class PTO < Request
TYPE = 77.freeze
APPROVED = 'approved'.freeze

PATH = '/time_off/employee'.freeze
APPROVED = 'approved'.freeze

class << self

def approved(employee_id:, year:)
ptos = where(employee_id: employee_id, type_id: TYPE, year: year)
ptos.select { |pto| pto.status == APPROVED }
pto_types = Employee.pto_types(employee_id)
ptos = []

pto_types.each { |pto_type|

(ptos << where(employee_id: employee_id, type_id: pto_type, year: year)).flatten!
}

ptos.select { |pto|
pto.status == APPROVED
}
end
end
end
Expand Down
77 changes: 77 additions & 0 deletions spec/bambooing/time_off/employee_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require 'spec_helper'
require 'support/configuration_shared_context'

RSpec.describe Bambooing::TimeOff::Employee do
include_context 'configuration'

let(:employee_id) { 1 }
let(:pto_types) { [77,81] }
let(:path) { "/time_off/employee" }

describe '.where' do
let(:params) do
{
employeeId: 1
}
end
let(:payload) do
{
policies: [
{ categoryId: 77 }, { categoryId: 81 }
]
}
end

it 'returns a list of PTO types by employee_id' do
allow(Bambooing::Client).to receive(:get).with(path: path, params: params, headers: {}).and_return(payload)

result = described_class.pto_types(employee_id)

expect(result.size).to eq(2)

expect(result).to all(be_kind_of(Numeric))
end

context 'when an error is raised' do
context 'since a response redirection is received' do
it 'returns empty list' do
allow(Bambooing::Client).to receive(:get).with(path: path, params: params, headers: {}).and_raise(Bambooing::Client::Redirection)

result = described_class.pto_types(employee_id)

expect(result).to eq([])
end
end

context 'since there is a client error' do
it 'returns empty list' do
allow(Bambooing::Client).to receive(:get).with(path: path, params: params, headers: {}).and_raise(Bambooing::Client::ClientError)

result = described_class.pto_types(employee_id)

expect(result).to eq([])
end
end

context 'since there is a server error' do
it 'returns empty list' do
allow(Bambooing::Client).to receive(:get).with(path: path, params: params, headers: {}).and_raise(Bambooing::Client::ServerError)

result = described_class.pto_types(employee_id)

expect(result).to eq([])
end
end

context 'since an unknown response is received' do
it 'returns empty list' do
allow(Bambooing::Client).to receive(:get).with(path: path, params: params, headers: {}).and_raise(Bambooing::Client::UnknownResponse)

result = described_class.pto_types(employee_id)

expect(result).to eq([])
end
end
end
end
end
5 changes: 3 additions & 2 deletions spec/bambooing/time_off/table/pto_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

RSpec.describe Bambooing::TimeOff::Table::PTO do
include_context 'configuration'
let(:employee_class) { Bambooing::TimeOff::Employee }

describe '.approved' do
let(:request_class) do
Expand All @@ -16,10 +17,10 @@
end

it 'every request status is approved' do
allow(described_class).to receive(:where).with(employee_id: 1, type_id: 77, year: 2019).and_return(requests)
allow(described_class).to receive(:where).with(employee_id: 1, type_id: anything, year: 2019).and_return(requests)
allow(employee_class).to receive(:pto_types).with(1).and_return([77,81])
result = described_class.approved(employee_id: 1, year: 2019)

expect(result.size).to eq(1)
expect(result).to all(have_attributes(status: 'approved'))
end
end
Expand Down