Skip to content

Instantly share code, notes, and snippets.

@yappo
Created November 9, 2013 03:09
Show Gist options
  • Save yappo/7381056 to your computer and use it in GitHub Desktop.
Save yappo/7381056 to your computer and use it in GitHub Desktop.
# diff lib/Kossy.pm local/lib/perl5/Kossy.pm | pbcopy
21c21
< our $VERSION = '0.23';
---
> our $VERSION = '0.25';
43,46c43,53
< my $root_dir = shift;
< my @caller = caller;
< $root_dir ||= File::Basename::dirname( Cwd::realpath($caller[1]) );
< $self = $self->new($root_dir);
---
> my %args;
> if ( @_ < 2 ) {
> my $root_dir = shift;
> my @caller = caller;
> $root_dir ||= File::Basename::dirname( Cwd::realpath($caller[1]) );
> $args{root_dir} = $root_dir;
> }
> else {
> %args = @_;
> }
> $self = $self->new(%args);
81c88
< req => Kossy::Request->new($env),
---
> req => Kossy::Request->new($env, (parse_json_body => $self->{parse_json_body}) ),
389a397,454
> use Kossy::BodyParser;
> use Kossy::BodyParser::UrlEncoded;
> use Kossy::BodyParser::MultiPart;
> use Kossy::BodyParser::JSON;
>
> sub new {
> my($class, $env, %opts) = @_;
> Carp::croak(q{$env is required})
> unless defined $env && ref($env) eq 'HASH';
>
> bless {
> %opts,
> env => $env,
> }, $class;
> }
>
> sub request_body_parser {
> my $self = shift;
> unless (exists $self->{request_body_parser}) {
> $self->{request_body_parser} = $self->_build_request_body_parser();
> }
> return $self->{request_body_parser};
> }
>
> sub _build_request_body_parser {
> my $self = shift;
>
> my $parser = Kossy::BodyParser->new();
> $parser->register(
> 'application/x-www-form-urlencoded',
> 'Kossy::BodyParser::UrlEncoded'
> );
> $parser->register(
> 'multipart/form-data',
> 'Kossy::BodyParser::MultiPart'
> );
> if ( $self->{parse_json_body} ) {
> $parser->register(
> 'application/json',
> 'Kossy::BodyParser::JSON'
> );
> }
> $parser;
> }
>
> sub _parse_request_body {
> my $self = shift;
> $self->request_body_parser->parse($self->env);
> }
>
> sub uploads {
> my $self = shift;
> unless ($self->env->{'kossy.request.upload_parameters'}) {
> $self->_parse_request_body;
> }
> $self->env->{'plack.request.upload'} ||=
> Hash::MultiValue->new(@{$self->env->{'kossy.request.upload_parameters'}});
> }
393c458
< $self->{'kossy.body_parameters'} ||= $self->_decode_parameters($self->SUPER::body_parameters());
---
> $self->env->{'kossy.request.body'} ||= $self->_decode_parameters(@{$self->_body_parameters()});
398c463
< $self->{'kossy.query_parameters'} ||= $self->_decode_parameters($self->SUPER::query_parameters());
---
> $self->env->{'kossy.request.query'} ||= $self->_decode_parameters(@{$self->_query_parameters()});
401,402c466,474
< sub _decode_parameters {
< my ($self, $stuff) = @_;
---
> sub parameters {
> my $self = shift;
> $self->env->{'kossy.request.merged'} ||= do {
> Hash::MultiValue->new(
> $self->query_parameters->flatten,
> $self->body_parameters->flatten,
> );
> };
> }
404c476,477
< my @flatten = $stuff->flatten();
---
> sub _decode_parameters {
> my ($self, @flatten) = @_;
411c484,485
< sub parameters {
---
>
> sub _body_parameters {
413,417c487,499
< $self->env->{'kossy.request.merged'} ||= do {
< my $query = $self->query_parameters;
< my $body = $self->body_parameters;
< Hash::MultiValue->new( $query->flatten, $body->flatten );
< };
---
> unless ($self->env->{'kossy.request.body_parameters'}) {
> $self->_parse_request_body;
> }
> return $self->env->{'kossy.request.body_parameters'};
> }
>
> sub _query_parameters {
> my $self = shift;
> unless ( $self->env->{'kossy.request.query_parameter'} ) {
> $self->env->{'kossy.request.query_parameters'} =
> URL::Encode::url_params_flat($self->env->{'QUERY_STRING'});
> }
> return $self->env->{'kossy.request.query_parameters'};
421c503,507
< shift->SUPER::body_parameters();
---
> my $self = shift;
> unless ($self->env->{'plack.request.body'}) {
> $self->env->{'plack.request.body'} = Hash::MultiValue->new(@{$self->_body_parameters});
> }
> return $self->env->{'plack.request.body'};
422a509
>
424c511,515
< shift->SUPER::query_parameters();
---
> my $self = shift;
> unless ($self->env->{'plack.request.query'}) {
> $self->env->{'plack.request.query'} = Hash::MultiValue->new(@{$self->_query_parameters});
> }
> return $self->env->{'plack.request.query'};
430,432c521,524
< my $query = $self->SUPER::query_parameters();
< my $body = $self->SUPER::body_parameters();
< Hash::MultiValue->new( $query->flatten, $body->flatten );
---
> Hash::MultiValue->new(
> @{$self->_query_parameters},
> @{$self->_body_parameters}
> );
478a571
> use Cookie::Baker;
523c616
< my $cookie = $self->_bake_cookie($name, $val);
---
> my $cookie = bake_cookie($name, $val);
528c621
< # 'X-Frame-Options' => 'DENY',
---
> 'X-Frame-Options' => 'DENY',
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment