Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

day 17

  • Loading branch information...
commit edfa557927aef21d5dc119a47428f38a9f69a3df 1 parent 188e06b
@typester authored
View
2  config.pl
@@ -8,7 +8,7 @@
database => [
'dbi:SQLite:' . $home->file('database.db'), '', '',
{
- unicode => 1,
+# unicode => 1,
sqlite_unicode => 1,
},
],
View
55 lib/Jobeet/Controller/API.pm
@@ -0,0 +1,55 @@
+package Jobeet::Controller::API;
+use Ark 'Controller';
+
+use Jobeet::Models;
+
+sub token :Chained('/') :PathPart('api') :CaptureArgs(1) {
+ my ($self, $c, $token) = @_;
+
+ $c->detach('/default') unless length $token == 40;
+
+ my $affiliate = models('Schema::Affiliate')->single({ token => $token })
+ or $c->detach('/default');
+
+ $c->stash->{affiliate} = $affiliate;
+}
+
+sub jobs :Chained('token') :Args(0) {
+ my ($self, $c) = @_;
+
+ my $json = [];
+ my $affiliate = $c->stash->{affiliate};
+ my $max_rows = models('conf')->{max_jobs_on_homepage};
+
+ for my $category ($affiliate->categories) {
+ for my $job ($category->get_active_jobs({ rows => $max_rows })->all) {
+ push @$json, {
+ category => $category->name,
+ type => $job->type,
+ company => $job->company,
+ url => $job->url,
+ position => $job->position,
+ location => $job->location,
+ description => $job->description,
+ how_to_apply => $job->how_to_apply,
+ expires_at => $job->expires_at->epoch,
+ };
+ }
+ }
+
+ $c->stash->{json} = $json;
+}
+
+sub end :Private {
+ my ($self, $c) = @_;
+
+ if ($c->stash->{json}) {
+ $c->forward( $c->view('JSON') );
+ }
+ else {
+ $c->forward('/end');
+ }
+}
+
+1;
+
View
12 lib/Jobeet/Schema/Result/Affiliate.pm
@@ -3,6 +3,9 @@ use strict;
use warnings;
use base 'Jobeet::Schema::ResultBase';
+use Digest::SHA1 qw/sha1_hex/;
+use Data::UUID;
+
__PACKAGE__->table('jobeet_affiliate');
__PACKAGE__->add_columns(
@@ -49,5 +52,14 @@ __PACKAGE__->has_many(
__PACKAGE__->many_to_many( categories => category_affiliate => 'category' );
+sub insert {
+ my $self = shift;
+
+ $self->token(sha1_hex(Data::UUID->new->create))
+ unless $self->token;
+
+ $self->next::method(@_);
+}
+
1;
View
9 lib/Jobeet/View/JSON.pm
@@ -0,0 +1,9 @@
+package Jobeet::View::JSON;
+use Ark 'View::JSON';
+
+has '+expose_stash' => (
+ default => 'json',
+);
+
+1;
+
View
26 sql/fixtures/default.pl
@@ -84,4 +84,30 @@
}
}
+{
+ my $affiliate = models('Schema::Affiliate')->create({
+ url => 'http://www.sensio-labs.com/',
+ email => 'fabien.potencier@example.com',
+ is_active => 1,
+ });
+ $affiliate->add_to_category_affiliate({
+ category_id => models('Schema::Category')->find({ name => 'Programming' })->id,
+ });
+}
+
+{
+ my $affiliate = models('Schema::Affiliate')->create({
+ url => 'http://www.symfony-project.org/',
+ email => 'fabien.potencier@example.org',
+ is_active => 1,
+ });
+ $affiliate->add_to_category_affiliate({
+ category_id => models('Schema::Category')->find({ name => 'Design' })->id,
+ });
+ $affiliate->add_to_category_affiliate({
+ category_id => models('Schema::Category')->find({ name => 'Programming' })->id,
+ });
+}
+
1;
+
View
28 t/controller/api.t
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Jobeet::Test;
+use Jobeet::Models;
+use JSON;
+
+my $datafile = models('home')->subdir(qw/sql fixtures/)->file('default.pl');
+do $datafile or die $!;
+
+{
+ my $res = request( GET => '/api/foo/jobs' );
+ is $res->code, 404, '404 ok';
+}
+
+{
+ my $data = models('Schema::Affiliate')->single({});
+
+ my $res = request( GET => '/api/' . $data->token . '/jobs' );
+ is $res->code, 200, '200 ok';
+ like $res->content_type, qr!application/json!, 'content_type ok';
+
+ my $json = from_json($res->content);
+ is $json->[0]{category}, $data->categories->first->name, 'category ok';
+}
+
+done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.