Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow API domain to be specified #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/bamboozled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
module Bamboozled
class << self
# Creates a standard client that will raise all errors it encounters
def client(subdomain: nil, api_key: nil, httparty_options: {})
Bamboozled::Base.new(subdomain: subdomain, api_key: api_key, httparty_options: httparty_options)
def client(subdomain: nil, api_domain: nil, api_key: nil, httparty_options: {})
Bamboozled::Base.new(subdomain: subdomain, api_domain: api_domain, api_key: api_key,
httparty_options: httparty_options)
end
end
end
12 changes: 9 additions & 3 deletions lib/bamboozled/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ module API
class Base
attr_reader :subdomain, :api_key

def initialize(subdomain, api_key, httparty_options = {})
@subdomain = subdomain
def initialize(domain_options, api_key, httparty_options = {})
if domain_options.is_a?(Hash)
@subdomain = domain_options[:subdomain]
@api_domain = domain_options[:api_domain]
else
@subdomain = domain_options
end
@api_key = api_key
@httparty_options = httparty_options || {}
end
Expand Down Expand Up @@ -78,7 +83,8 @@ def auth
end

def path_prefix
"https://api.bamboohr.com/api/gateway.php/#{subdomain}/v1/"
api_domain = @api_domain || 'api.bamboohr.com'
"https://#{api_domain}/api/gateway.php/#{subdomain}/v1/"
end
end
end
Expand Down
22 changes: 16 additions & 6 deletions lib/bamboozled/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,40 @@ module Bamboozled
class Base
attr_reader :request

def initialize(subdomain: nil, api_key: nil, httparty_options: {})
def initialize(subdomain: nil, api_domain: nil, api_key: nil, httparty_options: {})
@subdomain, @api_key = subdomain
@api_domain = api_domain
@api_key = api_key
@httparty_options = httparty_options
end

def employee
@employee ||= Bamboozled::API::Employee.new(@subdomain, @api_key, @httparty_options)
@employee ||= Bamboozled::API::Employee.new(domain_options, @api_key, @httparty_options)
end

def report
@report ||= Bamboozled::API::Report.new(@subdomain, @api_key, @httparty_options)
@report ||= Bamboozled::API::Report.new(domain_options, @api_key, @httparty_options)
end

def meta
@meta ||= Bamboozled::API::Meta.new(@subdomain, @api_key, @httparty_options)
@meta ||= Bamboozled::API::Meta.new(domain_options, @api_key, @httparty_options)
end

def time_off
@time_off ||= Bamboozled::API::TimeOff.new(@subdomain, @api_key, @httparty_options)
@time_off ||= Bamboozled::API::TimeOff.new(domain_options, @api_key, @httparty_options)
end

def time_tracking
@time_tracking ||= Bamboozled::API::TimeTracking.new(@subdomain, @api_key, @httparty_options)
@time_tracking ||= Bamboozled::API::TimeTracking.new(domain_options, @api_key, @httparty_options)
end

protected

def domain_options
{
subdomain: @subdomain,
api_domain: @api_domain
}
end
end
end
17 changes: 17 additions & 0 deletions spec/lib/bamboozled/api/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
require "spec_helper"

RSpec.describe "Bamboozled::API::Base" do
it "accepts a hash as first constructor parameter" do
expect do
Bamboozled::API::Base.new({ subdomain: 'x', api_domain: 'api.bamboohr.co.uk' }, "x", { log_format: :curl })
end.not_to raise_error
end

it "uses subdomain and api_domain from first parameter if has provided" do
response = double("response", code: 200, body: "{}", to_str: "{}")

expect(HTTParty).to receive(:get).
with("https://api.bamboohr.co.uk/api/gateway.php/x/v1/test", hash_including(log_format: :curl)).
and_return(response)

base = Bamboozled::API::Base.new({ subdomain: 'x', api_domain: 'api.bamboohr.co.uk' }, "x", { log_format: :curl })
base.send(:request, :get, "test")
end

it "takes HTTParty options as a constructor parameter" do
expect { Bamboozled::API::Base.new("x", "x", { log_format: :curl }) }.not_to raise_error
end
Expand Down
228 changes: 123 additions & 105 deletions spec/lib/bamboozled/api/employee_spec.rb
Original file line number Diff line number Diff line change
@@ -1,140 +1,158 @@
require "spec_helper"

