Renaming to DoctorKarma.

This commit is contained in:
sergiotarxz 2022-02-06 18:07:40 +01:00
parent 7199804655
commit 1cc0432e97
7 changed files with 16 additions and 273 deletions

View File

@ -1,6 +1,6 @@
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'DoctorAgustin',
NAME => 'DoctorKarma',
VERSION => '0.1',
INST_SCRIPT => './bin',
INST_BIN => './bin',

View File

@ -8,14 +8,14 @@ use warnings;
use Data::Dumper;
use JSON;
use DoctorAgustin::Config;
use DoctorAgustin::Telegram;
use DoctorAgustin::Logger;
use DoctorKarma::Config;
use DoctorKarma::Telegram;
use DoctorKarma::Logger;
my $config = DoctorAgustin::Config->new;
my $logger = DoctorAgustin::Logger->new;
my $config = DoctorKarma::Config->new;
my $logger = DoctorKarma::Logger->new;
my $telegram =
DoctorAgustin::Telegram->new( telegram_token => $config->telegram_token );
DoctorKarma::Telegram->new( telegram_token => $config->telegram_token );
while (1) {
my $updates = $telegram->get_updates;
for my $update ($updates->@*) {

View File

@ -1,88 +0,0 @@
package DoctorAgustin::Config;
use v5.30.0;
use strict;
use warnings;
use Term::ReadLine;
use Const::Fast;
use JSON;
use Path::Tiny;
use DoctorAgustin::Logger;
sub HOME { $ENV{HOME} }
sub CONFIG_DIR { "@{[HOME]}/.config/doctoragustin" }
sub CONFIG_FILE { "@{[CONFIG_DIR]}/config.json" }
sub new {
my $class = shift;
my $self = bless {}, $class;
$self->_create_config_file_if_not_exists;
return $self;
}
sub logger {
my $self = shift;
if ( !defined $self->{logger} ) {
my $logger = DoctorAgustin::Logger->new;
$self->{logger} = $logger;
}
return $self->{logger};
}
sub _config {
my $self = shift;
if ( !defined $self->{config} ) {
if ( !-f CONFIG_FILE ) {
$self->logger->log_error(
qq(@{[CONFIG_FILE]} is not a plain file, unable to read config.)
);
die;
}
my $config = decode_json( path(CONFIG_FILE)->slurp_utf8 );
$self->{config} = $config;
}
return $self->{config};
}
sub telegram_token {
my $self = shift;
if ( !defined $self->{telegram_token} ) {
my $config = $self->_config;
$self->{telegram_token} = $config->{telegram_token};
}
return $self->{telegram_token};
}
sub _create_config_file_if_not_exists {
my $self = shift;
if ( !-e CONFIG_FILE ) {
$self->logger->log_info(q(Config file not detected));
$self->_create_config_file;
}
}
sub _create_config_file {
my $self = shift;
my $logger = $self->logger;
if ( !-e CONFIG_DIR ) {
$logger->log_info(qq(Creating @{[CONFIG_DIR]}));
eval { path(CONFIG_DIR)->mkpath; };
if ($@) {
$logger->log_error(
qq(Unable to create directory @{[CONFIG_DIR]}: $@));
die;
}
}
my $term = Term::ReadLine->new('Doctor Agustín');
my $token = $term->readline('Telegram token:');
my $config_contents = { telegram_token => $token };
$config_contents = encode_json($config_contents);
path(CONFIG_FILE)->spew_utf8($config_contents);
}
1;

View File

@ -1,39 +0,0 @@
package DoctorAgustin::Logger;
use Carp;
use Sys::Syslog;
sub new {
my $class = shift;
my $self = bless {}, $class;
return $self;
}
sub _log {
my $self = shift;
my $level = shift;
my $message = shift;
my $critical = shift;
openlog ('Doctor Agustín: ', $critical ? '': 'perror', 'user');
syslog ($level, $message);
closelog();
}
sub log_error {
my $self = shift;
$self->_log (LOG_ERR, shift);
}
sub log_critical {
my $self = shift;
my $error = shift;
$self->_log (LOG_ERR, $error, 1);
confess $error;
}
sub log_info {
my $self = shift;
$self->_log (LOG_INFO, shift);
}
1;

View File

@ -1,130 +0,0 @@
package DoctorAgustin::Telegram;
use v5.30.0;
use strict;
use warnings;
use Types::Standard qw/Str Int Ref/;
use Params::ValidationCompiler qw(validation_for);
use Mojo::UserAgent;
use JSON;
use DoctorAgustin::Logger;
{
my $validator = validation_for(
params => {
telegram_token => { type => Str },
}
);
sub new {
my $class = shift;
my $self = bless {}, $class;
my %params = $validator->(@_);
my $telegram_token = $params{telegram_token};
$self->{telegram_token} = $telegram_token;
return $self;
}
}
sub _logger {
my $self = shift;
if (!defined $self->{logger}) {
$self->{logger} = DoctorAgustin::Logger->new;
}
return $self->{logger};
}
sub _user_agent {
my $self = shift;
if ( !defined $self->{user_agent} ) {
$self->{user_agent} = Mojo::UserAgent->new;
}
return $self->{user_agent};
}
{
my $validator = validation_for(
params => {
method => { type => Str },
body => { type => Ref },
}
);
sub _request {
my $self = shift;
my %params = $validator->(@_);
my $method = $params{method};
my $body = $params{body};
my $ua = $self->_user_agent;
my $logger = $self->_logger;
my $url = $self->_generate_url( method => $method );
my $response = decode_json(
$ua->post( $url => {} => json => $body )->result->body );
unless ($response->{ok}) {
$logger->log_critical($response->{description});
}
return $response;
}
}
sub get_updates {
my $self = shift;
my $last_update = $self->_last_update;
if ( !defined $last_update ) {
$last_update = 0;
}
my $response = $self->_request(
method => q/getUpdates/,
body => { offset => $last_update + 1 }
);
if ( scalar $response->{result}->@* ) {
$last_update = $response->{result}[-1]{update_id};
$self->_set_last_update($last_update);
}
return $response->{result};
}
{
my $validator = validation_for(
params => {
method => { type => Str },
}
);
sub _generate_url {
my $self = shift;
my %params = $validator->(@_);
my $method = $params{method};
my $telegram_token = $self->_telegram_token;
my $url = qq(https://api.telegram.org/bot$telegram_token/$method);
return $url;
}
}
{
my $validator = validation_for( params => [ { type => Int } ] );
sub _set_last_update {
my $self = shift;
my ($last_update) = $validator->(@_);
$self->{last_update} = $last_update;
}
}
sub _last_update {
my $self = shift;
if ( !exists $self->{last_update} ) {
return undef;
}
return $self->{last_update};
}
sub _telegram_token {
my $self = shift;
return $self->{telegram_token};
}
1;

View File

@ -14,14 +14,14 @@ use JSON;
BEGIN {
unshift @INC, 'lib';
use_ok 'DoctorAgustin::Config';
use_ok 'DoctorKarma::Config';
}
{
## GIVEN
my $result = 'hola';
my $readline_mockmodule = Test::MockModule->new('Term::ReadLine');
my $logger_mockmodule = Test::MockModule->new('DoctorAgustin::Logger');
my $logger_mockmodule = Test::MockModule->new('DoctorKarma::Logger');
my $readline_mockobject = Test::MockObject->new;
my $new_home = Path::Tiny->tempdir;
@ -41,7 +41,7 @@ BEGIN {
## WHEN
lives_ok {
my $config = DoctorAgustin::Config->new;
my $config = DoctorKarma::Config->new;
my $telegram_token = $config->telegram_token;
## THEN
@ -55,10 +55,10 @@ BEGIN {
## GIVEN
my $result = 'hola';
my $readline_mockmodule = Test::MockModule->new('Term::ReadLine');
my $logger_mockmodule = Test::MockModule->new('DoctorAgustin::Logger');
my $logger_mockmodule = Test::MockModule->new('DoctorKarma::Logger');
my $readline_mockobject = Test::MockObject->new;
my $new_home = Path::Tiny->tempdir;
my $config_dir = qq($new_home/.config/doctoragustin);
my $config_dir = qq($new_home/.config/doctorkarma);
my $config_file = qq($config_dir/config.json);
local $ENV{HOME} = qq($new_home);
@ -71,7 +71,7 @@ BEGIN {
lives_ok {
## WHEN
my $config = DoctorAgustin::Config->new;
my $config = DoctorKarma::Config->new;
my $telegram_token = $config->telegram_token;
## THEN

View File

@ -11,12 +11,12 @@ use Test::MockObject;
BEGIN {
unshift @INC, 'lib';
use_ok 'DoctorAgustin::Telegram';
use_ok 'DoctorKarma::Telegram';
}
{
my $result = 'hola';
my $telegram = DoctorAgustin::Telegram->new (telegram_token => $result);
ok $telegram->isa('DoctorAgustin::Telegram'), 'Telegram object created successfully.';
my $telegram = DoctorKarma::Telegram->new (telegram_token => $result);
ok $telegram->isa('DoctorKarma::Telegram'), 'Telegram object created successfully.';
is $telegram->_telegram_token, $result, 'Telegram token setup was successful.';
}