Skip to content

Commit

Permalink
Merge pull request #2 from JobBrander/add-all-api-params
Browse files Browse the repository at this point in the history
Add support for all API parameters
  • Loading branch information
karllhughes committed Sep 1, 2015
2 parents 29b6d18 + cf11654 commit 6cbf0d6
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 148 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ script:
- ./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
- 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

17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# Changelog
All Notable changes to `jobs-indeed` will be documented in this file

## 0.4.0 - 2015-08-31

### Added
- Support for all query parameters available on the Indeed `http://api.indeed.com/ads/apisearch` resource

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing

## 0.3.6 - 2015-08-05

### Added
Expand Down
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,30 @@ Usage is the same as Job Branders's Jobs Client, using `\JobBrander\Jobs\Client\

```php
$client = new JobBrander\Jobs\Client\Provider\Indeed([
'publisherId' => 'YOUR INDEED PUBLISHER ID',
'version' => 2,
'publisher' => 'YOUR INDEED PUBLISHER ID',
'v' => 2, // Optional. Default is 2.
'highlight' => 0,
]);

// Search for 200 job listings for 'project manager' in Chicago, IL
$jobs = $client->setKeyword('project manager') // Query. By default terms are ANDed. To see what is possible, use the [advanced search page](http://www.indeed.com/advanced_search) to perform a search and then check the url for the q value.
->setCity('Chicago') // Location uses a postal code or a "city, state/province/region" combination
->setState('IL') // Location uses a postal code or a "city, state/province/region" combination
->setCount(200) // Maximum number of results returned per query. Default is 10
$jobs = $client
->setKeyword('project manager') // Query. By default terms are ANDed. To see what is possible, use the [advanced search page](http://www.indeed.com/advanced_search) to perform a search and then check the url for the q value.
->setFormat('json') // Format. Which output format of the API you wish to use. The options are "xml" and "json". If omitted or invalid, the json format is used.
->setCity('Chicago') // City.
->setState('IL') // State.
->setLocation('Chicago, IL') // Location. Use a postal code or a "city, state/province/region" combination. Will overwrite any changes made using setCity and setState
->setSort('date') // Sort by relevance or date. Default is relevance.
->setRadius('100') // Distance from search location ("as the crow flies"). Default is 25.
->setSiteType('jobsite') // Site type. To show only jobs from job boards use "jobsite". For jobs from direct employer websites use "employer".
->setJobType('fulltime') // Job type. Allowed values: "fulltime", "parttime", "contract", "internship", "temporary".
->setPage(2) // Start results at this result number, beginning with 0. Default is 0.
->setCount(200) // Maximum number of results returned per query. Default is 10
->setDaysBack(10) // Number of days back to search.
->filterDuplicates(false) // Filter duplicate results. 0 turns off duplicate job filtering. Default is 1.
->includeLatLong(true) // If latlong=1, returns latitude and longitude information for each job result. Default is 0.
->setCountry('us') // Search within country specified. Default is us.
->setChannel('channel-one') // Channel Name: Group API requests to a specific channel
->setUserIp($_SERVER['REMOTE_ADDR']) // The IP number of the end-user to whom the job results will be displayed.
->setUserAgent($_SERVER['HTTP_USER_AGENT']) // The User-Agent (browser) of the end-user to whom the job results will be displayed.
->getJobs();
```

