| 1 | package Cikl::Smrt::Handlers::ConfigBasedHandler; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Encode qw/encode_utf8/; | ||
| 7 | use Cikl::Smrt::FeedParserConfig; | ||
| 8 | use Cikl::Smrt::Parsers; | ||
| 9 | use Cikl::Smrt::Decoders::AutoDecoder; | ||
| 10 | use Cikl::Smrt::Fetchers; | ||
| 11 | use Cikl::Smrt::HandlerRole; | ||
| 12 | use URI; | ||
| 13 | |||
| 14 | use Mouse; | ||
| 15 | with 'Cikl::Smrt::HandlerRole'; | ||
| 16 | |||
| 17 | sub name { | ||
| 18 | return 'config_based'; | ||
| 19 | }; | ||
| 20 | |||
| 21 | use namespace::autoclean; | ||
| 22 | |||
| 23 | has 'feedparser_config' => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'Cikl::Smrt::FeedParserConfig', | ||
| 26 | required => 1 | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub _build_detecttime_format { | ||
| 30 | my $self = shift; | ||
| 31 | return $self->feedparser_config->{detecttime_format}; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _build_detecttime_zone { | ||
| 35 | my $self = shift; | ||
| 36 | return $self->feedparser_config->{detecttime_zone}; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _refresh { | ||
| 40 | my $self = shift; | ||
| 41 | return $self->feedparser_config->{refresh}; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _default_event_data { | ||
| 45 | my $self = shift; | ||
| 46 | return $self->feedparser_config->default_event_data; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _fetcher { | ||
| 50 | my $self = shift; | ||
| 51 | my $fetchers = Cikl::Smrt::Fetchers->new(); | ||
| 52 | my $feedparser_config = $self->feedparser_config; | ||
| 53 | my $feedurl = URI->new($feedparser_config->feed()); | ||
| 54 | my $fetcher_class = $fetchers->lookup($feedurl); | ||
| 55 | if (!defined($fetcher_class)) { | ||
| 56 | die("Could not determine fetcher"); | ||
| 57 | } | ||
| 58 | |||
| 59 | my %args = (%$feedparser_config, proxy => $self->proxy(), feedurl => $feedurl); | ||
| 60 | |||
| 61 | return $fetcher_class->new(%args); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _parser { | ||
| 65 | my $self = shift; | ||
| 66 | my $parsers = Cikl::Smrt::Parsers->new(); | ||
| 67 | my $parser_name = $self->feedparser_config->parser; | ||
| 68 | my $parser_class = $parsers->get($parser_name); | ||
| 69 | if (!defined($parser_class)) { | ||
| 70 | die("Could not find a parser for parser=$parser_name. Valid parsers: " . $parsers->valid_parser_names_string); | ||
| 71 | } | ||
| 72 | my %args = (%{$self->feedparser_config}, config => $self->feedparser_config); | ||
| 73 | |||
| 74 | return $parser_class->new(%args); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _decoder { | ||
| 78 | my $self = shift; | ||
| 79 | my $feedparser_config = $self->feedparser_config; | ||
| 80 | my $feedurl = URI->new($feedparser_config->feed()); | ||
| 81 | my %decoder_args = (%$feedparser_config); | ||
| 82 | return Cikl::Smrt::Decoders::AutoDecoder->new( | ||
| 83 | feedurl => $feedurl, | ||
| 84 | decoder_args => \%decoder_args | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | |||
| 88 | __PACKAGE__->meta->make_immutable; | ||
| 89 | |||
| 90 | 1; | ||
| 91 |