Permalink
Browse files

More spice changes

  • Loading branch information...
Getty committed Apr 16, 2012
1 parent ce4e2c6 commit cf9d51a61ee2bb94fac2f608cb8e8ed0cad832c9
Showing with 68 additions and 23 deletions.
  1. +1 −0 dist.ini
  2. +38 −23 lib/DDG/Meta/ZeroClickInfoSpice.pm
  3. +26 −0 lib/DDG/ZeroClickInfo/Spice.pm
  4. +3 −0 t/50-spice.t
View
@@ -25,6 +25,7 @@ Path::Class = 0.25
File::ShareDir = 1.03
Package::Stash = 0
URI = 1.60
+URI::Encode = 0.061
[Prereqs / TestRequires]
Test::Dirs = 0.03
@@ -4,6 +4,7 @@ use strict;
use warnings;
use Carp;
use DDG::ZeroClickInfo::Spice;
+use Package::Stash;
sub zeroclickinfospice_attributes {qw(
call
@@ -35,32 +36,46 @@ sub apply_keywords {
shift @parts;
shift @parts;
my $answer_type = lc(join(' ',@parts));
-
- {
- my %zcispice_params = (
- caller => $target,
- );
- no strict "refs";
- *{"${target}::call_self"} = sub { undef };
- *{"${target}::spice_new"} = sub {
- shift;
- DDG::ZeroClickInfo::Spice->new(%zcispice_params, ref $_[0] eq 'HASH' ? %{$_[0]} : @_)
- };
- *{"${target}::spice"} = sub {
- if (ref $_[0] eq 'HASH') {
- for (keys %{$_[0]}) {
- $zcispice_params{check_zeroclickinfospice_key($_)} = $_[0]->{$_};
- }
+ my %zcispice_params = (
+ caller => $target,
+ );
+
+ my $stash = Package::Stash->new($target);
+ $stash->add_symbol('&call_self',sub { undef });
+ $stash->add_symbol('&spice_new',sub {
+ shift;
+ my @call;
+ my %params = %zcispice_params;
+ for (@_) {
+ if (ref $_ eq 'HASH') {
+ for my $k (keys %{$_}) {
+ $params{$k} = $_->{$k};
+ };
+ } elsif (ref $_ eq 'DDG::ZeroClickInfo::Spice') {
+ return $_;
+ } elsif (!defined $_) {
+ # do nothing
} else {
- while (@_) {
- my $key = shift;
- my $value = shift;
- $zcispice_params{check_zeroclickinfospice_key($key)} = $value;
- }
+ push @call, $_;
}
- };
- }
+ }
+ $params{'call'} = [@call] if @call;
+ DDG::ZeroClickInfo::Spice->new(%zcispice_params, ref $_[0] eq 'HASH' ? %{$_[0]} : @_)
+ });
+ $stash->add_symbol('&spice',sub {
+ if (ref $_[0] eq 'HASH') {
+ for (keys %{$_[0]}) {
+ $zcispice_params{check_zeroclickinfospice_key($_)} = $_[0]->{$_};
+ }
+ } else {
+ while (@_) {
+ my $key = shift;
+ my $value = shift;
+ $zcispice_params{check_zeroclickinfospice_key($key)} = $value;
+ }
+ }
+ });
}
@@ -2,6 +2,7 @@ package DDG::ZeroClickInfo::Spice;
use Moo;
use URI;
+use URI::Encode qw(uri_encode uri_decode);
has call => (
is => 'ro',
@@ -53,6 +54,28 @@ sub _build_path {
return '/js/'.join('/',map { s/([a-z])([A-Z])/$1_$2/; lc; } @parts).'/';
}
+has callback => (
+ is => 'ro',
+ lazy => 1,
+ builder => '_build_callback',
+);
+
+sub _build_callback {
+ my ( $self ) = @_;
+ return join('_',map { s/([a-z])([A-Z])/$1_$2/; lc; } split('::',$self->caller));
+}
+
+has call_path => (
+ is => 'ro',
+ lazy => 1,
+ builder => '_build_call_path',
+);
+
+sub _build_call_path {
+ my ( $self ) = @_;
+ return $self->path.join('/',map { uri_encode($_,1) } @{$self->call});
+}
+
has nginx_conf => (
is => 'ro',
lazy => 1,
@@ -61,6 +84,9 @@ has nginx_conf => (
sub _build_nginx_conf {
my ( $self ) = @_;
+ my $to = $self->to;
+ my $callback = $self->callback;
+ $to =~ s/{{callback}}/$callback/g;
my $uri = URI->new($self->to);
my $host = $uri->host;
my $scheme = $uri->scheme;
View
@@ -33,11 +33,14 @@ my $zci_spice = DDG::ZeroClickInfo::Spice->new(
from => '([^/]+)/(?:([^/]+)/(?:([^/]+)|)|)',
to => 'http://api.alternativeto.net/software/$1/?$2&$3&count=6&callback=nrat',
caller => 'DDGTest::Spice::SomeThing',
+ call => ['a##a','b b','c#??c'],
);
isa_ok($zci_spice,'DDG::ZeroClickInfo::Spice');
is($zci_spice->path,'/js/spice/some_thing/','Checking for proper path');
+is($zci_spice->callback,'ddgtest_spice_some_thing','Checking for proper callback');
+is($zci_spice->call_path,'/js/spice/some_thing/a%23%23a/b%20%20b/c%23%3F%3Fc','Checking for proper call path');
is($zci_spice->nginx_conf,<<'__END_OF_CONF__','Checking for proper nginx.conf snippet');
location ^~ /js/spice/some_thing/ {

0 comments on commit cf9d51a

Please sign in to comment.