Skip to content

Commit

Permalink
Merge pull request #2 from JobBrander/update_setters
Browse files Browse the repository at this point in the history
Update setters
  • Loading branch information
karllhughes committed Oct 15, 2015
2 parents 99ce576 + 0764c39 commit 31795b0
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 70 deletions.
14 changes: 4 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ php:
before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source --dev
- travis_retry pyrus install pear/PHP_CodeSniffer
- travis_retry phpenv rehash

script:
- phpcs --standard=psr2 src/
- phpunit --coverage-text --coverage-clover=coverage.clover
- ./vendor/bin/phpcs --standard=psr2 src/
- ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

matrix:
allow_failures:
- php: 7.0
- php: hhvm
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" != "7.0" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" != "7.0" ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# Changelog
All Notable changes to `jobs-ziprecruiter` will be documented in this file

## 0.2.0 - 2015-10-15

### Added
- Support for all setters in API
- Readme documentation for all supported methods

### Deprecated
- Nothing

### Fixed
- Sorting methods alphabetically
- Travis-ci support for PHP 7.0 and HHVM

### Removed
- setCity and setState methods not supported by official API

### Security
- Nothing

## 0.1.0 - 2015-08-06

### Added
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,22 @@ Usage is the same as Job Branders's Jobs Client, using `\JobBrander\Jobs\Client\

```php
$client = new JobBrander\Jobs\Client\Provider\Ziprecruiter([
'apiKey' => 'YOUR ZIPRECRUITER API KEY'
'api_key' => 'YOUR ZIPRECRUITER API KEY'
]);

// Search for 200 job listings for 'project manager' in Chicago, IL
$jobs = $client->setKeyword('project manager')
->setCity('Chicago')
->setState('IL')
->setCount(200)
$jobs = $client
// Supported by Ziprecruiter
->setApiKey('') // assigned API key
->setSearch('') // search terms, e.g. “Inside Sales”
->setLocation('Chicago, IL') // location, e.g., “San Francisco, CA”
->setRadiusMiles() // distance of the job relative to the location
->setPage() // current page ranging from 1-N
->setJobsPerPage() // number of job results to show per page. A maximum of 500 results are returned through pagination
->setDaysAgo() // only show jobs posted within this number of days
// Additional setters
->setKeyword('project manager') // Alias for 'setSearch()'
->setCount(200) // Alias for setPage()
->getJobs();
```

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
"jobbrander/jobs-common": "~1.0.3"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"phpunit/phpunit": ">=4.6",
"phpunit/php-code-coverage": "~2.0",
"mockery/mockery": ">=0.9.4"
"mockery/mockery": ">=0.9.4",
"squizlabs/php_codesniffer": "~2.0"
},
"autoload": {
"psr-4": {
Expand Down
97 changes: 58 additions & 39 deletions src/Ziprecruiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,62 @@
class Ziprecruiter extends AbstractProvider
{
/**
* API Key
* Map of setter methods to query parameters
*
* @var string
* @var array
*/
protected $apiKey;
protected $queryMap = [
'setApiKey' => 'api_key',
'setSearch' => 'search',
'setLocation' => 'location',
'setRadiusMiles' => 'radius_miles',
'setPage' => 'page',
'setJobsPerPage' => 'jobs_per_page',
'setDaysAgo' => 'days_ago',
'setKeyword' => 'search',
'setCount' => 'jobs_per_page',
];

/**
* Query params
* Current api query parameters
*
* @var array
*/
protected $queryParams = [];
protected $queryParams = [
'api_key' => null,
'search' => null,
'location' => null,
'radius_miles' => null,
'page' => null,
'jobs_per_page' => null,
'days_ago' => null,
];

/**
* Create new Ziprecruiter jobs client.
*
* @param array $parameters
*/
public function __construct($parameters = [])
{
parent::__construct($parameters);
array_walk($parameters, [$this, 'updateQuery']);
}

/**
* Add query params, if valid
* Magic method to handle get and set methods for properties
*
* @param string $value
* @param string $key
* @param string $method
* @param array $parameters
*
* @return void
* @return mixed
*/
private function addToQueryStringIfValid($value, $key)
public function __call($method, $parameters)
{
$computed_value = $this->$value();
if (!is_null($computed_value)) {
$this->queryParams[$key] = $computed_value;
if (isset($this->queryMap[$method], $parameters[0])) {
$this->updateQuery($parameters[0], $this->queryMap[$method]);
}
return parent::__call($method, $parameters);
}

/**
Expand Down Expand Up @@ -99,39 +128,13 @@ public function getListingsPath()
return 'jobs';
}

/**
* Get combined location
*
* @return string
*/
public function getLocation()
{
$location = ($this->city ? $this->city.', ' : null).($this->state ?: null);

if ($location) {
return $location;
}

return null;
}

/**
* Get query string for client based on properties
*
* @return string
*/
public function getQueryString()
{
$query_params = [
'api_key' => 'getApiKey',
'search' => 'getKeyword',
'location' => 'getLocation',
'page' => 'getPage',
'jobs_per_page' => 'getCount',
];

array_walk($query_params, [$this, 'addToQueryStringIfValid']);

return http_build_query($this->queryParams);
}

Expand All @@ -156,4 +159,20 @@ public function getVerb()
{
return 'GET';
}

/**
* Attempts to update current query parameters.
*
* @param string $value
* @param string $key
*
* @return Careerbuilder
*/
protected function updateQuery($value, $key)
{
if (array_key_exists($key, $this->queryParams)) {
$this->queryParams[$key] = $value;
}
return $this;
}
}
19 changes: 5 additions & 14 deletions tests/src/ZiprecruiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ZiprecruiterTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->params = [
'apiKey' => '12345667'
'api_key' => '12345667'
];
$this->client = new Ziprecruiter($this->params);
}
Expand Down Expand Up @@ -63,7 +63,7 @@ public function testUrlIncludesLocationWhenCityAndStateProvided()
$state = uniqid();
$param = 'location='.urlencode($city.', '.$state);

$url = $this->client->setCity($city)->setState($state)->getUrl();
$url = $this->client->setLocation($city.', '.$state)->getUrl();

$this->assertContains($param, $url);
}
Expand All @@ -73,7 +73,7 @@ public function testUrlIncludesLocationWhenCityProvided()
$city = uniqid();
$param = 'location='.urlencode($city);

$url = $this->client->setCity($city)->getUrl();
$url = $this->client->setLocation($city)->getUrl();

$this->assertContains($param, $url);
}
Expand All @@ -83,7 +83,7 @@ public function testUrlIncludesLocationWhenStateProvided()
$state = uniqid();
$param = 'location='.urlencode($state);

$url = $this->client->setState($state)->getUrl();
$url = $this->client->setLocation($state)->getUrl();

$this->assertContains($param, $url);
}
Expand Down Expand Up @@ -118,22 +118,13 @@ public function testUrlNotIncludesLimitWhenNotProvided()

public function testUrlIncludesApiKeyWhenProvided()
{
$param = 'api_key='.$this->params['apiKey'];
$param = 'api_key='.$this->params['api_key'];

$url = $this->client->getUrl();

$this->assertContains($param, $url);
}

public function testUrlNotIncludesApiKeyWhenNotProvided()
{
$param = 'api_key=';

$url = $this->client->setApiKey(null)->getUrl();

$this->assertNotContains($param, $url);
}

public function testUrlIncludesStartWhenProvided()
{
$page = uniqid();
Expand Down

0 comments on commit 31795b0

Please sign in to comment.