Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #383 from duckduckgo/zaahir/remove-metadata-requir…
Browse files Browse the repository at this point in the history
…ement

Remove Metadata requirement for IAs to run in DuckPAN
  • Loading branch information
zachthompson authored Apr 26, 2017
2 parents e1260bc + ff53196 commit 390d577
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/App/DuckPAN.pm
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ sub phrase_to_camel {
sub get_ia_by_name {
my ($self, $name) = @_;
my $ia;

if ($name =~ /^DDG::/) {
$ia = DDG::Meta::Data->get_ia(module => $name);
$ia = $ia->[0] if $ia;
Expand All @@ -442,6 +443,7 @@ sub get_ia_by_name {
? DDG::Meta::Data->get_ia(id => $name)
: DDG::Meta::Data->get_ia(id => $self->camel_to_underscore($name));
}

return $ia;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/App/DuckPAN/Cmd/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ sub change_js {
}

# Rewrite all relative asset links in CSS
# E.g url("/assets/background.png") => url("http://duckduckgo.com/assets")
# Capture leading quote, insert $hostname, append filename
# E.g url("/assets/background.png") => url("http://duckduckgo.com/assets/background.png")
sub change_css {
my ( $self, $css ) = @_;
my $hostname = $self->hostname;
Expand Down
62 changes: 55 additions & 7 deletions lib/App/DuckPAN/DDG.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,36 @@ use Class::Load ':all';
use Data::Printer return_value => 'dump';
use List::Util qw (first);

# This function tells the user which modules / instant answers failed to load.
# This function tells the user which modules / Instant Answers failed to load
sub show_failed_modules {
my ($self, $failed_to_load) = @_;

if (%$failed_to_load) {
$self->app->emit_notice("These instant answers were not loaded:");
$self->app->emit_notice("These Instant Answers were not loaded:");
$self->app->emit_notice(p($failed_to_load, colored => $self->app->colors));
$self->app->emit_notice(
"To learn more about installing Perl dependencies, please read http://docs.duckduckhack.com/resources/other-dev-environments.html#dealing-with-installation-issues.",
"Note: You can ignore these errors if you're not working on these instant answers."
"Note: You can ignore these errors if you're not working on these Instant Answers."
) if first { /dependencies/ } values %$failed_to_load;
}
}

# This function tells the user which modules / Instant Answers have no associated metadata
sub show_no_metadata_modules {
my ($self, $no_metadata) = @_;

if (@$no_metadata) {
$self->app->emit_notice("No metadata was found for these Instant Answers:");
$self->app->emit_notice(p($no_metadata, colored => $self->app->colors));
$self->app->emit_notice(
"Using temporary Metadata instead.",
"If you have created an IA Page, please ensure it is in 'development' status or later.",
"To update the IA Page status, you will need to open a Pull Request.",
"More info: https://docs.duckduckhack.com/submitting/submitting-overview.html\n"
);
}
}

sub get_blocks_from_current_dir {
my ($self, @args) = @_;

Expand Down Expand Up @@ -65,21 +81,50 @@ sub get_blocks_from_current_dir {
# The key contains the module name and the value contains the dependency that wasn't met.
my %failed_to_load;

my @no_metadata;

my (%blocks_plugins, @UC_TRIGGERS);
# This loop goes through each Goodie / Spice, and it tries to load it.
foreach my $arg (@args) {

# Let's try to load each Goodie / Spice module
# and see if they load successfully.
my $class;

# Attempt to load Metadata based on passed ID
if (my $ia = $self->app->get_ia_by_name($arg)) {
$class = $ia->{perl_module};
}

# Check if input resembles an ID
# i.e. lowercased alphanumeric string, possibly containing underscores
# Assumes no lowercased Perl package names exist (e.g. DDG::Goodie::lowercase)
elsif ($arg =~ /^[a-z0-9\_]+$/) {
my @msg = (
"Could not retrieve Instant Answers Metadata for ID: $arg.",
"If a local Perl Module exists, please provide the module name instead of the ID.",
"Or, if you have created an IA Page for any of these, please ensure its status is 'development' or 'testing'.",
"To update the IA Page status, you will need to open a Pull Request.",
"More info: https://docs.duckduckhack.com/submitting/submitting-overview.html\n"
);
$self->app->emit_notice(@msg);
$failed_to_load{$arg} = "No metadata found. See details above.";
next;
}

# Check if Perl Package name provided
# We don't have Goodie packages with
elsif ($arg =~ /^(DDG::(?:Goodie|Spice|Fathead)::)?[A-Z]+[a-z0-9]*?$/) {
push @no_metadata, $arg;
$class = $1 ? $arg : "DDG::$type->{name}::$arg";
}

# Bad input
else {
$failed_to_load{$arg} =
'Could not retrieve metadata - please ensure the Instant Answer page ' .
'is in development status or later.';
$failed_to_load{$arg} = "Invalid Instant Answer ID or Perl package name";
next;
}

my ($load_success, $load_error_message) = try_load_class($class);

# If they load successfully, $load_success would be a 1.
Expand Down Expand Up @@ -135,6 +180,9 @@ sub get_blocks_from_current_dir {
# and @successfully_loaded does, we just use what's in @successfully_loaded.
@args = @successfully_loaded;

# Now let's tell the user which Instant Answers have no metadata
$self->show_no_metadata_modules(\@no_metadata);

# Now let's tell the user why some of the modules failed.
$self->show_failed_modules(\%failed_to_load);

Expand All @@ -144,7 +192,7 @@ sub get_blocks_from_current_dir {
}

if(@UC_TRIGGERS){
$self->app->emit_notice('Detected potential UPPERCASE triggers in the following instant answers. If yours is listed, check it out! Only lowercase will work.' . "\n"
$self->app->emit_notice('Detected potential UPPERCASE triggers in the following Instant Answers. If yours is listed, check it out! Only lowercase will work.' . "\n"
. p(@UC_TRIGGERS, colored => $self->app->colors));
}

Expand Down
14 changes: 11 additions & 3 deletions lib/App/DuckPAN/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,17 @@ sub request {
my $caller = $result->caller;
my $id;
#exlude parent/child IAs like CheatSheets
if(my $ia = DDG::Meta::Data->get_ia(module => $caller)){
if (my $ia = DDG::Meta::Data->get_ia(module => $caller)){
if (@$ia == 1) {
$id = $ia->[0]{id};
push @ids, $id;
}
}
else {
my $clean_module = $caller;
$clean_module =~ s/DDG::[^:]+:://;
$id = App::DuckPAN->camel_to_underscore($clean_module);
}
push @ids, $id;

# Info for terminal.
p($result) if $result;
Expand Down Expand Up @@ -480,7 +485,10 @@ sub request {
# calls_nrc : css calls
# calls_template : handlebars templates

my $calls_nrj = join('', map{ DDG::Meta::Data->get_js(id => $_) } @ids);
my $calls_nrj = join('', map {
DDG::Meta::Data->get_js(id => $_)
|| qq(DDH.$_=DDH.$_||{};DDH.$_.meta={"tab":"Answer", "id":"$_"};)
} @ids);
my $calls_script = join('', map { q|<script type='text/JavaScript' src='| . $_ . q|'></script>| } @calls_script);
# For now we only allow a single goodie. If that changes, we will do the
# same join/map as with spices.
Expand Down

0 comments on commit 390d577

Please sign in to comment.