Webservice-Sendy-API
view release on metacpan or search on metacpan
lib/Webservice/Sendy/API.pm view on Meta::CPAN
package Webservice::Sendy::API;
use v5.10;
use strict;
use HTTP::Tiny;
use JSON qw/decode_json/;
use Util::H2O::More qw/baptise ddd HTTPTiny2h2o h2o ini2h2o o2d/;
our $VERSION = '0.8';
sub new {
my $pkg = shift;
my $params = { @_, ua => HTTP::Tiny->new };
my $self = baptise $params, $pkg, qw/config/;
if (not $self->config) {
my $HOME = (getpwuid($<))[7];
$self->config("$HOME/.sendy.ini");
}
if (not -e $self->config) {
die sprintf "Webservice::Sendy::API requires a configuration file! (looking for, '%s')\n", $self->config;
}
# update config field with contents of the config file
$self->config(ini2h2o $self->config);
return $self;
}
sub form_data {
my $self = shift;
return {
api_key => $self->config->defaults->api_key,
@_,
};
}
sub create_campaign {
my $self = shift;
my $params = {@_};
my @required = qw/from_name from_email reply_to title subject html_text
list_ids brand_id track_opens track_clicks send_campaign/;
my @optional = qw/plain_text segment_ids exclude_list_ids query_string schedule_date_time schedule_timezone/;
h2o $params, @required, @optional;
# FATAL error if title, subject, and html_text is not provided; the other fields
# required by the API can use defaults in the configuration file, listed after
# the check:
# look up hash for defaults to use
my $required_defaults = h2o {
title => undef,
subject => undef,
html_text => undef,
from_name => $self->config->campaign->from_name,
from_email => $self->config->campaign->from_email,
reply_to => $self->config->campaign->reply_to,
brand_id => $self->config->defaults->brand_id,
list_ids => $self->config->defaults->list_id,
track_clicks => ($params->no_track_clicks) ? 0 : 1, # --no_track_clicks
track_opens => ($params->no_track_opens) ? 0 : 1, # --no_track_opens
send_campaign => 0,
};
my $required_options = {};
foreach my $param (keys %$required_defaults) {
if (not defined $params->$param and defined $required_defaults->$param) {
$params->$param($required_defaults->$param);
}
# FATAL for anything in $required_defaults set to 'undef'
elsif (not defined $params->$param and not defined $required_defaults->$param) {
die sprintf "[campaign] Missing '%s' flag; creation requires: %s'\n", $param, join(",", keys %$required_defaults);
}
$required_options->{$param} = $params->$param;
}
( run in 1.947 second using v1.01-cache-2.11-cpan-39bf76dae61 )