Subject: | Complex URL parameters in Triggermail |
Date: | Thu, 17 Nov 2011 10:19:16 -0500 |
To: | bug-Triggermail [...] rt.cpan.org |
From: | Stephen Miketa <steve [...] sailthru.com> |
Sam -
I recently had a client come to us for support of your Triggermail Perl
module, and she had a problem while trying to submit complex variables in
the vars array. She would make the appropriate call and then receive Perl
errors but no email.
Example:
my $tr_mail = Triggermail->new('api_key', 'secret');
my %vars = ( charges => ["2", "3", "4", "5"] );
$tr_mail->send('receipt', 'my_email@gmail.com', \%vars);
I believe the problem had to do with generating appropriate URL parameter
arrays. In version 1.003, when complex data is submitted, the _flatten_hash
subroutine creates a hash - which goes directly into the _httpRequest
function as the following:
'sig' => '4b1e4f5ff391ffcebc31b2ac1eefb4cf',
'email' => 'steve@sailthru.com',
'format' => 'json',
'template' => 'TestTemplate',
'api_key' => 'supersecretapikey',
'vars[charges]' => [ '2', '3', '4', '5' ]
When the data is POST'ed to the API, the vars[charges] value is treated as
a string instead of an array as it should. The parameters should be set as
the following so our API can interpret them as an array:
'vars[charges][1]' => '3',
'api_key' => 'supersecretapikey',
'email' => 'steve@sailthru.com',
'template' => 'TestTemplate',
'vars[charges][0]' => '2',
'vars[charges][3]' => '5',
'vars[charges][2]' => '4'
I worked with the client to resolve the issue and developed a patch for the
module. Would you please review and update the module so that others don't
have the same problem? Please let me know if you have any questions.
(Please forgive any funkiness in my code, I haven't written Perl in years! )
Patch Below:
===================:
#####################################################
# Begin array Flattening patch. Enhancing _flatten_hash
# to create appropriate URL parameter arrays
#
# Begin Patch: 2011-11-15 steve@sailthru.com
#####################################################
sub _flatten_hash {
validate_pos(
@_,
{ type => HASHREF },
{ type => SCALAR },
{ type => HASHREF },
{ type => HASHREF },
);
my ( $self, $name, $nested_hash, $mother_hash ) = @_;
my @parents;
$self->_flatten_hash_routine( $nested_hash, $mother_hash, \@parents);
print Dumper($mother_hash);
}
sub _flatten_hash_routine {
validate_pos(
@_,
{ type => HASHREF },
{ type => HASHREF },
{ type => HASHREF },
{ type => ARRAYREF }
);
my ($self, $unflattened, $flattened, $parents) = @_;
while (my ($key, $value) = each(%$unflattened)) {
push (@$parents, $key);
my $type_name = ref($value);
if ($type_name eq "HASH") {
$self->_flatten_hash_routine($value, $flattened, $parents);
} elsif ($type_name eq "ARRAY") {
my $parent_idx = 0;
foreach (@$value) {
my $array_val = $_;
push (@$parents, $parent_idx++);
my $array_idx = $self->_build_url_idx($parents);
$flattened->{$array_idx} = $array_val;
pop(@$parents);
}
} else {
my $array_idx = $self->_build_url_idx($parents);
$flattened->{$array_idx} = $value;
}
pop (@$parents);
}
}
sub _build_url_idx {
validate_pos(
@_,
{ type => HASHREF },
{ type => ARRAYREF }
);
my ($self, $dimension) = @_;
return 'vars[' . join('][', @$dimension ) . ']';
}
#####################################################
# End Patch: steve@sailthru.com 2011-11-15
#####################################################
===================
--
*Stephen Miketa | Developer | Sailthru, Inc.*
Email. steve@sailthru.com
Phone. 724.816.1850
Message body is not shown because it is too large.