Metabase-Client-Simple
view release on metacpan or search on metacpan
lib/Metabase/Client/Simple.pm view on Meta::CPAN
use 5.006;
use strict;
use warnings;
package Metabase::Client::Simple;
# ABSTRACT: a client that submits to Metabase servers
our $VERSION = '0.012';
use JSON::MaybeXS;
use HTTP::Tiny 0.056; # can_ssl
use URI;
my @valid_args;
BEGIN {
@valid_args = qw(profile secret uri);
for my $arg (@valid_args) {
no strict 'refs';
*$arg = sub { $_[0]->{$arg}; }
}
}
#pod =method new
#pod
#pod my $client = Metabase::Client::Simple->new(\%arg)
#pod
#pod This is the object constructor.
#pod
#pod Valid arguments are:
#pod
#pod profile - a Metabase::User::Profile object
#pod secret - a Metabase::User::Secret object
#pod uri - the root URI for the metabase server
#pod
#pod If you use a C<uri> argument with the 'https' scheme, you must have
#pod L<IO::Socket::SSL> and L<Net::SSLeay> installed. You may also
#pod require L<Mozilla::CA>.
#pod
#pod =cut
sub new {
my ( $class, @args ) = @_;
my $args = $class->__validate_args( \@args, { map { $_ => 1 } @valid_args } );
# uri must have a trailing slash
$args->{uri} .= "/" unless substr( $args->{uri}, -1 ) eq '/';
my $self = bless $args => $class;
unless ( $self->profile->isa('Metabase::User::Profile') ) {
Carp::confess("'profile' argument for $class must be a Metabase::User::Profile");
}
unless ( $self->secret->isa('Metabase::User::Secret') ) {
Carp::confess("'profile' argument for $class must be a Metabase::User::secret");
}
my $scheme = URI->new( $self->uri )->scheme;
my ( $can_ssl, $reason ) = HTTP::Tiny::can_ssl();
unless ( $scheme eq 'http' || ( $scheme eq 'https' && $can_ssl ) ) {
my $msg = "Scheme '$scheme' is not supported.\n";
if ( $scheme eq 'https' ) {
$msg .= $reason;
}
die $msg;
}
return $self;
}
sub _ua {
my ($self) = @_;
if ( !$self->{_ua} ) {
$self->{_ua} =
HTTP::Tiny->new( agent => __PACKAGE__ . "/" . __PACKAGE__->VERSION . " ", );
}
return $self->{_ua};
}
#pod =method submit_fact
#pod
#pod $client->submit_fact($fact);
#pod
#pod This method will submit a L<Metabase::Fact|Metabase::Fact> object to the
#pod client's server. On success, it will return a true value. On failure, it will
#pod raise an exception.
#pod
#pod =cut
sub submit_fact {
my ( $self, $fact ) = @_;
my $path = sprintf 'submit/%s', $fact->type;
$fact->set_creator( $self->profile->resource )
unless $fact->creator;
lib/Metabase/Client/Simple.pm view on Meta::CPAN
else {
return "$prefix\: " . $res->{reason};
}
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Metabase::Client::Simple - a client that submits to Metabase servers
=head1 VERSION
version 0.012
=head1 SYNOPSIS
use Metabase::Client::Simple;
use Metabase::User::Profile;
use Metabase::User::Secret;
my $profile = Metabase::User::Profile->load('user.profile.json');
my $secret = Metabase::User::Secret ->load('user.secret.json' );
my $client = Metabase::Client::Simple->new({
profile => $profile,
secret => $secret,
uri => 'http://metabase.example.com/',
});
my $fact = generate_metabase_fact;
$client->submit_fact($fact);
=head1 DESCRIPTION
Metabase::Client::Simple provides is extremely simple, lightweight library for
submitting facts to a L<Metabase|Metabase> web server.
=head1 METHODS
=head2 new
my $client = Metabase::Client::Simple->new(\%arg)
This is the object constructor.
Valid arguments are:
profile - a Metabase::User::Profile object
secret - a Metabase::User::Secret object
uri - the root URI for the metabase server
If you use a C<uri> argument with the 'https' scheme, you must have
L<IO::Socket::SSL> and L<Net::SSLeay> installed. You may also
require L<Mozilla::CA>.
=head2 submit_fact
$client->submit_fact($fact);
This method will submit a L<Metabase::Fact|Metabase::Fact> object to the
client's server. On success, it will return a true value. On failure, it will
raise an exception.
=head2 guid_exists
$client->guid_exists('2f8519c6-24cf-11df-90b1-0018f34ec37c');
This method will check whether the given GUID is found on the metabase server.
The GUID must be in lower-case, string form. It will return true or false.
Note that a server error will also result in a false value.
=head2 register
$client->register;
This method will submit the user credentials to the metabase server. It will
be called automatically by C<submit_fact> if necessary. You generally won't
need to use it. On success, it will return a true value. On failure, it will
raise an exception.
=for Pod::Coverage profile secret uri
=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
=head1 SUPPORT
=head2 Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker
at L<https://github.com/cpan-testers/Metabase-Client-Simple/issues>.
You will be notified automatically of any progress on your issue.
=head2 Source Code
This is open source software. The code repository is available for
public review and contribution under the terms of the license.
L<https://github.com/cpan-testers/Metabase-Client-Simple>
git clone https://github.com/cpan-testers/Metabase-Client-Simple.git
=head1 AUTHORS
=over 4
=item *
David Golden <dagolden@cpan.org>
=item *
Ricardo Signes <rjbs@cpan.org>
( run in 1.785 second using v1.01-cache-2.11-cpan-39bf76dae61 )