Skip to content

Commit

Permalink
Merge pull request #1 from kylefdoherty/create_employees
Browse files Browse the repository at this point in the history
Create employees
  • Loading branch information
kylefdoherty committed Jul 28, 2015
2 parents aed0839 + 75ce3d3 commit 264c9a2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 12 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ DEPENDENCIES
rake
turn
webmock

BUNDLED WITH
1.10.3
6 changes: 5 additions & 1 deletion lib/bamboozled/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ def request(method, path, options = {})
case response.code
when 200..201
begin
JSON.parse(response).with_indifferent_access
if response.body.to_s.empty?
{"headers" => response.headers}.with_indifferent_access
else
JSON.parse(response.body).with_indifferent_access
end
rescue
MultiXml.parse(response, symbolize_keys: true)
end
Expand Down
16 changes: 16 additions & 0 deletions lib/bamboozled/api/employee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ def photo_url(employee)
"http://#{@subdomain}.bamboohr.com/employees/photos/?h=#{digest}"
end

def add(employee_details:)
details = generate_xml(employee_details)
options = {body: details}

request(:post, "employees/", options)
end

private

def generate_xml(employee_details)
"".tap do |xml|
xml << "<employee>"
employee_details.each { |k, v| xml << "<field id='#{k}'>#{v}</field>" }
xml << "</employee>"
end
end
end
end
end
7 changes: 7 additions & 0 deletions spec/fixtures/add_employee_details.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"firstName": "Bruce",
"lastName": "Wayne",
"workEmail": "bruce.wayne@gmail.com",
"jobTitle": "Batman",
"city": "Gotham"
}
5 changes: 5 additions & 0 deletions spec/fixtures/add_employee_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
date: Tue, 17 Jun 2014 19:25:35 UTC
location: https://api.bamboohr.com/api/gateway.php/alphasights/v1/employees/44259

8 changes: 8 additions & 0 deletions spec/fixtures/add_employee_xml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body:
<employee><field id='firstName'>Bruce</field><field id='lastName'>Wayne</field><field id='workEmail'>bruce.wayne@gmail.com</field><field id='jobTitle'>Batman</field><field id='city'>Gotham</field></employee>
headers:
Accept:
- application/json
User-Agent:
- Bamboozled/0.0.7

18 changes: 18 additions & 0 deletions spec/lib/bamboozled/employee_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,26 @@
url.must_equal required_url
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'))

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

employee = @client.employee.add(employee_details: details)

employee["headers"]["location"].
must_equal "https://api.bamboohr.com/api/gateway.php/alphasights/v1/employees/44259"
end
end

# TODO - Figure out how to test this with webmock
# it 'returns binary data for an employee photo' do
# end

end

11 changes: 0 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,3 @@
#dependencies
require 'minitest/autorun'
require 'webmock/minitest'

require 'turn'

Turn.config do |c|
# :outline - turn's original case/test outline mode [default]
c.format = :outline
# turn on invoke/execute tracing, enable full backtrace
c.trace = true
# use humanized test names (works only with :outline format)
c.natural = true
end

0 comments on commit 264c9a2

Please sign in to comment.