Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
Loose dependency on Moo and use Mojo::Base
Browse files Browse the repository at this point in the history
  • Loading branch information
mdom committed Feb 26, 2016
1 parent 3768c93 commit 3860aa2
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 105 deletions.
1 change: 0 additions & 1 deletion META.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"Mojo::ByteStream" : "0",
"Mojo::JSON" : "0",
"Mojo::UserAgent" : "0",
"Moo" : "0",
"OptArgs" : "0",
"POSIX" : "0",
"Path::Tiny" : "0",
Expand Down
2 changes: 0 additions & 2 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ my %WriteMakefileArgs = (
"Mojo::ByteStream" => 0,
"Mojo::JSON" => 0,
"Mojo::UserAgent" => 0,
"Moo" => 0,
"OptArgs" => 0,
"POSIX" => 0,
"Path::Tiny" => 0,
Expand Down Expand Up @@ -61,7 +60,6 @@ my %FallbackPrereqs = (
"Mojo::UserAgent" => 0,
"Mojo::UserAgent::Server" => 0,
"Mojolicious::Lite" => 0,
"Moo" => 0,
"OptArgs" => 0,
"POSIX" => 0,
"Path::Tiny" => 0,
Expand Down
1 change: 0 additions & 1 deletion dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,3 @@ allow_dirty = Changes
[ Git::Tag ]

[ AutoPrereqs ]
[ Test::Perl::Critic ]
67 changes: 32 additions & 35 deletions lib/App/txtnix.pm
Original file line number Diff line number Diff line change
@@ -1,56 +1,51 @@
package App::txtnix;

use strict;
use warnings;
use Mojo::Base -base;
use 5.14.0;
use Config::Tiny;
use Path::Tiny;
use HTTP::Date;
use Mojo::UserAgent;
use Moo;
use App::txtnix::Tweet;
use App::txtnix::Cache;
use IO::Pager;
use Mojo::ByteStream 'b';

our $VERSION = '0.01';

has ua => ( is => 'lazy' );
has cache => ( is => 'ro', default => sub { App::txtnix::Cache->new() } );

has twtfile => (
is => 'rw',
default => sub { path('~/twtxt') },
coerce => sub { ref $_[0] ? $_[0] : path( $_[0] ) }
);

has pager => ( is => 'rw', default => sub { 1 } );
has sorting => ( is => 'rw', default => sub { "descending" } );
has timeout => ( is => 'rw', default => sub { 5 } );
has use_cache => ( is => 'rw', default => sub { 1 } );
has limit => ( is => 'rw', default => sub { 20 } );
has time_format => ( is => 'rw', default => sub { '%F %H:%M' } );
has disclose_identity => ( is => 'rw', default => sub { 0 } );
has rewrite_urls => ( is => 'rw', default => sub { 1 } );
has embed_names => ( is => 'rw', default => sub { 1 } );
has check_following => ( is => 'rw', default => sub { 1 } );
has following => ( is => 'rw', default => sub { {} } );
has nick => ( is => 'rw', default => sub { $ENV{USER} } );
has twturl => ( is => 'rw' );
has pre_tweet_hook => ( is => 'rw' );
has post_tweet_hook => ( is => 'rw' );
has config => ( is => 'rw' );
has force => ( is => 'rw' );
has since => ( is => 'rw', default => sub { 0 }, coerce => \&to_epoch );
has until => ( is => 'rw', default => sub { time }, coerce => \&to_epoch );

sub BUILDARGS {
has 'ua' => sub { shift->_build_ua };
has cache => sub { App::txtnix::Cache->new() };

has twtfile => sub { path('~/twtxt') };
has pager => sub { 1 };
has sorting => sub { "descending" };
has timeout => sub { 5 };
has use_cache => sub { 1 };
has limit => sub { 20 };
has time_format => sub { '%F %H:%M' };
has disclose_identity => sub { 0 };
has rewrite_urls => sub { 1 };
has embed_names => sub { 1 };
has check_following => sub { 1 };
has following => sub { {} };
has nick => sub { $ENV{USER} };
has since => sub { 0 };
has until => sub { time };

has [qw( twturl pre_tweet_hook post_tweet_hook config force )];

sub new {
my ( $class, @args ) = @_;
my $args = ref $args[0] ? $args[0] : {@args};
$args->{use_cache} = delete $args->{cache};
$args->{config} =
path( $args->{config} || '~/.config/twtxt/config' );

$args->{since} = $class->to_epoch( $args->{since} )
if exists $args->{since};
$args->{until} = $class->to_epoch( $args->{until} )
if exists $args->{until};

if ( exists $args->{ascending} and $args->{ascending} ) {
$args->{sorting} = 'ascending';
}
Expand All @@ -67,7 +62,8 @@ sub BUILDARGS {
$args->{following} = $config->{following};
}
}
return $args;
$args->{twtfile} = path( $args->{twtfile} ) if exists $args->{twtfile};
return bless {%$args}, ref $class || $class;
}

sub _build_ua {
Expand Down Expand Up @@ -102,7 +98,7 @@ sub read_file {
}

sub to_epoch {
return $_[0] =~ /[^\d]/ ? str2time( $_[0] ) : $_[0];
return str2time( $_[1] );
}

sub sync {
Expand Down Expand Up @@ -238,6 +234,7 @@ sub parse_twtfile {
next if not defined $text;
$text = b($text)->decode;
$text =~ s/\P{XPosixPrint}//g;
$time = $self->to_epoch($time);
if ( $time and $text ) {
push @tweets,
App::txtnix::Tweet->new(
Expand Down
14 changes: 3 additions & 11 deletions lib/App/txtnix/Cache.pm
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package App::txtnix::Cache;
use strict;
use warnings;
use Moo;
use Mojo::Base -base;
use Mojo::ByteStream 'b';
use Path::Tiny;
use Mojo::JSON qw(encode_json decode_json);

has cache_dir => ( is => 'lazy', coerce => \&to_path );

sub to_path {
ref $_[0] eq 'Path::Tiny' ? $_[0] : path( $_[0] );
}

sub _build_cache_dir {
has cache_dir => sub {
my $dir = path('~/.cache/txtnix/');
$dir->mkpath if not $dir->exists;
return $dir;
}
};

sub get {
my ( $self, $key ) = @_;
Expand Down
3 changes: 1 addition & 2 deletions lib/App/txtnix/Cmd/config/edit.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package App::txtnix::Cmd::config::edit;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

sub run {
my ( $self, $opts ) = @_;
Expand Down
5 changes: 2 additions & 3 deletions lib/App/txtnix/Cmd/config/get.pm
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package App::txtnix::Cmd::config::get;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

has key => ( is => 'rw' );
has 'key';

sub run {
my ($self) = @_;
Expand Down
5 changes: 2 additions & 3 deletions lib/App/txtnix/Cmd/config/remove.pm
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package App::txtnix::Cmd::config::remove;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

has key => ( is => 'rw' );
has 'key';

sub run {
my ($self) = @_;
Expand Down
7 changes: 3 additions & 4 deletions lib/App/txtnix/Cmd/config/set.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package App::txtnix::Cmd::config::set;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

has key => ( is => 'rw' );
has value => ( is => 'rw' );
has 'key';
has 'value';

sub run {
my ($self) = @_;
Expand Down
7 changes: 3 additions & 4 deletions lib/App/txtnix/Cmd/follow.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package App::txtnix::Cmd::follow;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

has nickname => ( is => 'rw' );
has url => ( is => 'rw' );
has 'nickname';
has 'url';

sub run {
my ($self) = @_;
Expand Down
7 changes: 3 additions & 4 deletions lib/App/txtnix/Cmd/following.pm
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package App::txtnix::Cmd::following;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

has 'nick' => ( is => 'rw' );
has 'url' => ( is => 'rw' );
has 'nick';
has 'url';

sub run {
my ( $self, $whom, $url ) = @_;
Expand Down
3 changes: 1 addition & 2 deletions lib/App/txtnix/Cmd/timeline.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package App::txtnix::Cmd::timeline;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

sub run {
my $self = shift;
Expand Down
18 changes: 5 additions & 13 deletions lib/App/txtnix/Cmd/tweet.pm
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package App::txtnix::Cmd::tweet;
use Moo;
use Mojo::Base 'App::txtnix';
use Mojo::ByteStream 'b';
use App::txtnix::Tweet;
use Path::Tiny;
use String::ShellQuote qw(shell_quote);

extends 'App::txtnix';

has text => ( is => 'rw' );
has created_at => ( is => 'rw' );
has 'text';
has 'created_at';

sub run {
my ($self) = @_;
my $text = $self->text;
$text = b($text)->decode;
$text =~ s/\@(\w+)/$self->expand_mention($1)/ge;

if ( $self->created_at ) {
$self->to_epoch( $self->created_at );
}
else {
$self->created_at(time);
}
my $time = $self->created_at ? $self->to_epoch( $self->created_at ) : time;

my $tweet =
App::txtnix::Tweet->new( text => $text, timestamp => $self->created_at );
my $tweet = App::txtnix::Tweet->new( text => $text, timestamp => $time );
my $file = path( $self->twtfile );
$file->touch unless $file->exists;

Expand Down
5 changes: 2 additions & 3 deletions lib/App/txtnix/Cmd/unfollow.pm
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package App::txtnix::Cmd::unfollow;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

has nickname => ( is => 'rw' );
has 'nickname';

sub run {
my ($self) = @_;
Expand Down
5 changes: 2 additions & 3 deletions lib/App/txtnix/Cmd/view.pm
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package App::txtnix::Cmd::view;
use Moo;
extends 'App::txtnix';
use Mojo::Base 'App::txtnix';

has source => ( is => 'rw' );
has 'source';

sub run {
my ($self) = @_;
Expand Down
17 changes: 3 additions & 14 deletions lib/App/txtnix/Tweet.pm
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
package App::txtnix::Tweet;
use strict;
use warnings;
use Mojo::Base -base;
use HTTP::Date 'str2time';
use POSIX ();
use Moo;

has user => ( is => 'ro' );
has timestamp => (
is => 'ro',
coerce => \&to_epoch,
default => sub { time }
);
has text => ( is => 'ro' );

sub to_epoch {
return $_[0] =~ /[^\d]/ ? str2time( $_[0] ) : $_[0];
}
has [ 'user', 'text' ];
has timestamp => sub { time };

sub strftime {
my ( $self, $format ) = @_;
Expand Down

0 comments on commit 3860aa2

Please sign in to comment.