Expand Down
192 changes: 151 additions & 41 deletions src/Indeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,58 @@
class Indeed extends AbstractProvider
{
/**
* Publisher Id
* Map of setter methods to query parameters
*
* @var string
*/
protected $publisherId;

/**
* Version
*
* @var string
*/
protected $version;

/**
* Highlight
*
* @var string
* @var array
*/
protected $highlight;
protected $queryMap = [
'setPublisher' => 'publisher',
'setVersion' => 'v',
'setFormat' => 'format',
'setKeyword' => 'q',
'setLocation' => 'l',
'setSort' => 'sort',
'setRadius' => 'radius',
'setSiteType' => 'st',
'setJobType' => 'jt',
'setPage' => 'start',
'setCount' => 'limit',
'setDaysBack' => 'fromage',
'setHighlight' => 'highlight',
'filterDuplicates' => 'filter',
'includeLatLong' => 'latlong',
'setCountry' => 'co',
'setChannel' => 'chnl',
'setUserIp' => 'userip',
'setUserAgent' => 'useragent',
];

/**
* Query params
*
* @var array
*/
protected $queryParams = [];
protected $queryParams = [
'publisher' => null,
'v' => '2',
'format' => null,
'q' => null,
'l' => null,
'sort' => null,
'radius' => null,
'st' => null,
'jt' => null,
'start' => null,
'limit' => null,
'fromage' => null,
'highlight' => null,
'filter' => null,
'latlong' => null,
'co' => null,
'chnl' => null,
'userip' => null,
'useragent' => null,
];

/**
* Job defaults
Expand All @@ -41,20 +67,57 @@ class Indeed extends AbstractProvider
'date','snippet','url','jobkey'
];


/**
* Add query params, if valid
* Create new indeed jobs client.
*
* @param string $value
* @param string $key
* @param array $parameters
*/
public function __construct($parameters = [])
{
parent::__construct($parameters);

$this->addDefaultUserInformationToParameters($parameters);

array_walk($parameters, [$this, 'updateQuery']);
}

/**
* Magic method to handle get and set methods for properties
*
* @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);
}

/**
* Attempts to apply default user information to parameters when none provided.
*
* @param array $parameters
*
* @return void
*/
protected function addDefaultUserInformationToParameters(&$parameters = [])
{
$defaultKeys = [
'userip' => 'REMOTE_ADDR',
'useragent' => 'HTTP_USER_AGENT',
];

array_walk($defaultKeys, function ($value, $key) use (&$parameters) {
if (!isset($parameters[$key]) && isset($_SERVER[$value])) {
$parameters[$key] = $_SERVER[$value];
}
});
}

/**
Expand Down Expand Up @@ -95,13 +158,35 @@ protected function createJobFromPayload($payload = [])
]);
}

/**
* Updates query params to include integer representation of boolean value
* to filter results for duplicates or not.
*
* @param mixed $value
*
* @return Indeed
*/
public function filterDuplicates($value)
{
$filter = (bool) $value ? '1' : null;

return $this->updateQuery($filter, 'filter');
}

/**
* Get data format
*
* @return string
*/
public function getFormat()
{
$validFormats = ['json', 'xml'];

if (isset($this->queryParams['format'])
&& in_array(strtolower($this->queryParams['format']), $validFormats)) {
return strtolower($this->queryParams['format']);
}

return 'json';
}

Expand All @@ -122,7 +207,9 @@ public function getListingsPath()
*/
public function getLocation()
{
$location = ($this->city ? $this->city.', ' : null).($this->state ?: null);
$locationArray = array_filter([$this->city, $this->state]);

$location = implode(', ', $locationArray);

if ($location) {
return $location;
Expand All @@ -138,18 +225,11 @@ public function getLocation()
*/
public function getQueryString()
{
$query_params = [
'publisher' => 'getPublisherId',
'v' => 'getVersion',
'highlight' => 'getHighlight',
'format' => 'getFormat',
'q' => 'getKeyword',
'l' => 'getLocation',
'start' => 'getPage',
'limit' => 'getCount',
];
$location = $this->getLocation();

array_walk($query_params, [$this, 'addToQueryStringIfValid']);
if (!empty($location)) {
$this->updateQuery($location, 'l');
}

return http_build_query($this->queryParams);
}
Expand All @@ -161,9 +241,7 @@ public function getQueryString()
*/
public function getUrl()
{
$query_string = $this->getQueryString();

return 'http://api.indeed.com/ads/apisearch?'.$query_string;
return 'http://api.indeed.com/ads/apisearch?'.$this->getQueryString();
}

/**
Expand All @@ -176,6 +254,21 @@ public function getVerb()
return 'GET';
}

/**
* Updates query params to include integer representation of boolean value
* to include lattitude and longitud in results.
*
* @param mixed $value
*
* @return Indeed
*/
public function includeLatLong($value)
{
$latlong = (bool) $value ? '1' : null;

return $this->updateQuery($latlong, 'latlong');
}

/**
* Attempt to parse and add location to Job
*
Expand All @@ -197,4 +290,21 @@ private function setJobLocation(Job $job, $location)

return $job;
}

/**
* Attempts to update current query parameters.
*
* @param string $value
* @param string $key
*
* @return Indeed
*/
protected function updateQuery($value, $key)
{
if (array_key_exists($key, $this->queryParams)) {
$this->queryParams[$key] = $value;
}

return $this;
}
}
Loading

0 comments on commit 6cbf0d6

Please sign in to comment.