App-Filite-Client
view release on metacpan or search on metacpan
`new_from_config()`
Load %attrs from the config instead of as parameters.
Attributes
All attributes are read-write.
`server` Str
The filite server to share things via. This will typically be a URL
like "https://example.com/" or "http://example.net:8080".
`password` Str
Filite is a single user system so has a password but no username.
`useragent` Object
Can be set to a custom HTTP::Tiny instance. Cannot be specified in the
config file.
`errors` Int
The number of errors which have been seen so far. It makes little
sense to set this in the constructor or config file.
Methods
examples/config.json view on Meta::CPAN
{ "password": "abc123", "server": "example.com" }
lib/App/Filite/Client.pm view on Meta::CPAN
use Carp qw( croak );
use File::XDG;
use Getopt::Long qw( GetOptionsFromArray );
use HTTP::Tiny;
use HTTP::Tiny::Multipart;
use JSON::PP qw( encode_json decode_json );
use MIME::Base64 qw( encode_base64 );
use Path::Tiny qw( path );
use Class::Tiny {
password => sub { croak "Missing option: password" },
server => sub { croak "Missing option: server" },
useragent => sub { shift->_build_useragent },
errors => sub { 0 },
};
use namespace::autoclean;
sub new_from_config {
my ( $class ) = ( shift );
lib/App/Filite/Client.pm view on Meta::CPAN
}
croak "Expected config file: $config_file" unless $config_file->is_file;
my $args = decode_json( $config_file->slurp_utf8 );
my $self = $class->new( %$args );
return $self;
}
sub _build_useragent {
my ( $self ) = ( shift );
my $auth = encode_base64( sprintf( 'admin:%s', $self->password ) );
chomp $auth;
return HTTP::Tiny->new(
agent => sprintf( '%s/%s ', __PACKAGE__, $VERSION ),
default_headers => { 'Authorization' => "Basic $auth" },
);
}
sub _parse_opts {
my ( $self, $args ) = ( shift, @_ );
lib/App/Filite/Client.pm view on Meta::CPAN
All attributes are read-write.
=over
=item C<< server >> B<< Str >>
The filite server to share things via. This will typically be a URL
like "https://example.com/" or "http://example.net:8080".
=item C<< password >> B<< Str >>
Filite is a single user system so has a password but no username.
=item C<< useragent >> B<< Object >>
Can be set to a custom L<HTTP::Tiny> instance. Cannot be specified in
the config file.
=item C<< errors >> B<< Int >>
The number of errors which have been seen so far. It makes little
sense to set this in the constructor or config file.
t/share/config.json view on Meta::CPAN
{ "password": "abc123", "server": "example.com" }
t/unit/App/Filite/Client.t view on Meta::CPAN
};
describe "method `new_from_config`" => sub {
tests 'it works' => sub {
my $object = do {
local $ENV{'FILITE_CLIENT_CONFIG'} = "$SHARE/config.json";
$CLASS->new_from_config;
};
isa_ok( $object, $CLASS );
is( $object->password, 'abc123', 'password attribute' );
is( $object->server, 'example.com', 'server attribute' );
is( $object->errors, 0, 'errors attribute' );
isa_ok( $object->useragent, 'HTTP::Tiny' );
};
};
describe "method `share`" => sub {
my $guard;
my @calls;
t/unit/App/Filite/Client.t view on Meta::CPAN
};
case 'imply text' => sub {
@input = ( "$SHARE/image.png", { highlight => 1 } );
@urls = ( 'http://example.net/t/foo' );
$expected_result = $urls[0];
$expected_calls = [ [ share_text => @input ] ];
};
tests 'it works' => sub {
my $object = $CLASS->new( server => 'example.com', password => 1 );
my $result = $object->share( @input );
is( $result, $expected_result, 'result' );
is( \@calls, $expected_calls, 'calls' );
};
};
describe "method `share_text`" => sub {
tests 'it works' => sub {
t/unit/App/Filite/Client.t view on Meta::CPAN
my $mock = mock {}, add => [
post => sub {
shift;
@args = @_;
return { success => 1, content => 'abc' };
},
];
my $object = $CLASS->new(
useragent => $mock,
server => 'example.org',
password => 'xyz',
);
my $got = $object->share_text( "$SHARE/file.txt", { highlight => 1 } );
is( $got, 'http://example.org/t/abc', 'result' );
is(
\@args,
array {
item string 'http://example.org/t';
item hash {
t/unit/App/Filite/Client.t view on Meta::CPAN
my $mock = mock {}, add => [
post_multipart => sub {
shift;
@args = @_;
return { success => 1, content => 'abc' };
},
];
my $object = $CLASS->new(
useragent => $mock,
server => 'example.org',
password => 'xyz',
);
my $got = $object->share_file( "$SHARE/image.png", {} );
is( $got, 'http://example.org/f/abc', 'result' );
is(
\@args,
array {
item string 'http://example.org/f';
item hash { etc; };
t/unit/App/Filite/Client.t view on Meta::CPAN
my $mock = mock {}, add => [
post => sub {
shift;
@args = @_;
return { success => 1, content => 'abc' };
},
];
my $object = $CLASS->new(
useragent => $mock,
server => 'example.org',
password => 'xyz',
);
my $got = $object->share_link( 'https://www.forward.example/', {} );
is( $got, 'http://example.org/l/abc', 'result' );
is(
\@args,
array {
item string 'http://example.org/l';
item hash {
( run in 0.787 second using v1.01-cache-2.11-cpan-49f99fa48dc )