Browse Source

Merge branch 'dev'

Sniperovitch 8 years ago
parent
commit
a57b68111f
5 changed files with 69 additions and 13 deletions
  1. 1 0
      .travis.yml
  2. 4 2
      config.yml.smp
  3. 31 11
      lib/Bartender.pm
  4. 16 0
      t/002_index_route.t
  5. 17 0
      t/003_status_route.t

+ 1 - 0
.travis.yml

@@ -3,5 +3,6 @@ perl:
   - "5.24"
   - "5.24"
 before_install:
 before_install:
   - "cpanm Dancer2"
   - "cpanm Dancer2"
+  - "cp -v config.yml.smp config.yml"
 notifications:
 notifications:
   irc: "chat.freenode.net#pressecitron"
   irc: "chat.freenode.net#pressecitron"

+ 4 - 2
config.yml.smp

@@ -1,5 +1,7 @@
+appname: Bartender
+
 cocktail:
 cocktail:
-  binary: /home/bernard/cocktail/cocktail
-  store: /tmp/bernard
+  binary: /home/sniperovitch/cocktail/cocktail
+  store: /home/sniperovitch/store
 
 
 template: "simple"
 template: "simple"

+ 31 - 11
lib/Bartender.pm

@@ -4,6 +4,11 @@ use Data::Dumper;
 use POSIX ();
 use POSIX ();
 
 
 our $VERSION = '0.1';
 our $VERSION = '0.1';
+die "config.yml not loaded" unless defined config->{appname};
+
+get '/' => sub {
+    return "Welcome on " . config->{appname};
+};
 
 
 get '/shake' => sub {
 get '/shake' => sub {
     my %param = params;
     my %param = params;
@@ -50,7 +55,7 @@ get '/status' => sub {
     my $opt_projet  = $param{projetId};
     my $opt_projet  = $param{projetId};
 
 
     my $cocktail_store = config->{cocktail}{store};
     my $cocktail_store = config->{cocktail}{store};
-    if(not -d $cocktail_store) {
+    if(not defined $cocktail_store) {
         $error->{message} .= qq{"cocktail:store" n'est pas configuré dans config.yml};
         $error->{message} .= qq{"cocktail:store" n'est pas configuré dans config.yml};
     }
     }
 
 
@@ -62,23 +67,38 @@ get '/status' => sub {
         return template 'error', $error;
         return template 'error', $error;
     }
     }
 
 
-    my $compilation_status;
-    if(-e "$cocktail_store/$opt_dossier/$opt_projet.lock") {
-        $compilation_status = "En cours de compilation...";
+    my $lock_filename = "$cocktail_store/$opt_dossier/$opt_projet.lock";
+    my $pdf_filename  = "$cocktail_store/$opt_dossier/$opt_projet.pdf";
+
+    my ($compilation_status, $compilation_text);
+    if(-e $lock_filename) {
+        my $date_time = _get_datetime_for($lock_filename);
+        $compilation_text = "En cours de compilation depuis $date_time";
+        $compilation_status = 'IN PROGRESS';
     }
     }
-    elsif(-e "$cocktail_store/$opt_dossier/$opt_projet.pdf") {
-        my @stat = stat "$cocktail_store/$opt_dossier/$opt_projet.pdf";
-        my $ctime = $stat[10];
-        my $date_time = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($ctime) );
-        $compilation_status = $date_time;
+    elsif(-e $pdf_filename) {
+        my $date_time = _get_datetime_for($pdf_filename);
+        $compilation_text = $date_time;
+        $compilation_status = 'COMPLETED';
     }
     }
     else {
     else {
-        $compilation_status = "Aucune compilation.";
+        $compilation_text = "Aucune compilation.";
+        $compilation_status = 'NONE';
     }
     }
 
 
     header 'Content-Type' => 'application/json';
     header 'Content-Type' => 'application/json';
-    return to_json { text => $compilation_status };
+    return to_json { text => $compilation_text, status => $compilation_status };
 };
 };
 
 
+sub _get_datetime_for {
+    my $filename = shift;
+    return unless -e $filename;
+
+    my @stat = stat $filename;
+    my $ctime = $stat[10];
+    my $date_time = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($ctime) );
+    return $date_time;
+}
 
 
 true;
 true;
+

+ 16 - 0
t/002_index_route.t

@@ -0,0 +1,16 @@
+use strict;
+use warnings;
+use Data::Dumper;
+
+use Bartender;
+use Test::More tests => 3;
+use Plack::Test;
+use HTTP::Request::Common;
+
+my $app = Bartender->to_app;
+is( ref $app, 'CODE', 'Got app' );
+my $test = Plack::Test->create($app);
+my $res  = $test->request( GET '/' );
+ok( $res->is_success, '[GET /] successful' );
+is( $res->content, "Welcome on Bartender", '[GET / content] successful' );
+

+ 17 - 0
t/003_status_route.t

@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use Data::Dumper;
+
+use Bartender;
+use Test::More tests => 2;
+use Plack::Test;
+use HTTP::Request::Common;
+use JSON ();
+
+my $app = Bartender->to_app;
+my $test = Plack::Test->create($app);
+my $res  = $test->request( GET '/status?dossier=foo&projetId=bar' );
+ok( $res->is_success, '[GET /status] successful' );
+my $compilation = JSON::decode_json( $res->content );
+is( $compilation->{status}, 'NONE', '[GET /status NONE] successful' );
+