-
Notifications
You must be signed in to change notification settings - Fork 5
/
Perlresume.pm
120 lines (94 loc) · 2.88 KB
/
Perlresume.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package Perlresume;
use Dancer ':syntax';
use Dancer::Plugin::Database;
use Perlresume::MetaCPAN;
use Perlresume::Kwalitee;
our $VERSION = '0.1';
my $mcpan = Perlresume::MetaCPAN->new;
my $kwalitee = Perlresume::Kwalitee->new(dbh => database('cpants'));
set 'warnings' => 0;
get '/' => sub {
if (my $author = params->{author}) {
return redirect '/' . $author;
}
my $authors = load_last_searches();
template 'index' => {authors => $authors};
};
get '/:author' => sub {
my $id = uc params->{author};
my $cpan_profile = $mcpan->fetch_author($id);
if (!$cpan_profile) {
status 'not_found';
return template 'not_found';
}
my $author = find_or_create($cpan_profile);
$author->{updated} = time;
$author->{views}++;
my $views = $author->{views};
update_author($author);
my $kwalitee_profile = $kwalitee->fetch_author($id);
template 'resume' => {
title => $cpan_profile->{asciiname}
? $cpan_profile->{asciiname}
: $cpan_profile->{name},
%$cpan_profile,
%$kwalitee_profile,
views => $author->{views}
};
};
true;
sub find_or_create {
my ($cpan_author) = @_;
if (my $author =
database('perlresume')->quick_select('resume', {pauseid => $cpan_author->{pauseid}}))
{
# TODO: remove me when everything is updated
if (!$author->{asciiname}) {
my $name =
$cpan_author->{name}
? $cpan_author->{name}
: $cpan_author->{asciiname};
my $asciiname =
$cpan_author->{asciiname}
? $cpan_author->{asciiname}
: $cpan_author->{name};
$author->{name} = $name;
$author->{asciiname} = $asciiname;
database('perlresume')
->quick_update( 'resume', { pauseid => $author->{pauseid} },
$author );
}
return $author;
}
my $name =
$cpan_author->{name} ? $cpan_author->{name} : $cpan_author->{asciiname};
my $asciiname =
$cpan_author->{asciiname}
? $cpan_author->{asciiname}
: $cpan_author->{name};
database('perlresume')->quick_insert(
'resume',
{
pauseid => $cpan_author->{pauseid},
asciiname => $asciiname,
name => $name,
updated => time
}
);
return {pauseid => $cpan_author->{pauseid}, views => 0};
}
sub update_author {
my ($author) = @_;
database('perlresume')->quick_update('resume', {pauseid => $author->{pauseid}},
$author);
}
sub load_last_searches {
my $sth = database('perlresume')->prepare(
'SELECT pauseid, name FROM resume ORDER BY updated DESC LIMIT 10',
);
$sth->execute;
my $authors =
[map { {pauseid => $_->[0], name => $_->[1]} }
@{$sth->fetchall_arrayref}];
return $authors;
}