| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::BambooHR::UserAgent; | 
| 2 |  |  |  |  |  |  | $WebService::BambooHR::UserAgent::VERSION = '0.04'; | 
| 3 | 5 |  |  | 5 |  | 4456 | use 5.006; | 
|  | 5 |  |  |  |  | 35 |  | 
|  | 5 |  |  |  |  | 224 |  | 
| 4 | 5 |  |  | 5 |  | 34 | use Moo::Role; | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 43 |  | 
| 5 | 5 |  |  | 5 |  | 1944 | use HTTP::Tiny; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 131 |  | 
| 6 | 5 |  |  | 5 |  | 4988 | use MIME::Base64; | 
|  | 5 |  |  |  |  | 4468 |  | 
|  | 5 |  |  |  |  | 363 |  | 
| 7 | 5 |  |  | 5 |  | 36 | use WebService::BambooHR::Exception; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 7505 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | my $COMMA = ','; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | has 'base_url' => | 
| 12 |  |  |  |  |  |  | ( | 
| 13 |  |  |  |  |  |  | is => 'ro', | 
| 14 |  |  |  |  |  |  | default => sub { return 'https://api.bamboohr.com/api/gateway.php'; }, | 
| 15 |  |  |  |  |  |  | ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | has 'api_key' => | 
| 18 |  |  |  |  |  |  | ( | 
| 19 |  |  |  |  |  |  | is       => 'ro', | 
| 20 |  |  |  |  |  |  | ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | has 'ua' => | 
| 23 |  |  |  |  |  |  | ( | 
| 24 |  |  |  |  |  |  | is      => 'rw', | 
| 25 |  |  |  |  |  |  | default => sub { HTTP::Tiny->new(agent => "WebService-BambooHR/0.01"); }, | 
| 26 |  |  |  |  |  |  | ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | has 'company' => | 
| 29 |  |  |  |  |  |  | ( | 
| 30 |  |  |  |  |  |  | is       => 'ro', | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub _get | 
| 34 |  |  |  |  |  |  | { | 
| 35 | 3 |  |  | 3 |  | 6 | my $self     = shift; | 
| 36 | 3 |  |  |  |  | 9 | my $url      = shift; | 
| 37 | 3 |  |  |  |  | 28 | my $full_url = $self->base_url.'/'.$self->company.'/v1/'.$url; | 
| 38 | 3 |  |  |  |  | 37 | my $ua       = $self->ua; | 
| 39 | 3 |  |  |  |  | 53 | my $auth     = encode_base64($self->api_key.':x', ''); | 
| 40 | 3 |  |  |  |  | 100 | my $headers  = { Accept => 'application/json', 'Authorization' => "Basic $auth" }; | 
| 41 | 3 |  |  |  |  | 34 | my $response = $ua->request('GET', $full_url, { headers => $headers }); | 
| 42 | 3 |  |  |  |  | 2046926 | my @caller   = caller(1); | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | # If the public method has wrapped us in eval { ... } | 
| 45 |  |  |  |  |  |  | # then we need to go one level higher up the call stack | 
| 46 | 3 | 100 |  |  |  | 36 | @caller = caller(2) if $caller[3] eq '(eval)'; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 3 | 50 |  |  |  | 21 | if (not $response->{success}) { | 
| 49 | 0 |  |  |  |  | 0 | WebService::BambooHR::Exception->throw({ | 
| 50 |  |  |  |  |  |  | method      => $caller[3], | 
| 51 |  |  |  |  |  |  | message     => "request to API failed", | 
| 52 |  |  |  |  |  |  | code        => $response->{status}, | 
| 53 |  |  |  |  |  |  | reason      => $response->{reason}, | 
| 54 |  |  |  |  |  |  | filename    => $caller[1], | 
| 55 |  |  |  |  |  |  | line_number => $caller[2], | 
| 56 |  |  |  |  |  |  | }); | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 3 |  |  |  |  | 34 | return $response; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub _post | 
| 63 |  |  |  |  |  |  | { | 
| 64 | 1 |  |  | 1 |  | 2 | my $self     = shift; | 
| 65 | 1 |  |  |  |  | 4 | my $url      = shift; | 
| 66 | 1 |  |  |  |  | 3 | my $content  = shift; | 
| 67 | 1 |  |  |  |  | 27 | my $full_url = $self->base_url.'/'.$self->company.'/v1/'.$url; | 
| 68 | 1 |  |  |  |  | 11 | my $ua       = $self->ua; | 
| 69 | 1 |  |  |  |  | 32 | my $auth     = encode_base64($self->api_key.':x', ''); | 
| 70 | 1 |  |  |  |  | 7 | my $headers  = { Accept => 'application/json', 'Authorization' => "Basic $auth" }; | 
| 71 | 1 |  |  |  |  | 10 | my $response = $ua->request('POST', $full_url, { headers => $headers, content => $content }); | 
| 72 | 1 |  |  |  |  | 1687675 | my @caller   = caller(1); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | # If the public method has wrapped us in eval { ... } | 
| 75 |  |  |  |  |  |  | # then we need to go one level higher up the call stack | 
| 76 | 1 | 50 |  |  |  | 10 | @caller = caller(2) if $caller[3] eq '(eval)'; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 1 | 50 |  |  |  | 6 | if (not $response->{success}) { | 
| 79 | 0 |  |  |  |  | 0 | WebService::BambooHR::Exception->throw({ | 
| 80 |  |  |  |  |  |  | method      => $caller[3], | 
| 81 |  |  |  |  |  |  | message     => "request to API failed", | 
| 82 |  |  |  |  |  |  | code        => $response->{status}, | 
| 83 |  |  |  |  |  |  | reason      => $response->{reason}, | 
| 84 |  |  |  |  |  |  | filename    => $caller[1], | 
| 85 |  |  |  |  |  |  | line_number => $caller[2], | 
| 86 |  |  |  |  |  |  | }); | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 1 |  |  |  |  | 16 | return $response; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | my %field = | 
| 93 |  |  |  |  |  |  | ( | 
| 94 |  |  |  |  |  |  | address1                => q{The employee's first address line.}, | 
| 95 |  |  |  |  |  |  | address2                => q{The employee's second address line.}, | 
| 96 |  |  |  |  |  |  | age                     => q{The employee's age. To change age, update dateOfBirth field.}, | 
| 97 |  |  |  |  |  |  | bestEmail               => q{The employee's work email if set, otherwise their home email.}, | 
| 98 |  |  |  |  |  |  | birthday                => q{The employee's month and day of birth. To change birthday, update dateOfBirth field.}, | 
| 99 |  |  |  |  |  |  | city                    => q{The employee's city.}, | 
| 100 |  |  |  |  |  |  | country                 => q{The employee's country.}, | 
| 101 |  |  |  |  |  |  | dateOfBirth             => q{The date the employee was born.}, | 
| 102 |  |  |  |  |  |  | department              => q{The employee's CURRENT department.}, | 
| 103 |  |  |  |  |  |  | division                => q{The employee's CURRENT division.}, | 
| 104 |  |  |  |  |  |  | eeo                     => q{The employee's EEO job category. These are defined by the U.S. Equal Employment Opportunity Commission.}, | 
| 105 |  |  |  |  |  |  | employeeAccess          => q{Whether the employee is allowed to access BambooHR.}, | 
| 106 |  |  |  |  |  |  | employeeNumber          => q{Employee number (assigned by your company).}, | 
| 107 |  |  |  |  |  |  | employmentStatus        => q{DEPRECATED. Please use "status" instead. The employee's employee status (Active or Inactive).}, | 
| 108 |  |  |  |  |  |  | employmentHistoryStatus => q{The employee's CURRENT employment status. Options are customized by account.}, | 
| 109 |  |  |  |  |  |  | ethnicity               => q{The employee's ethnicity.}, | 
| 110 |  |  |  |  |  |  | exempt                  => q{The FLSA employee exemption code (Exempt or Non-exempt).}, | 
| 111 |  |  |  |  |  |  | firstName               => q{The employee's first name.}, | 
| 112 |  |  |  |  |  |  | flsaCode                => q{The employee's FLSA code (Exempt or Non-exempt).}, | 
| 113 |  |  |  |  |  |  | fullName1               => q{The employee's first and last name. (e.g., John Doe). Read only.}, | 
| 114 |  |  |  |  |  |  | fullName2               => q{The employee's last and first name. (e.g., Doe, John). Read only.}, | 
| 115 |  |  |  |  |  |  | fullName3               => q{The employee's full name and their nickname. (e.g., Doe, John Quentin (JDog)). Read only.}, | 
| 116 |  |  |  |  |  |  | fullName4               => q{The employee's full name without their nickname, last name first. (e.g., Doe, John Quentin). Read only.}, | 
| 117 |  |  |  |  |  |  | fullName5               => q{The employee's full name without their nickname, first name first. (e.g., John Quentin Doe). Read only.}, | 
| 118 |  |  |  |  |  |  | displayName             => q{The employee's name displayed in a format configured by the user. Read only.}, | 
| 119 |  |  |  |  |  |  | gender                  => q{The employee's gender (Male or Female).}, | 
| 120 |  |  |  |  |  |  | hireDate                => q{The date the employee was hired.}, | 
| 121 |  |  |  |  |  |  | homeEmail               => q{The employee's home email address.}, | 
| 122 |  |  |  |  |  |  | homePhone               => q{The employee's home phone number.}, | 
| 123 |  |  |  |  |  |  | id                      => q{The employee ID automatically assigned by BambooHR. Read only.}, | 
| 124 |  |  |  |  |  |  | jobTitle                => q{The CURRENT value of the employee's job title, updating this field will create a new row in position history.}, | 
| 125 |  |  |  |  |  |  | lastChanged             => q{The date and time that the employee record was last changed.}, | 
| 126 |  |  |  |  |  |  | lastName                => q{The employee's last name.}, | 
| 127 |  |  |  |  |  |  | location                => q{The employee's CURRENT location.}, | 
| 128 |  |  |  |  |  |  | maritalStatus           => q{The employee's marital status (Single, Married, or Domestic Partnership).}, | 
| 129 |  |  |  |  |  |  | middleName              => q{The employee's middle name.}, | 
| 130 |  |  |  |  |  |  | mobilePhone             => q{The employee's mobile phone number.}, | 
| 131 |  |  |  |  |  |  | nickname                => q{The employee's nickname.}, | 
| 132 |  |  |  |  |  |  | payChangeReason         => q{The reason for the employee's last pay rate change.}, | 
| 133 |  |  |  |  |  |  | payGroup                => q{The custom pay group that the employee belongs to.}, | 
| 134 |  |  |  |  |  |  | payGroupId              => q{The ID value corresponding to the pay group that an employee belongs to.}, | 
| 135 |  |  |  |  |  |  | payRate                 => q{The employee's CURRENT pay rate (e.g., $8.25).}, | 
| 136 |  |  |  |  |  |  | payRateEffectiveDate    => q{The day the most recent change was made.}, | 
| 137 |  |  |  |  |  |  | payType                 => q{The employee's CURRENT pay type. ie: "hourly","salary","commission","exception hourly","monthly","weekly","piece rate","contract","daily","pro rata".}, | 
| 138 |  |  |  |  |  |  | ssn                     => q{The employee's Social Security number.}, | 
| 139 |  |  |  |  |  |  | sin                     => q{The employee's Canadian Social Insurance Number.}, | 
| 140 |  |  |  |  |  |  | state                   => q{The employee's state/province.}, | 
| 141 |  |  |  |  |  |  | stateCode               => q{The 2 character abbreviation for the employee's state (US only). Read only.}, | 
| 142 |  |  |  |  |  |  | status                  => q{The employee's employee status (Active or Inactive).}, | 
| 143 |  |  |  |  |  |  | supervisor              => q{The employee’s CURRENT supervisor. Read only.}, | 
| 144 |  |  |  |  |  |  | supervisorId            => q{The 'employeeNumber' of the employee's CURRENT supervisor. Read only.}, | 
| 145 |  |  |  |  |  |  | supervisorEId           => q{The ID of the employee's CURRENT supervisor. Read only.}, | 
| 146 |  |  |  |  |  |  | terminationDate         => q{date the employee was terminated.}, | 
| 147 |  |  |  |  |  |  | workEmail               => q{The employee's work email address.}, | 
| 148 |  |  |  |  |  |  | workPhone               => q{The employee's work phone number, without extension.}, | 
| 149 |  |  |  |  |  |  | workPhonePlusExtension  => q{The employee's work phone and extension. Read only.}, | 
| 150 |  |  |  |  |  |  | workPhoneExtension      => q{The employee's work phone extension (if any).}, | 
| 151 |  |  |  |  |  |  | zipcode                 => q{The employee's ZIP code.}, | 
| 152 |  |  |  |  |  |  | photoUploaded           => q{The employee has uploaded a photo.}, | 
| 153 |  |  |  |  |  |  | rehireDate              => q{The date the employee was rehired.}, | 
| 154 |  |  |  |  |  |  | standardHoursPerWeek    => q{The number of hours the employee works in a standard week.}, | 
| 155 |  |  |  |  |  |  | bonusDate               => q{The date of the last bonus.}, | 
| 156 |  |  |  |  |  |  | bonusAmount             => q{The amount of the most recent bonus.}, | 
| 157 |  |  |  |  |  |  | bonusReason             => q{The reason for the most recent bonus.}, | 
| 158 |  |  |  |  |  |  | bonusComment            => q{Comment about the most recent bonus.}, | 
| 159 |  |  |  |  |  |  | commissionDate          => q{The date of the last commission.}, | 
| 160 |  |  |  |  |  |  | commisionDate           => q{This field name contains a typo, and exists for backwards compatibility.}, | 
| 161 |  |  |  |  |  |  | commissionAmount        => q{The amount of the most recent commission.}, | 
| 162 |  |  |  |  |  |  | commissionComment       => q{Comment about the most recent commission.}, | 
| 163 |  |  |  |  |  |  | 1360                    => q{The termination type when an employee has been terminated.}, | 
| 164 |  |  |  |  |  |  | 1361                    => q{The termination reason when an employee has been terminated.}, | 
| 165 |  |  |  |  |  |  | 1610                    => q{Employee self-service access.}, | 
| 166 |  |  |  |  |  |  | ); | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | my %field_alias = | 
| 169 |  |  |  |  |  |  | ( | 
| 170 |  |  |  |  |  |  | selfServiceAccess => '1610', | 
| 171 |  |  |  |  |  |  | terminationType   => '1360', | 
| 172 |  |  |  |  |  |  | terminationReason => '1361', | 
| 173 |  |  |  |  |  |  | ); | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | sub _check_fields | 
| 176 |  |  |  |  |  |  | { | 
| 177 | 3 |  |  | 3 |  | 7 | my $self        = shift; | 
| 178 | 3 |  |  |  |  | 23 | my @field_names = @_; | 
| 179 | 3 |  |  |  |  | 26 | my @caller      = caller(1); | 
| 180 | 3 |  |  |  |  | 6 | my @mapped_names; | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | FIELD: | 
| 183 | 3 |  |  |  |  | 10 | foreach my $field_name (@field_names) { | 
| 184 |  |  |  |  |  |  |  | 
| 185 | 145 | 100 |  |  |  | 227 | if (exists($field{$field_name})) { | 
|  |  | 50 |  |  |  |  |  | 
| 186 | 144 |  |  |  |  | 268 | push(@mapped_names, $field_name); | 
| 187 | 144 |  |  |  |  | 269 | next FIELD; | 
| 188 |  |  |  |  |  |  | } | 
| 189 |  |  |  |  |  |  | elsif (exists($field_alias{$field_name})) { | 
| 190 | 0 |  |  |  |  | 0 | push(@mapped_names, $field_alias{ $field_name }); | 
| 191 | 0 |  |  |  |  | 0 | next FIELD; | 
| 192 |  |  |  |  |  |  | } | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | WebService::BambooHR::Exception->throw({ | 
| 195 | 1 |  |  |  |  | 21 | method      => $caller[3], | 
| 196 |  |  |  |  |  |  | message     => "unknown field name '$field_name'", | 
| 197 |  |  |  |  |  |  | code        => 400, | 
| 198 |  |  |  |  |  |  | reason      => 'Bad Request', | 
| 199 |  |  |  |  |  |  | filename    => $caller[1], | 
| 200 |  |  |  |  |  |  | line_number => $caller[2], | 
| 201 |  |  |  |  |  |  | }); | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | } | 
| 204 |  |  |  |  |  |  |  | 
| 205 | 2 |  |  |  |  | 77 | return @mapped_names; | 
| 206 |  |  |  |  |  |  | } | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | sub _employee_record | 
| 209 |  |  |  |  |  |  | { | 
| 210 | 0 |  |  | 0 |  | 0 | my $self      = shift; | 
| 211 | 0 |  |  |  |  | 0 | my $field_ref = shift; | 
| 212 | 0 |  |  |  |  | 0 | my %mapped_field; | 
| 213 |  |  |  |  |  |  | my $mapped_name; | 
| 214 | 0 |  |  |  |  | 0 | local $_; | 
| 215 |  |  |  |  |  |  |  | 
| 216 | 0 |  |  |  |  | 0 | foreach my $field_name (keys %$field_ref) { | 
| 217 | 0 | 0 |  |  |  | 0 | $mapped_name = $field_alias{$field_name} if exists($field_alias{$field_name}); | 
| 218 | 0 |  |  |  |  | 0 | $mapped_field{$mapped_name} = $field_ref->{ $field_name }; | 
| 219 |  |  |  |  |  |  | } | 
| 220 |  |  |  |  |  |  |  | 
| 221 | 0 |  |  |  |  | 0 | return "\n  " | 
| 222 |  |  |  |  |  |  | .join("\n  ", | 
| 223 | 0 |  |  |  |  | 0 | map { qq[$mapped_field{$_}] } | 
| 224 |  |  |  |  |  |  | keys(%mapped_field) | 
| 225 |  |  |  |  |  |  | ) | 
| 226 |  |  |  |  |  |  | ."\n" | 
| 227 |  |  |  |  |  |  | ."\n"; | 
| 228 |  |  |  |  |  |  | } | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  | sub _field_list | 
| 231 |  |  |  |  |  |  | { | 
| 232 | 2 |  |  | 2 |  | 73 | return keys %field; | 
| 233 |  |  |  |  |  |  | } | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | 1; | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | =head1 NAME | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | WebService::BambooHR::UserAgent - handles low-level HTTP requests to BambooHR | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | use WebService::BambooHR::UserAgent; | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | my $ua = WebService::BambooHR::UserAgent->new( | 
| 246 |  |  |  |  |  |  | company => 'foobar', | 
| 247 |  |  |  |  |  |  | api_key => '.............' | 
| 248 |  |  |  |  |  |  | ); | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | This is used by L, and most users shouldn't | 
| 253 |  |  |  |  |  |  | even need to know it exists. | 
| 254 |  |  |  |  |  |  |  |