Skip to content

Commit

Permalink
Don't rely on accessor methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Aug 14, 2014
1 parent 50cc42f commit 75213bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
3 changes: 1 addition & 2 deletions Changes
@@ -1,6 +1,5 @@
# Change file for Plack::App::SeeAlso

{{$NEXT}}
Don't rely on accessor methods

0.11 2012-04-18
Fixed some typos and better error handling
Expand Down
7 changes: 2 additions & 5 deletions dist.ini
@@ -1,7 +1,7 @@
name = Plack-App-SeeAlso
license = Perl_5
version = 0.13
copyright_year = 2013
version = 0.14
copyright_year = 2014
author = Jakob Voß
copyright_holder = Jakob Voß

Expand All @@ -22,9 +22,6 @@ match = ^.*\.psgi$
match = ^examples/*
match = ^share/example*

; Be pedantic
; [Test::Perl::Critic]

; Replace {{$NEXT}} in first line of Changes with version number and timestamp
[NextRelease]
format=%-9v %{yyyy-MM-dd}d
Expand Down
23 changes: 14 additions & 9 deletions lib/Plack/App/SeeAlso.pm
Expand Up @@ -14,7 +14,6 @@ use Plack::Util;
use Carp qw(croak);
use Scalar::Util qw(blessed reftype);
use Try::Tiny;
use Data::Dumper;
use JSON;
use Encode;

Expand All @@ -41,26 +40,34 @@ sub prepare_app {
my $self = shift;
return if $self->{app}; # already initialized

# get default values from module variables
# get default configuration from module variables
$self->{Stylesheet} = 'seealso.xsl' unless exists $self->{Stylesheet};
foreach (@PROPERTIES) {
no strict 'refs';
$self->$_( ${ref($self)."::$_"} // '' ) unless exists $self->{$_};
unless (exists $self->{$_}) {
$self->{$_} = ${ref($self)."::$_"} // '';
}
}

# validate and normalize configuration
$self->{ShortName} = sprintf '%.16s', $self->{ShortName} // '';
$self->{LongName} = sprintf '%.48s', $self->{LongName} // '' ;
$self->{Description} = sprintf '%.1024s', $self->{Description} // '';
$self->{Tags} = sprintf '%.256s', $self->{Tags} // '';
$self->{Attribution} = sprintf '%.256s', $self->{Attribution} // '';

my $examples = $self->{Examples};
$examples = [] unless ref $examples and reftype $examples eq 'ARRAY';
$self->{Examples} = [
grep { ref $_ and reftype($_) eq 'HASH' and $_->{id} } @$examples
];

# TODO: validate
# Stylesheet
# Formats
# Contact
# Source
# DateModified
# Examples

my %formats = %{ $self->{Formats} || { } };
delete $formats{$_} for (qw(opensearchdescription seealso _));
Expand All @@ -79,7 +86,6 @@ sub prepare_app {
$formats{seealso} = [ $f->app( sub { $self->query(@_) } ), $f->type ];

my $app = unAPI( %formats );

$app = Plack::Middleware::JSONP->wrap($app);

if ($self->{Stylesheet}) {
Expand All @@ -105,7 +111,7 @@ sub call {
Plack::Util::response_cb( $result, sub {
my $res = shift;
return unless $res->[0] == 300;
my $base = $self->base || Plack::Request->new($env)->base;
my $base = $self->{base} || Plack::Request->new($env)->base;
my $xsl = $self->{Stylesheet};
$xsl = '<?xml-stylesheet type="text/xsl" href="'.$xsl.'"?>';
$xsl .= "\n<?seealso-query-base $base?>\n";
Expand All @@ -126,8 +132,7 @@ sub openSearchDescription {
xmlns:seealso="http://ws.gbv.de/seealso/schema/">';

my @prop = (
map { $_ => $_ } qw(ShortName LongName Description Tags
Contact Developer Attribution),
map { $_ => $_ } qw(ShortName LongName Description Tags Contact Developer Attribution),
DateModified => 'dcterms:modified',
Source => 'dc:source',
);
Expand All @@ -138,7 +143,7 @@ sub openSearchDescription {
push @xml, " <$tag>"._xmlescape($value)."</$tag>";
}

foreach (@{ $self->Examples || [] }) {
foreach (@{ $self->{Examples} || [] }) {
my $id = _xmlescape($_->{id});
push @xml, "<Query role=\"example\" searchTerms=\"$id\" />";
}
Expand Down

0 comments on commit 75213bc

Please sign in to comment.