Skip to content

Commit

Permalink
Merge pull request #8 from jobapis/v0.3
Browse files Browse the repository at this point in the history
V0.3 release
  • Loading branch information
karllhughes committed Oct 7, 2016
2 parents c7d3103 + b7a1664 commit 2580382
Show file tree
Hide file tree
Showing 12 changed files with 511 additions and 133 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ SENDGRID_API_KEY=
CAREERBUILDER_KEY=
INDEED_KEY=
USAJOBS_KEY=
JUJU_KEY=
GA_TRACKING_ID=
67 changes: 54 additions & 13 deletions app/Jobs/CollectJobsForUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use JobApis\Jobs\Client\Collection;
use JobApis\Jobs\Client\JobsMulti;
use JobApis\JobsToMail\Models\User;
use JobApis\JobsToMail\Notifications\JobsCollected;
Expand All @@ -18,6 +19,21 @@ class CollectJobsForUser implements ShouldQueue
*/
protected $user;

/**
* The maximum number of jobs to return
*/
const MAX_JOBS = 50;

/**
* The maximum number of jobs from each provider
*/
const MAX_JOBS_FROM_PROVIDER = 10;

/**
* The maximum age of a job to be included
*/
const MAX_DAYS_OLD = 14;

/**
* Create a new job instance.
*/
Expand All @@ -38,11 +54,12 @@ public function handle(JobsMulti $jobsClient)
// Collect jobs based on the user's keyword and location
$jobsByProvider = $jobsClient->setKeyword($this->user->keyword)
->setLocation($this->user->location)
->setPage(1, 10)
->setPage(1, self::MAX_JOBS_FROM_PROVIDER)
->getAllJobs();

// Sort jobs into one array
$jobs = $this->sortJobs($jobsByProvider);
$jobs = $this->getJobsFromCollections($jobsByProvider);
$jobs = $this->sortJobs($jobs);

// Trigger notification to user
if ($jobs) {
Expand All @@ -54,25 +71,49 @@ public function handle(JobsMulti $jobsClient)
}

/**
* Sort the array of collections returned by JobsMulti and return an array of jobs.
* Sort jobs by date posted, desc
*
* @param array $collectionsArray
* @param array $jobs
*
* @return array
*/
protected function sortJobs($collectionsArray = [])
protected function sortJobs($jobs = [])
{
$jobs = [];
// Convert the array of collections to one large array
foreach ($collectionsArray as $collection) {
foreach (array_slice($collection->all(), 0, 10) as $jobListing) {
$jobs[] = $jobListing;
}
}
// Order by date posted, desc
// Sort by date
usort($jobs, function ($item1, $item2) {
return $item2->datePosted <=> $item1->datePosted;
});
// Filter any older than max age
$jobs = array_filter($jobs, function ($job) {
return $job->datePosted > new \DateTime(self::MAX_DAYS_OLD.' days ago');
});
// Truncate to the max number of results
return array_slice($jobs, 0, self::MAX_JOBS);
}

/**
* Convert the array of collections to one large array
*
* @param array $collectionsArray
*
* @return array
*/
protected function getJobsFromCollections($collectionsArray = [])
{
$jobs = [];
array_walk_recursive(
$collectionsArray,
function (Collection $collection) use (&$jobs) {
$jobListings = array_slice(
$collection->all(),
0,
self::MAX_JOBS_FROM_PROVIDER
);
foreach ($jobListings as $jobListing) {
$jobs[] = $jobListing;
}
}
);
return $jobs;
}
}
34 changes: 27 additions & 7 deletions app/Notifications/Messages/JobMailMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ class JobMailMessage extends MailMessage
public function listing(Job $job)
{
$line = "";
$line .= $job->getTitle();
if ($job->getCompanyName()) {
$line .= " at {$job->getCompanyName()}";
}
if ($job->getLocation()) {
$line .= " in {$job->getLocation()}";
}
$line .= $this->getTitle($job->getTitle());
$line .= $this->getCompany($job->getCompanyName());
$line .= $this->getLocation($job->getLocation());
$line .= ".";
$this->jobListings[] = [
'link' => $job->getUrl(),
'text' => $line,
'date' => $this->getDate($job->getDatePosted()),
];
return $this;
}
Expand All @@ -45,4 +42,27 @@ public function data()
['jobListings' => $this->jobListings]
);
}

private function getTitle($title)
{
return $title ?: null;
}

private function getLocation($location)
{
return $location ? " in {$location}" : null;
}

private function getCompany($company)
{
return $company ? " at {$company}" : null;
}

private function getDate($dateTime)
{
if (is_object($dateTime) && \DateTime::class == get_class($dateTime)) {
return $dateTime->format('F j, Y');
}
return null;
}
}
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ All Notable changes to `jobs-to-mail` will be documented in this file.

## 0.3.0 - 2016-10-06

### Added
- Date posted to email when valid DateTime object is included in result.
- Support for [Juju](https://github.com/jobapis/jobs-juju) job board.
- Improvements to date sorting in email:
- Setting max age for results
- Fixing date comparison by ensuring all results use DateTime
- Setting max results

## Fixed
- Queue worker on Heroku doesn't work with timeout. Adjusting procfile appropriately.
- Command now queues jobs asynchronously.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"php": ">=7.0.0",
"laravel/framework": "5.3.*",
"s-ichikawa/laravel-sendgrid-driver": "^1.1",
"jobapis/jobs-multi": "^0.3.1"
"jobapis/jobs-multi": "^0.4.1"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
Loading

0 comments on commit 2580382

Please sign in to comment.