Permalink
Browse files

cosmetic changes and parameter validation

  • Loading branch information...
1 parent 8a0a880 commit 1a0a7e488f22ef2d19f5134462bd51d64396f9a2 @rizen committed Nov 6, 2012
Showing with 30 additions and 7 deletions.
  1. +5 −1 Changelog
  2. +4 −4 dist.ini
  3. +9 −2 lib/Box/Calc.pm
  4. +12 −0 t/03_calc.t
View
@@ -1,4 +1,8 @@
-version 0.0500 - October 27, 2012
+version 0.0501 - Novemver 6, 2012
+ * Fix some cosmetic problems.
+ * Added some parameter validation for the shipping_options method.
+
+version 0.0500 - November 6, 2012
* NOT backwards compatible.
* Now works asynchronously.
* Add SSL to web service.
View
@@ -4,7 +4,7 @@ license = Perl_5
copyright_holder = Plain Black Corporation
copyright_year = 2012
-version = 0.0500
+version = 0.0501
[@Classic]
@@ -20,9 +20,9 @@ Test::More = 0
Test::Deep = 0
Log::Any = 0
Ouch = 0.0400
-JSON = 2.0;
-AnyEvent::HTTP::LWP::UserAgent = 0;
-AnyEvent = 0;
+JSON = 2.0
+AnyEvent::HTTP::LWP::UserAgent = 0
+AnyEvent = 0
[PruneFiles]
filenames = BoxCalc.kpf
View
@@ -116,6 +116,11 @@ has items => (
}
);
+has implementation => (
+ is => 'rw',
+ default => 'perl',
+);
+
=head2 add_item(quantity, params)
Registers a new item. Returns the new item registered.
@@ -158,7 +163,7 @@ Returns a data structure with all the item names and quantities packed into boxe
sub packing_list {
my $self = shift;
- my $payload = {api_key => $self->api_key};
+ my $payload = {api_key => $self->api_key, implementation => $self->implementation};
foreach my $type (@{$self->box_types}) {
push @{$payload->{box_types}}, {
weight => $type->weight,
@@ -234,7 +239,9 @@ A 5 digit zip code (if shipping inside the United States) or the name of a coun
sub shipping_options {
my ($self, %params) = @_;
- my $payload = {api_key => $self->api_key, to => $params{to}, from => $params{from}};
+ ouch('bad param', '"from" must be a 5 digit zip code.', 'from') unless $params{from} =~ m/^\d{5}$/xms;
+ ouch('bad param', '"to" must be a 5 digit zip code or a country name.', 'to') unless ($params{to} =~ m/^\d{5}$/xms || length($params{to}) > 5);
+ my $payload = {api_key => $self->api_key, implementation => $self->implementation, to => $params{to}, from => $params{from}};
foreach my $type (@{$self->box_types}) {
push @{$payload->{box_types}}, {
weight => $type->weight,
View
@@ -16,5 +16,17 @@ my $item = $calc->add_item(x => 3, y => 7, z => 2, name => 'test', weight => 14)
isa_ok $item, 'Box::Calc::Item';
+eval { $calc->shipping_options(from => 531) };
+
+isa_ok $@, 'Ouch';
+is $@->code, 'bad param', 'from zip fails as it should';
+is $@->data, 'from', 'it is the from field failing';
+
+eval { $calc->shipping_options(from => 53716 ) };
+
+isa_ok $@, 'Ouch';
+is $@->code, 'bad param', 'to fails as it should';
+is $@->data, 'to', 'it is the to field failing';
+
done_testing;

0 comments on commit 1a0a7e4

Please sign in to comment.