| 1 | package WebService::Hexonet::Connector::ResponseTemplateManager; | ||
| 2 | |||
| 3 | use 5.030; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use WebService::Hexonet::Connector::ResponseTemplate; | ||
| 7 | use WebService::Hexonet::Connector::ResponseParser; | ||
| 8 | |||
| 9 | our $VERSION = 'v2.10.4'; | ||
| 10 | |||
| 11 | my $instance = undef; | ||
| 12 | |||
| 13 | |||
| 14 | sub getInstance { | ||
| 15 | if ( !defined $instance ) { | ||
| 16 | my $self = { templates => {} }; | ||
| 17 | $instance = bless $self, shift; | ||
| 18 | $instance->addTemplate( '404', $instance->generateTemplate( '421', 'Page not found' ) ); | ||
| 19 | $instance->addTemplate( '500', $instance->generateTemplate( '500', 'Internal server error' ) ); | ||
| 20 | $instance->addTemplate( 'empty', $instance->generateTemplate( '423', 'Empty API response. Probably unreachable API end point {CONNECTION_URL}' ) ); | ||
| 21 | $instance->addTemplate( 'error', $instance->generateTemplate( '421', 'Command failed due to server error. Client should try again' ) ); | ||
| 22 | $instance->addTemplate( 'expired', $instance->generateTemplate( '530', 'SESSION NOT FOUND' ) ); | ||
| 23 | $instance->addTemplate( 'httperror', $instance->generateTemplate( '421', 'Command failed due to HTTP communication error' ) ); | ||
| 24 | $instance->addTemplate( 'invalid', $instance->generateTemplate( '423', 'Invalid API response. Contact Support' ) ); | ||
| 25 | $instance->addTemplate( 'unauthorized', $instance->generateTemplate( '530', 'Unauthorized' ) ); | ||
| 26 | } | ||
| 27 | return $instance; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | sub generateTemplate { | ||
| 32 | my ( $self, $code, $description ) = @_; | ||
| 33 | return "[RESPONSE]\r\nCODE=${code}\r\nDESCRIPTION=${description}\r\nEOF\r\n"; | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | sub addTemplate { | ||
| 38 | my ( $self, $id, $plain ) = @_; | ||
| 39 | $self->{templates}->{$id} = $plain; | ||
| 40 | return $instance; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub getTemplate { | ||
| 45 | my ( $self, $id ) = @_; | ||
| 46 | my $plain; | ||
| 47 | if ( $self->hasTemplate($id) ) { | ||
| 48 | $plain = $self->{templates}->{$id}; | ||
| 49 | } else { | ||
| 50 | $plain = $self->generateTemplate( '500', 'Response Template not found' ); | ||
| 51 | } | ||
| 52 | return WebService::Hexonet::Connector::ResponseTemplate->new($plain); | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub getTemplates { | ||
| 57 | my $self = shift; | ||
| 58 | my $tmp = {}; | ||
| 59 | my $tpls = $self->{templates}; | ||
| 60 | foreach my $key ( keys %{$tpls} ) { | ||
| 61 | $tmp->{$key} = WebService::Hexonet::Connector::ResponseTemplate->new( $tpls->{$key} ); | ||
| 62 | } | ||
| 63 | return $tmp; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub hasTemplate { | ||
| 68 | my ( $self, $id ) = @_; | ||
| 69 | return defined $self->{templates}->{$id}; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub isTemplateMatchHash { | ||
| 74 | my ( $self, $tpl2, $id ) = @_; | ||
| 75 | my $tpl = $self->getTemplate($id); | ||
| 76 | my $h = $tpl->getHash(); | ||
| 77 | return ( $h->{CODE} eq $tpl2->{CODE} ) && ( $h->{DESCRIPTION} eq $tpl2->{DESCRIPTION} ); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub isTemplateMatchPlain { | ||
| 82 | my ( $self, $plain, $id ) = @_; | ||
| 83 | my $h = WebService::Hexonet::Connector::ResponseParser::parse($plain); | ||
| 84 | return $self->isTemplateMatchHash( $h, $id ); | ||
| 85 | } | ||
| 86 | |||
| 87 | 1; | ||
| 88 | |||
| 89 | __END__ |