[libcatmandu-perl] 01/101: fixes url_encode and url_decode
Jonas Smedegaard
dr at jones.dk
Tue Feb 23 13:43:48 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository libcatmandu-perl.
commit 0bb54f016c52471a17b6216e61428151160f6cce
Author: njfranck <njfranck at ca20c521.ugent.be>
Date: Tue Nov 17 12:20:03 2015 +0100
fixes url_encode and url_decode
---
cpanfile | 2 ++
lib/Catmandu/Fix/url_decode.pm | 40 +++++++++++++++++++++++++++++++++++++
lib/Catmandu/Fix/url_encode.pm | 41 ++++++++++++++++++++++++++++++++++++++
t/Catmandu-Fix-url_decode.t | 24 ++++++++++++++++++++++
t/Catmandu-Fix-url_encode.t | 24 ++++++++++++++++++++++
t/Catmandu-Fix-url_encode_decode.t | 32 +++++++++++++++++++++++++++++
6 files changed, 163 insertions(+)
diff --git a/cpanfile b/cpanfile
index 8085d93..f578783 100644
--- a/cpanfile
+++ b/cpanfile
@@ -39,7 +39,9 @@ requires 'Try::Tiny::ByClass', '0.01';
requires 'URI', 0;
requires 'URI::Template', 0;
requires 'YAML::XS', '0.41';
+requires 'URL::Encode','0';
+recommends 'URL::Encode::XS','0';
recommends 'Log::Log4perl', '1.44';
recommends 'Log::Any::Adapter::Log4perl', '0.06';
diff --git a/lib/Catmandu/Fix/url_decode.pm b/lib/Catmandu/Fix/url_decode.pm
new file mode 100644
index 0000000..a6ec8d9
--- /dev/null
+++ b/lib/Catmandu/Fix/url_decode.pm
@@ -0,0 +1,40 @@
+package Catmandu::Fix::url_decode;
+use Catmandu::Sane;
+use Moo;
+use Catmandu::Fix::Has;
+use URL::Encode;
+
+has path => (fix_arg => 1);
+
+with 'Catmandu::Fix::SimpleGetValue';
+
+sub emit_value {
+ my ($self, $var) = @_;
+ "${var} = URL::Encode::url_decode_utf8( ${var} );";
+}
+
+1;
+
+=pod
+
+=head1 NAME
+
+Catmandu::Fix::url_decode - url decode a string
+
+=head1 SYNOPSIS
+
+ # '3%A9' => 'café'
+ url_decode(place)
+
+ # '%E1%BD%81+%CF%84%E1%BF%B6%CE%BD+%CE%A0%CE%AD%CF%81%CF%83%CF%89%CE%BD+%CE%B2%CE%B1%CF%83%CE%B9%CE%BB%CE%B5%CF%8D%CF%82' => 'ὁ τῶν Πέρσων βασιλεύς'
+ url_decode(title)
+
+=head1 SEE ALSO
+
+L<Catmandu::Fix>, L<Catmandu::Fix::url_encode>, L<URL::Encode>
+
+=head1 AUTHOR
+
+Nicolas Franck, C<< <nicolas.franck at ugent.be> >>
+
+=cut
diff --git a/lib/Catmandu/Fix/url_encode.pm b/lib/Catmandu/Fix/url_encode.pm
new file mode 100644
index 0000000..af64afd
--- /dev/null
+++ b/lib/Catmandu/Fix/url_encode.pm
@@ -0,0 +1,41 @@
+package Catmandu::Fix::url_encode;
+use Catmandu::Sane;
+use Moo;
+use Catmandu::Fix::Has;
+use URL::Encode;
+
+has path => (fix_arg => 1);
+
+with 'Catmandu::Fix::SimpleGetValue';
+
+sub emit_value {
+ my ($self, $var) = @_;
+ "${var} = URL::Encode::url_encode_utf8( ${var} );";
+}
+
+1;
+
+=pod
+
+=head1 NAME
+
+Catmandu::Fix::url_encode - url encode a string
+
+=head1 SYNOPSIS
+
+ # 'café' => '3%A9'
+ url_encode(place)
+
+ # 'ὁ τῶν Πέρσων βασιλεύς' => '%E1%BD%81+%CF%84%E1%BF%B6%CE%BD+%CE%A0%CE%AD%CF%81%CF%83%CF%89%CE%BD+%CE%B2%CE%B1%CF%83%CE%B9%CE%BB%CE%B5%CF%8D%CF%82'
+ url_encode(title)
+
+=head1 SEE ALSO
+
+L<Catmandu::Fix>, L<Catmandu::Fix::url_decode>, L<URL::Encode>
+
+=head1 AUTHOR
+
+Nicolas Franck, C<< <nicolas.franck at ugent.be> >>
+
+=cut
+
diff --git a/t/Catmandu-Fix-url_decode.t b/t/Catmandu-Fix-url_decode.t
new file mode 100644
index 0000000..6779e79
--- /dev/null
+++ b/t/Catmandu-Fix-url_decode.t
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use utf8;
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+my $pkg;
+BEGIN {
+ $pkg = 'Catmandu::Fix::url_decode';
+ use_ok $pkg;
+}
+
+is_deeply
+ $pkg->new('name')->fix({name => 'caf%C3%A9'}),
+ {name => "café"},
+ "unescape utf8 string from French";
+
+is_deeply
+ $pkg->new('name')->fix({name => '%E1%BD%81+%CF%84%E1%BF%B6%CE%BD+%CE%A0%CE%AD%CF%81%CF%83%CF%89%CE%BD+%CE%B2%CE%B1%CF%83%CE%B9%CE%BB%CE%B5%CF%8D%CF%82'}),
+ {name => "ὁ τῶν Πέρσων βασιλεύς"},
+ "unescape utf8 string from Greek";
+
+done_testing 3;
diff --git a/t/Catmandu-Fix-url_encode.t b/t/Catmandu-Fix-url_encode.t
new file mode 100644
index 0000000..ae1d3d1
--- /dev/null
+++ b/t/Catmandu-Fix-url_encode.t
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl
+use utf8;
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+my $pkg;
+BEGIN {
+ $pkg = 'Catmandu::Fix::url_encode';
+ use_ok $pkg;
+}
+
+is_deeply
+ $pkg->new('name')->fix({name => 'café'}),
+ {name => "caf%C3%A9"},
+ "escape utf8 string from French";
+
+is_deeply
+ $pkg->new('name')->fix({name => 'ὁ τῶν Πέρσων βασιλεύς'}),
+ {name => "%E1%BD%81+%CF%84%E1%BF%B6%CE%BD+%CE%A0%CE%AD%CF%81%CF%83%CF%89%CE%BD+%CE%B2%CE%B1%CF%83%CE%B9%CE%BB%CE%B5%CF%8D%CF%82"},
+ "escape utf8 string from Greek";
+
+done_testing 3;
diff --git a/t/Catmandu-Fix-url_encode_decode.t b/t/Catmandu-Fix-url_encode_decode.t
new file mode 100644
index 0000000..9771b70
--- /dev/null
+++ b/t/Catmandu-Fix-url_encode_decode.t
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+use utf8;
+use strict;
+use warnings;
+use Test::More;
+use Test::Exception;
+
+my $pkg1;
+my $pkg2;
+BEGIN {
+ $pkg1 = 'Catmandu::Fix::url_encode';
+ use_ok $pkg1;
+ $pkg2 = 'Catmandu::Fix::url_decode';
+ use_ok $pkg2;
+}
+
+my $obj = { name => 'café' };
+my $obj2 = { name => 'ὁ τῶν Πέρσων βασιλεύς' };
+my $fixer1 = $pkg1->new('name');
+my $fixer2 = $pkg2->new('name');
+
+is_deeply
+ $fixer2->fix( $fixer1->fix($obj) ),
+ { name => "café" },
+ "escape and unescape French";
+
+is_deeply
+ $fixer2->fix( $fixer1->fix($obj2) ),
+ { name => "ὁ τῶν Πέρσων βασιλεύς" },
+ "escape and unescape Greek";
+
+done_testing 4;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcatmandu-perl.git
More information about the Pkg-perl-cvs-commits
mailing list