Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tuned spice parameters

  • Loading branch information...
commit ce4e2c6855d70610fab4748f7adf4792de1c860f 1 parent b11da82
@Getty Getty authored
View
1  dist.ini
@@ -24,6 +24,7 @@ Module::Data = 0.001
Path::Class = 0.25
File::ShareDir = 1.03
Package::Stash = 0
+URI = 1.60
[Prereqs / TestRequires]
Test::Dirs = 0.03
View
18 lib/DDG/Meta/ShareDir.pm
@@ -36,18 +36,28 @@ sub apply_keywords {
$basedir = $basedir->parent;
}
+ my $dir;
if ( -e $basedir->subdir('lib') and -e $basedir->subdir('share') ) {
- return dir($basedir->subdir('share'),$share_path);
+ $dir = dir($basedir->subdir('share'),$share_path);
+ return $dir if -d $dir;
} else {
- return dir(module_dir($target));
+ eval {
+ $dir = module_dir($target);
+ return $dir;
+ }
}
+ return "";
+
};
my $stash = Package::Stash->new($target);
- $stash->add_symbol('&module_share_dir', sub { dir('share',$share_path) });
+ $stash->add_symbol('&module_share_dir', sub {
+ $share_code->() unless defined $share;
+ $share;
+ });
$stash->add_symbol('&share', sub {
- $share = $share_code->() unless defined $share;
+ $share_code->() unless defined $share;
@_ ? -d dir($share,@_)
? $share->subdir(@_)
: $share->file(@_)
View
3  lib/DDG/Meta/ZeroClickInfoSpice.pm
@@ -10,6 +10,8 @@ sub zeroclickinfospice_attributes {qw(
caller
is_cached
ttl
+ from
+ to
)}
sub check_zeroclickinfospice_key {
@@ -40,6 +42,7 @@ sub apply_keywords {
);
no strict "refs";
+ *{"${target}::call_self"} = sub { undef };
*{"${target}::spice_new"} = sub {
shift;
DDG::ZeroClickInfo::Spice->new(%zcispice_params, ref $_[0] eq 'HASH' ? %{$_[0]} : @_)
View
66 lib/DDG/ZeroClickInfo/Spice.pm
@@ -1,36 +1,26 @@
package DDG::ZeroClickInfo::Spice;
use Moo;
+use URI;
has call => (
is => 'ro',
+ predicate => 'has_call',
);
-has call_path => (
+has caller => (
is => 'ro',
- lazy_build => 1,
- builder => '_build_call_path',
+ required => 1,
);
-sub _build_call_path {
- my ( $self ) = @_;
- my @parts = split('::',$self->caller);
- shift @parts;
- my $path = join('/',map { s/([a-z])([A-Z])/$1_$2/; lc; } @parts);
- return $self->call_path_root.$path.'/';
-}
-
-has call_path_root => (
+has from => (
is => 'ro',
- lazy_build => 1,
- builder => '_build_call_path_root',
+ predicate => 'has_from',
);
-sub _build_call_path_root { '/' }
-
-has caller => (
+has to => (
is => 'ro',
- required => 1,
+ predicate => 'has_to',
);
has is_cached => (
@@ -40,7 +30,7 @@ has is_cached => (
has spice_js_file => (
is => 'ro',
- lazy_build => 1,
+ lazy => 1,
builder => '_build_spice_js_file',
);
@@ -50,4 +40,42 @@ sub _build_spice_js_file {
return -f $filename ? $filename : undef;
}
+has path => (
+ is => 'ro',
+ lazy => 1,
+ builder => '_build_path',
+);
+
+sub _build_path {
+ my ( $self ) = @_;
+ my @parts = split('::',$self->caller);
+ shift @parts;
+ return '/js/'.join('/',map { s/([a-z])([A-Z])/$1_$2/; lc; } @parts).'/';
+}
+
+has nginx_conf => (
+ is => 'ro',
+ lazy => 1,
+ builder => '_build_nginx_conf',
+);
+
+sub _build_nginx_conf {
+ my ( $self ) = @_;
+ my $uri = URI->new($self->to);
+ my $host = $uri->host;
+ my $scheme = $uri->scheme;
+ my $uri_path = $self->to;
+ $uri_path =~ s!$scheme://$host!!;
+ my $self_path = $self->path;
+ my $from = $self->has_from ? $self->from : "(.*)";
+ return <<"__END_OF_CONF__";
+
+location ^~ $self_path {
+ rewrite ^$self_path$from $uri_path break;
+ proxy_pass $scheme://$host/;
+}
+
+__END_OF_CONF__
+}
+
1;
View
20 t/50-spice.t
@@ -10,6 +10,8 @@ use lib "$Bin/lib";
use DDGTest::Spice::Words;
use DDGTest::Spice::Regexp;
+use DDG::ZeroClickInfo::Spice;
+
my $spice = DDGTest::Spice::Words->new( block => undef );
isa_ok($spice,'DDGTest::Spice::Words');
@@ -27,4 +29,22 @@ is_deeply(DDGTest::Spice::Regexp->get_triggers,{
query_raw => [qr/aregexp (.*)/i, qr/bregexp (.*) (.*)/i, qr/cregexp (.*)/i]
},'Checking resulting get_triggers of DDGTest::Spice::Regexp',);
+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',
+);
+
+isa_ok($zci_spice,'DDG::ZeroClickInfo::Spice');
+
+is($zci_spice->path,'/js/spice/some_thing/','Checking for proper path');
+is($zci_spice->nginx_conf,<<'__END_OF_CONF__','Checking for proper nginx.conf snippet');
+
+location ^~ /js/spice/some_thing/ {
+ rewrite ^/js/spice/some_thing/([^/]+)/(?:([^/]+)/(?:([^/]+)|)|) /software/$1/?$2&$3&count=6&callback=nrat break;
+ proxy_pass http://api.alternativeto.net/;
+}
+
+__END_OF_CONF__
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.