RSpec.describe "Employees" do
before do
@client = Bamboozled.client(subdomain: "x", api_key: "x")
end
context 'UK domain specified' do
before do
@client = Bamboozled.client(subdomain: "x", api_domain: 'api.bamboohr.co.uk', api_key: "x")
end

it "Gets all employees" do
response = File.new("spec/fixtures/all_employees.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)
it "Gets all employees from UK domain if specified" do
response = File.new("spec/fixtures/all_employees.json")
stub_request(:any, /.*api\.bamboohr\.co\.uk.*/).to_return(response)

employees = @client.employee.all
employees = @client.employee.all

expect(employees).to be_a Array
expect(employees.first.count).to eq 7
expect(employees).to be_a Array
expect(employees.first.count).to eq 7
end
end

it "Gets one employee" do
response = File.new("spec/fixtures/one_employee.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)
context 'no api domain specified' do
before do
@client = Bamboozled.client(subdomain: "x", api_key: "x")
end

employee = @client.employee.find(1234)
it "Gets all employees" do
response = File.new("spec/fixtures/all_employees.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)

expect(employee).to be_a Hash
expect(employee.count).to eq 3
expect(employee["firstName"]).to eq "John"
expect(employee["lastName"]).to eq "Doe"
end
employees = @client.employee.all

expect(employees).to be_a Array
expect(employees.first.count).to eq 7
end

it "Gets one employee" do
response = File.new("spec/fixtures/one_employee.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)

employee = @client.employee.find(1234)

it "Gets employee job info" do
response = File.new("spec/fixtures/job_info.xml")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)

info = @client.employee.job_info(1234)

expect(info).to be_a Hash
expect(info[:table][:row].first[:employeeId]).to eq "100"

info[:table][:row].first[:field].each do |f|
case f[:id]
when "location"
expect(f[:__content__]).to eq "New York Office"
when "division"
expect(f[:__content__]).to eq "Sprockets"
when "department"
expect(f[:__content__]).to eq "Research and Development"
when "jobTitle"
expect(f[:__content__]).to eq "Machinist"
when "reportsTo"
expect(f[:__content__]).to eq "John Smith"
expect(employee).to be_a Hash
expect(employee.count).to eq 3
expect(employee["firstName"]).to eq "John"
expect(employee["lastName"]).to eq "Doe"
end

it "Gets employee job info" do
response = File.new("spec/fixtures/job_info.xml")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)

info = @client.employee.job_info(1234)

expect(info).to be_a Hash
expect(info[:table][:row].first[:employeeId]).to eq "100"

info[:table][:row].first[:field].each do |f|
case f[:id]
when "location"
expect(f[:__content__]).to eq "New York Office"
when "division"
expect(f[:__content__]).to eq "Sprockets"
when "department"
expect(f[:__content__]).to eq "Research and Development"
when "jobTitle"
expect(f[:__content__]).to eq "Machinist"
when "reportsTo"
expect(f[:__content__]).to eq "John Smith"
end
end
end
end

it "Gets an employee's time off estimate" do
response = File.new("spec/fixtures/time_off_estimate.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)
it "Gets an employee's time off estimate" do
response = File.new("spec/fixtures/time_off_estimate.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)

future = Time.now + (60 * 60 * 24 * 180)
estimate = @client.employee.time_off_estimate(1234, future)
future = Time.now + (60 * 60 * 24 * 180)
estimate = @client.employee.time_off_estimate(1234, future)

expect(estimate).to be_a Hash
expect(estimate["estimates"].keys).to match_array %w(end estimate)
expect(estimate["estimates"]["estimate"].count).to eq 2
expect(estimate["estimates"]["estimate"].first.keys)
.to match_array %w(timeOffType name units balance)
end
expect(estimate).to be_a Hash
expect(estimate["estimates"].keys).to match_array %w(end estimate)
expect(estimate["estimates"]["estimate"].count).to eq 2
expect(estimate["estimates"]["estimate"].first.keys)
.to match_array %w(timeOffType name units balance)
end

it "returns the proper url using employee email address" do
hashed = "4fdce145bab6d27d69e34403f99fd11c" # Hash of me@here.com
required_url = "http://x.bamboohr.com/employees/photos/?h=#{hashed}"
it "returns the proper url using employee email address" do
hashed = "4fdce145bab6d27d69e34403f99fd11c" # Hash of me@here.com
required_url = "http://x.bamboohr.com/employees/photos/?h=#{hashed}"

# Normal
url = @client.employee.photo_url("me@here.com")
expect(url).to eq required_url
# Normal
url = @client.employee.photo_url("me@here.com")
expect(url).to eq required_url

# Email with spaces
url = @client.employee.photo_url(" me@here.com ")
expect(url).to eq required_url
# Email with spaces
url = @client.employee.photo_url(" me@here.com ")
expect(url).to eq required_url

# Uppercase emails
url = @client.employee.photo_url("ME@HERE.COM")
expect(url).to eq required_url
end
# Uppercase emails
url = @client.employee.photo_url("ME@HERE.COM")
expect(url).to eq required_url
end

it "returns the proper url using employee id" do
response = File.new("spec/fixtures/employee_emails.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)
it "returns the proper url using employee id" do
response = File.new("spec/fixtures/employee_emails.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)

hashed = "4fdce145bab6d27d69e34403f99fd11c"
required_url = "http://x.bamboohr.com/employees/photos/?h=#{hashed}"
hashed = "4fdce145bab6d27d69e34403f99fd11c"
required_url = "http://x.bamboohr.com/employees/photos/?h=#{hashed}"

url = @client.employee.photo_url(123)
expect(url).to eq required_url
end
url = @client.employee.photo_url(123)
expect(url).to eq required_url
end

it "Gets all employee records which have changed since a given date" do
response = File.new("spec/fixtures/last_changed.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)
it "Gets all employee records which have changed since a given date" do
response = File.new("spec/fixtures/last_changed.json")
stub_request(:any, /.*api\.bamboohr\.com.*/).to_return(response)

employees = @client.employee.last_changed("2011-06-02T19:26:23+00:00")
employees = @client.employee.last_changed("2011-06-02T19:26:23+00:00")

expect(employees).to be_a Hash
expect(employees.keys.count).to eq 4
end
expect(employees).to be_a Hash
expect(employees.keys.count).to eq 4
end

describe "#add" do
it "creates a new employee in BambooHR" do
xml = YAML.load_file("spec/fixtures/add_employee_xml.yml")
response = File.new("spec/fixtures/add_employee_response.json")
details = JSON.parse(File.read("spec/fixtures/add_employee_details.json"))
describe "#add" do
it "creates a new employee in BambooHR" do
xml = YAML.load_file("spec/fixtures/add_employee_xml.yml")
response = File.new("spec/fixtures/add_employee_response.json")
details = JSON.parse(File.read("spec/fixtures/add_employee_details.json"))

stub_request(:post, /.*api\.bamboohr\.com.*/)
.with(xml).to_return(response)
stub_request(:post, /.*api\.bamboohr\.com.*/)
.with(xml).to_return(response)

employee = @client.employee.add(details)
location = employee["headers"]["location"]
employee = @client.employee.add(details)
location = employee["headers"]["location"]

expect(location).to eq "https://api.bamboohr.com/api/gateway.php/alphasights/v1/employees/44259"
expect(location).to eq "https://api.bamboohr.com/api/gateway.php/alphasights/v1/employees/44259"
end
end
end

describe "#update" do
it "updates an employee in BambooHR" do
xml = YAML.load_file("spec/fixtures/update_employee_xml.yml")
response = File.new("spec/fixtures/update_employee_response.json")
details = JSON.parse(File.read("spec/fixtures/update_employee_details.json"))
url = "https://x:x@api.bamboohr.com/api/gateway.php/x/v1/employees/1234"

stub_request(:post, url).with(xml).to_return(response)
employee = @client.employee.update("1234", details)
expected_headers = {
"content-type" => ["application/json; charset=utf-8"],
"date" => ["Tue, 17 Jun 2014 19:25:35 UTC"]
}

expect(employee["headers"]).to eq(expected_headers)
describe "#update" do
it "updates an employee in BambooHR" do
xml = YAML.load_file("spec/fixtures/update_employee_xml.yml")
response = File.new("spec/fixtures/update_employee_response.json")
details = JSON.parse(File.read("spec/fixtures/update_employee_details.json"))
url = "https://x:x@api.bamboohr.com/api/gateway.php/x/v1/employees/1234"

stub_request(:post, url).with(xml).to_return(response)
employee = @client.employee.update("1234", details)
expected_headers = {
"content-type" => ["application/json; charset=utf-8"],
"date" => ["Tue, 17 Jun 2014 19:25:35 UTC"]
}

expect(employee["headers"]).to eq(expected_headers)
end
end
end
end