Skip to content

Instantly share code, notes, and snippets.

@lucas1 lucas1/Manifest.pm
Created Aug 11, 2015

Embed
What would you like to do?
package StreamRoot::Manifest;
use strict;
use warnings;
use WWW::Curl::Easy;
use JSON;
sub new {
my $class = shift;
my $self = {};
my %params = @_;
$self->{curl} = WWW::Curl::Easy->new;
$self->{curl}->setopt(CURLOPT_TIMEOUT, 180);
$self->{curl}->setopt(CURLOPT_HEADER, 0);
$self->{curl}->setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
$self->{curl}->setopt(CURLOPT_AUTOREFERER, 1);
$self->{curl}->setopt(CURLOPT_FOLLOWLOCATION, 1);
$self->{curl}->setopt(CURLOPT_SSL_VERIFYPEER, 0);
$self->{curl}->setopt(CURLOPT_VERBOSE, 0);
$self->{token} = $params{token}||undef;
return bless $self, $class;
}
sub curl {
my ( $self, $url, $data, $referer ) = @_;
my $params = "";
if (ref($data) eq 'HASH') {
for(keys %$data){
$params .= "&" if $params;
$params .= "$_=$data->{$_}";
}
}else{
$params = "$data";
}
$self->{curl}->setopt(CURLOPT_POSTFIELDS, $params);
$self->{curl}->setopt(CURLOPT_URL, $url);
my $content = '';
open(my $writedata, ">", \$content);
$self->{curl}->setopt(CURLOPT_WRITEDATA, $writedata);
if ($self->{curl}->perform == 0) {
if ($self->{curl}->getinfo(CURLINFO_HTTP_CODE) =~ /^2/) {
decode_json($content);
}else{
die($content);
}
}
}
sub setToken {
my $self = shift;
$self->{token} = shift||undef;
}
sub getToken {
my $self = shift;
return $self->{token};
}
sub authentification {
my ( $self, $username, $password ) = @_;
my $data = {
'username' => $username,
'password' => $password
};
my $content = $self->curl('http://manifests.streamroot.io/auth', $data);
$self->setToken($content->{token});
return $content->{token};
}
sub addingAManifest {
my $self = shift;
my %params = @_;
my $data = {};
$data->{url} = $params{url};
$data->{status} = $params{status} if $params{status};
$data->{ttl} = $params{ttl} if $params{ttl};
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'POST');
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]);
my $content = $self->curl('http://manifests.streamroot.io', $data);
return $content;
}
sub updatingAManifest {
my $self = shift;
my %params = @_;
my $data = {};
$data->{status} = $params{status} if $params{status};
$data->{live} = $params{live} if $params{live};
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]);
my $content = $self->curl('http://manifests.streamroot.io/' . $params{id}, $data);
return $content;
}
sub removingAManifest {
my ( $self, $id ) = @_;
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE');
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]);
my $content = $self->curl('http://manifests.streamroot.io/' . $id);
return $content;
}
sub showingAManifest {
my ( $self, $id ) = @_;
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'GET');
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]);
my $content = $self->curl('http://manifests.streamroot.io/' . $id);
return $content;
}
sub showingAllYourManifests {
my ( $self, $pattern ) = @_;
my $data = {};
$data->{pattern} = $pattern if $pattern;
$self->{curl}->setopt(CURLOPT_CUSTOMREQUEST, 'GET');
$self->{curl}->setopt(CURLOPT_HTTPHEADER(), ['Authorization:Bearer '.$self->getToken]);
my $content = $self->curl('http://manifests.streamroot.io', $data);
return $content;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.