| 1 | package Plack::App::GraphEasy; | ||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use parent qw/Plack::Component/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use Graph::Easy; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use Graph::Easy::Parser; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 2 | ▶ | use Plack::Request; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 2 | ▶ | use HTTP::Status qw//; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use Plack::Util::Accessor qw/ | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | stderr | ||||||||||||||
| 11 | timeout | ||||||||||||||
| 12 | 2 | /; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | our $VERSION = '0.01'; | |||||||||||||
| 15 | |||||||||||||||
| 16 | 2 | our $FORM_HTML = <<'_HTML_'; | |||||||||||||
| 17 | <html> | ||||||||||||||
| 18 | <head> | ||||||||||||||
| 19 | <meta charset="utf-8"> | ||||||||||||||
| 20 | <title>GraphEasy</title> | ||||||||||||||
| 21 | </head> | ||||||||||||||
| 22 | <body> | ||||||||||||||
| 23 | Manual: <a href="http://bloodgate.com/perl/graph/manual/" target="_blank">http://bloodgate.com/perl/graph/manual/</a> | ||||||||||||||
| 24 | <br> | ||||||||||||||
| 25 | <textarea name="txt" id="txt" style="width:80%%;height:70px;"></textarea> | ||||||||||||||
| 26 | <br> | ||||||||||||||
| 27 | <input type="button" name="send" value="Send" id="btn"> <input type="reset" value="reset" id="reset_btn"> | ||||||||||||||
| 28 | <div id="result"> | ||||||||||||||
| 29 | <pre id="graph" style="font-family:"monospace";"></pre> | ||||||||||||||
| 30 | </div> | ||||||||||||||
| 31 | <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | ||||||||||||||
| 32 | <script> | ||||||||||||||
| 33 | $(document).ready(function(){ | ||||||||||||||
| 34 | $("#btn").click(function(){ | ||||||||||||||
| 35 | var text = $('#txt').val(); | ||||||||||||||
| 36 | $.ajax({ | ||||||||||||||
| 37 | type: 'post', | ||||||||||||||
| 38 | url: '%s', | ||||||||||||||
| 39 | data: { | ||||||||||||||
| 40 | 'text': text | ||||||||||||||
| 41 | }, | ||||||||||||||
| 42 | cache: false, | ||||||||||||||
| 43 | async: false, | ||||||||||||||
| 44 | dataType: 'html', | ||||||||||||||
| 45 | timeout: 10000, | ||||||||||||||
| 46 | success: function(data) { | ||||||||||||||
| 47 | $('#graph').text(data); | ||||||||||||||
| 48 | }, | ||||||||||||||
| 49 | error : function(xhr, text, error) { | ||||||||||||||
| 50 | $('#graph').text(xhr.status+': '+text); | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | }); | ||||||||||||||
| 53 | }); | ||||||||||||||
| 54 | $("#reset_btn").click(function(){ | ||||||||||||||
| 55 | $('#txt').val(''); | ||||||||||||||
| 56 | $('#graph').text(''); | ||||||||||||||
| 57 | }); | ||||||||||||||
| 58 | }); | ||||||||||||||
| 59 | </script> | ||||||||||||||
| 60 | </body> | ||||||||||||||
| 61 | </html> | ||||||||||||||
| 62 | _HTML_ | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 5 | ▶ | sub call { | ||||||||||||
|
Subroutine
call: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||
| 65 | 5 | my ($self, $env) = @_; | |||||||||||||
| 66 | |||||||||||||||
| 67 | 5 | my $req = Plack::Request->new($env); | |||||||||||||
| 68 | 5 | my $method = $req->method; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 5 | ▶ | if ($method eq 'GET') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 71 | 1 | return $self->_put_form($req); | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | elsif ($method eq 'POST') { | ||||||||||||||
| 74 | 4 | return $self->_put_graph($req); | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | return $self->_return_status(404); | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 1 | ▶ | sub _put_form { | ||||||||||||
|
Subroutine
_put_form: called CC=1 SCAR=0.0
| |||||||||||||||
| 81 | 1 | my ($self, $req) = @_; | |||||||||||||
| 82 | |||||||||||||||
| 83 | 1 | my $content = sprintf( | |||||||||||||
| 84 | $FORM_HTML, | ||||||||||||||
| 85 | $req->path, | ||||||||||||||
| 86 | ); | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 1 | return $self->_return_success($content, 'text/html; charset=UTF-8'); | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 4 | ▶ | sub _put_graph { | ||||||||||||
|
Subroutine
_put_graph: called CC=8 SCAR=24.2
| |||||||||||||||
| 92 | 4 | my ($self, $req) = @_; | |||||||||||||
| 93 | |||||||||||||||
| 94 | 4 | ▶ | my $input_text = $req->param('text') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | or return $self->_return_success('no text'); | ||||||||||||||
| 96 | 3 | $input_text =~ s/^[\s\t\r\n]+//g; | |||||||||||||
| 97 | 3 | $input_text =~ s/[\s\t\r\n]+$//g; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 3 | ▶ | if ($input_text !~ m![\]\}]$!) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 0 | return $self->_return_success('wrong text'); | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 3 | my $parser = Graph::Easy::Parser->new; | |||||||||||||
| 104 | 3 | my $graph = $parser->from_text($input_text); | |||||||||||||
| 105 | 3 | ▶ | if (!$graph) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | return $self->_return_status(500, "Error: something wrong"); | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | 3 | ▶ | if ($parser->error) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | 0 | return $self->_return_success("Error: ". $parser->error); | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 3 | ▶ | $graph->timeout($self->timeout || 10); | ||||||||||||
|
Condition50%1/2
$self->timeout || 10
MC/DC0%0/1
$self->timeout || 10
$self->timeout
Truth table50%1/2
A
A= $self->timeout
| |||||||||||||||
| 113 | |||||||||||||||
| 114 | 3 | my $result; | |||||||||||||
| 115 | 3 | eval { | |||||||||||||
| 116 | 3 | $result = $graph->as_ascii; | |||||||||||||
| 117 | }; | ||||||||||||||
| 118 | 3 | ▶ | if (my $e = $@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | 0 | ▶ | if ($e =~ m!layout did not finish in time!) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 0 | return $self->_return_success("timeout"); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | 0 | return $self->_return_status(500, "Error: $e"); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 3 | return $self->_return_success($result); | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 5 | ▶ | sub _return_success { | ||||||||||||
|
Subroutine
_return_success: called CC=2 SCAR=6.9
| |||||||||||||||
| 129 | 5 | my ($self, $msg, $content_type) = @_; | |||||||||||||
| 130 | |||||||||||||||
| 131 | return [ | ||||||||||||||
| 132 | 5 | ▶ | 200, | ||||||||||||
|
Condition100%2/2
$content_type || 'text/plain'
MC/DC100%1/1
$content_type || 'text/plain'
$content_type
Truth table100%2/2
A
A= $content_type
| |||||||||||||||
| 133 | [ | ||||||||||||||
| 134 | 'Content-Type' => $content_type || 'text/plain', | ||||||||||||||
| 135 | 'Content-Length' => length $msg | ||||||||||||||
| 136 | ], | ||||||||||||||
| 137 | [$msg] | ||||||||||||||
| 138 | ]; | ||||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 0 | ▶ | sub _return_status { | ||||||||||||
|
Subroutine
_return_status: not called CC=4 SCAR=30.0
| |||||||||||||||
| 142 | 0 | my $self = shift; | |||||||||||||
| 143 | 0 | ▶ | my $status_code = shift || 500; | ||||||||||||
|
Condition0%0/2
shift() || 500
MC/DC0%0/1
shift() || 500
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 144 | 0 | ▶ | my $err = shift || ''; | ||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||
| 145 | |||||||||||||||
| 146 | 0 | my $msg = HTTP::Status::status_message($status_code); | |||||||||||||
| 147 | |||||||||||||||
| 148 | 0 | ▶ | if ($self->stderr) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 149 | 0 | print STDERR "$msg\n$err\n"; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | return [ | ||||||||||||||
| 153 | 0 | $status_code, | |||||||||||||
| 154 | [ | ||||||||||||||
| 155 | 'Content-Type' => 'text/plain', | ||||||||||||||
| 156 | 'Content-Length' => length $msg | ||||||||||||||
| 157 | ], | ||||||||||||||
| 158 | [$msg] | ||||||||||||||
| 159 | ]; | ||||||||||||||
| 160 | } | ||||||||||||||
| 161 | |||||||||||||||
| 162 | 2 | 1; | |||||||||||||
| 163 | |||||||||||||||
| 164 | __END__ | ||||||||||||||