File: 012-browser.psgi

package info (click to toggle)
libweb-machine-perl 0.17-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 940 kB
  • sloc: perl: 5,481; makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,054 bytes parent folder | download | duplicates (5)
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
#!perl

use strict;
use warnings;

use Web::Machine;

=pod

And of course, you don't have to just provide
text based results ...

=cut

{
    package YAPC::NA::2012::Example012::Resource;
    use strict;
    use warnings;
    use JSON::XS ();
    use GD::Simple;

    use parent 'Web::Machine::Resource';

    sub content_types_provided { [
        { 'image/gif' => 'to_gif'  },
        { 'text/html' => 'to_html' },
    ] }

    sub to_html {
        my $self = shift;
        '<html><body><ul>' .
            (join "" => map {
                '<li>' . $_->[0] . ' &mdash; ' . $_->[1]->type . '</li>'
            } $self->request->header('Accept')->iterable)
        . '</ul><br/><img src="/hello_world.gif" border="1"/></body></html>'
    }

    sub to_gif {
        my $self = shift;
        my $img  = GD::Simple->new( 130, 20 );
        $img->fgcolor('red');
        $img->moveTo(15, 15);
        $img->string( $self->request->path_info );
        $img->gif;
    }
}

Web::Machine->new( resource => 'YAPC::NA::2012::Example012::Resource' )->to_app;