biblesearch / lib / Biblesearch.pm /
Newer Older
95 lines | 2.223kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Biblesearch;
2

            
3
our $VERSION = '0.01';
4

            
5
use Mojo::Base 'Mojolicious';
6
use DBIx::Custom;
聖書の部分は静的に配信できるようにした。
Yuki Kimoto authored on 2014-03-29
7
use File::Path 'mkpath';
8
use Mojo::JSON;
9
use Mojo::Util 'spurt';
add files
Yuki Kimoto authored on 2014-03-26
10

            
11
has 'dbi';
12

            
13
sub startup {
14
  my $self = shift;
15
  
16
  # DBI
17
  my $db_file = $self->home->rel_file('db/bible.db');
18
  my $dbi = DBIx::Custom->connect(
19
    dsn =>  "dbi:SQLite:dbname=$db_file",
20
    option => {sqlite_unicode => 1},
21
    connector => 1
22
  );
23
  $self->dbi($dbi);
24
  
25
  # Models
26
  my $models = [
27
    {
28
      table => 'book',
29
      primary_key => 'id'
30
    },
31
    {
32
      table => 'section',
33
      primary_key => [qw/book_id chapter section/]
34
    }
35
  ];
36
  $dbi->create_model($_) for @$models;
37
  
38
  # Route
39
  my $r = $self->routes;
40
  $r->get('/')->to(template => 'index');
聖書の部分は静的に配信できるようにした。
Yuki Kimoto authored on 2014-03-29
41
  $r->get('/api/book/:id/content.json')->to(cb => sub {
文字列の置換をjavascript側で行うようにした。
Yuki Kimoto authored on 2014-03-29
42
    my $self = shift;
43
    my $id = $self->param('id');
44
    
45
    my $dbi = $self->app->dbi;
46
    my $content = $dbi->model('book')->select('content', id => $id)->value;
47
    
聖書の部分は静的に配信できるようにした。
Yuki Kimoto authored on 2014-03-29
48
    if ($content) {
49
      my $dir = $self->app->home->rel_file("public/api/book/$id");
50
      mkpath $dir;
51
      
52
      my $data = {
53
        content => $content
54
      };
55
      
56
      my $json = Mojo::JSON->new;
57
      my $data_json = $json->encode($data);
58
      spurt $data_json, $self->app->home->rel_file("public/api/book/$id/content.json");
59
      
60
      $self->render_static("/api/book/$id/content.json");
61
    }
62
    else {
63
      $self->render_not_found;
64
    }
文字列の置換をjavascript側で行うようにした。
Yuki Kimoto authored on 2014-03-29
65
  });
回数を取得するAPIを追加
Yuki Kimoto authored on 2014-03-29
66
  
67
  $r->get('/api/word-count/:word')->to(cb => sub {
68
    my $self = shift;
69
    my $word = $self->param('word');
70
    my $dbi = $self->app->dbi;
71
    
72
    my $word_count_h = {};
73
    for (my $i = 0; $i < 66; $i++) {
74
      my $num = sprintf "%02d", $i + 1;
75
      
76
      my $content = $dbi->select(
77
        'content_no_tag',
78
        table => 'book',
79
        where => {id => $num}
80
      )->value;
81
      my $content_length = length $content;
82
      my $word_q = quotemeta($word);
83
      $content =~ s/$word_q//g;
84
      my $content_length_no_word = length $content;
85
      
86
      # 文字の個数
87
      my $word_count = ($content_length - $content_length_no_word) / length $word;
88
      $word_count_h->{$num} = $word_count;
89
    }
90
    
91
    $self->render(json => {word_count => $word_count_h});
92
  });
add files
Yuki Kimoto authored on 2014-03-26
93
}
94

            
95
1;