Skip to content

Commit

Permalink
add Feed items
Browse files Browse the repository at this point in the history
done using the Account object, given they require an account_id
  • Loading branch information
leejo committed Apr 24, 2016
1 parent 9200c38 commit 42a9986
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
22 changes: 22 additions & 0 deletions lib/Business/Mondo/Account.pm
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ sub get {
});
}

sub add_feed_item {
my ( $self,%params ) = @_;

$params{params}{title} && $params{params}{image_url} ||
Business::Mondo::Exception->throw({
message => "add_feed_item requires params: title, image_url",
});

$params{account_id} = $self->id;
$params{type} //= 'basic';

# title -> params[title] (params, params, params, params, params... what a mess)
$params{params} = { $self->_params_as_array_string( 'params',$params{params} ) };

my %post_params = (
%{ delete $params{params} },
%params,
);

return $self->client->api_post( '/feed',\%post_params );
}

=head1 SEE ALSO
L<Business::Mondo>
Expand Down
4 changes: 2 additions & 2 deletions lib/Business/Mondo/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ sub _api_request {

if ( $method =~ /POST|PUT|DELETE/ ) {
if ( $params ) {
$req->content_type( 'application/json' );
$req->content( JSON->new->encode( $params ) );
$req->content_type( 'application/x-www-form-urlencoded; charset=utf-8' );
$req->content( $self->normalize_params( $params ) );

carp( $req->content )
if $ENV{MONDO_DEBUG};
Expand Down
4 changes: 1 addition & 3 deletions lib/Business/Mondo/Transaction.pm
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ Returns a new instanced of the object with annotated data having called the API
sub annotate {
my ( $self,%annotations ) = @_;

%annotations = map {
+"metadata[$_]" => $annotations{$_}
} keys %annotations;
%annotations = $self->_params_as_array_string( 'metadata',\%annotations );

my $data = $self->client->api_patch( $self->url,\%annotations );
$data = $data->{transaction};
Expand Down
17 changes: 15 additions & 2 deletions lib/Business/Mondo/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ into the corresponding ISO8601 string
=cut

sub normalize_params {
my ( $self,$params ) = @_;
my ( $self,$params,$rfc_encode ) = @_;

return '' if ( ! $params || ! keys( %{ $params } ) );

return join( '&',
map { $_->[0] . '=' . $_->[1] }
map { [ _rfc5849_encode( $_ ),_rfc5849_encode( $params->{$_} ) ] }
map { $rfc_encode
? [ _rfc5849_encode( $_ ),_rfc5849_encode( $params->{$_} ) ]
: [ $_,$params->{$_} ]
}
sort { $a cmp $b }
keys( %{ $params } )
);
Expand All @@ -48,6 +51,16 @@ sub _rfc5849_encode {
return $str;
}

sub _params_as_array_string {
my ( $self,$key,$params ) = @_;

my %modded_params = map {
+"$key\[$_\]" => $params->{$_}
} keys %{ $params };

return %modded_params;
}

=head1 AUTHOR
Lee Johnson - C<leejo@cpan.org>
Expand Down
25 changes: 25 additions & 0 deletions t/business/mondo/account.t
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ is(
' ... with expected message'
);

throws_ok(
sub { $Account->add_feed_item },
'Business::Mondo::Exception'
);

is(
$@->message,
'add_feed_item requires params: title, image_url',
' ... with expected message'
);

no warnings 'redefine';
*Business::Mondo::Client::api_post = sub { {} };

ok( $Account->add_feed_item(
params => {
title => "My custom item",
image_url => "www.example.com/image.png",
background_color => "#FCF1EE",
body_color => "#FCF1EE",
title_color => "#333",
body => "Some body text to display",
},
),'->add_feed_item' );

done_testing();

# vim: ts=4:sw=4:et

0 comments on commit 42a9986

Please sign in to comment.