| 1 | package HTTP::Throwable::Role::JSONBody; | ||
| 2 | # ABSTRACT - JSON Body | ||
| 3 | 1 | $HTTP::Throwable::Role::JSONBody::VERSION = '0.001'; | |
| 4 | 1 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use JSON::MaybeXS; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | has payload => ( | |
| 8 | is => 'ro', | ||
| 9 | required => 1, | ||
| 10 | ); | ||
| 11 | |||
| 12 | sub body { | ||
| 13 | 1 | ▶ | my $self = shift; |
|
Subroutine
body: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 14 | |||
| 15 | 1 | return encode_json($self->payload); | |
| 16 | } | ||
| 17 | |||
| 18 | sub body_headers { | ||
| 19 | 1 | ▶ | my ($self, $body) = @_; |
|
Subroutine
body_headers: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 20 | |||
| 21 | return [ | ||
| 22 | 1 | 'Content-Type' => 'application/json', | |
| 23 | 'Content-Length' => length $body, | ||
| 24 | ]; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub as_string { | ||
| 28 | 0 | ▶ | my $self = shift; |
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||
| 29 | |||
| 30 | 0 | return $self->body; | |
| 31 | } | ||
| 32 | |||
| 33 | 1 | ▶ | no Moo::Role; 1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | |||
| 35 | =pod | ||
| 36 | |||
| 37 | =encoding UTF-8 | ||
| 38 | |||
| 39 | =head1 NAME | ||
| 40 | |||
| 41 | HTTP::Throwable::Role::JSONBody | ||
| 42 | |||
| 43 | =head1 VERSION | ||
| 44 | |||
| 45 | version 0.001 | ||
| 46 | |||
| 47 | =head1 OVERVIEW | ||
| 48 | |||
| 49 | This role does two things - accepts a C<payload> argument that should be | ||
| 50 | anything you can pass to L<JSON/"encode_json">, and then encodes it as the | ||
| 51 | body, specifying a C<Content-Type> of C<application/json>. | ||
| 52 | |||
| 53 | =head1 AUTHOR | ||
| 54 | |||
| 55 | Matthew Horsfall <wolfsage@gmail.com> | ||
| 56 | |||
| 57 | =head1 COPYRIGHT AND LICENSE | ||
| 58 | |||
| 59 | This software is copyright (c) 2016 by Matthew Horsfall. | ||
| 60 | |||
| 61 | This is free software; you can redistribute it and/or modify it under | ||
| 62 | the same terms as the Perl 5 programming language system itself. | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | __END__ | ||