| 1 | package WebService::Buxfer::Response; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use JSON::XS (); | ||
| 5 | use Carp qw(carp); | ||
| 6 | |||
| 7 | has 'raw_response' => ( | ||
| 8 | is => 'ro', | ||
| 9 | isa => 'Object', | ||
| 10 | handles => [ qw( status_code status_message is_success is_error ) ] | ||
| 11 | ); | ||
| 12 | |||
| 13 | has 'content' => ( is => 'rw', isa => 'HashRef', lazy_build => 1 ); | ||
| 14 | |||
| 15 | sub BUILDARGS { | ||
| 16 | my ( $self, $res ) = @_; | ||
| 17 | return { raw_response => $res }; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub _build_content { | ||
| 21 | my $self = shift; | ||
| 22 | my $content = $self->raw_response->content; | ||
| 23 | return {} unless $content; | ||
| 24 | |||
| 25 | my $obj; | ||
| 26 | return JSON::XS::decode_json( $content ); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub ok { | ||
| 30 | my $self = shift; | ||
| 31 | my $status = $self->buxfer_status; | ||
| 32 | return defined $status && $status !~ /^ERROR:/ && !$self->raw_response->is_error; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub buxfer_status { | ||
| 36 | return shift->content->{response}->{status}; | ||
| 37 | } | ||
| 38 | |||
| 39 | no Moose; | ||
| 40 | |||
| 41 | __PACKAGE__->meta->make_immutable; | ||
| 42 | |||
| 43 | 1; | ||
| 44 | |||
| 45 | __END__ |