App-Wax

 view release on metacpan or  search on metacpan

lib/App/Wax.pm  view on Meta::CPAN

# return a best-effort guess at the URL's file extension based on its content
# type, e.g. ".md" or ".tar.gz", or an empty string if one can't be determined.
# XXX note: makes a network request to determine the content type
method extension ($_url) {
    my ($url, $url_index) = @$_url;
    my $extension = '';
    my $split = $self->is_url($url);

    return $extension unless ($split);

    my ($scheme, $domain, $path, $query, $fragment) = @$split;
    my $content_type = $self->content_type($_url);

    return $extension unless ($content_type); # won't be defined if the URL is invalid

    if (INFER_EXTENSION->{$content_type}) {
        # try to get a more specific extension from the path
        if (not(defined $query) && $path && ($path =~ EXTENSION)) {
            $extension = $+;
        }
    }

lib/App/Wax.pm  view on Meta::CPAN


    $self->debug('extension (%d): %s', $url_index, $extension);

    return $extension;
}

# return a truthy value (an arrayref containing the URL's components)
# if the supplied value can be parsed as a URL, or a falsey value otherwise
method is_url ($url) {
    if ($url =~ m{^[a-zA-Z][\w+]*://}) { # basic sanity check
        my ($scheme, $domain, $path, $query, $fragment) = uri_split($url);

        if ($scheme && ($domain || $path)) { # no domain for file:// URLs
            return [$scheme, $domain, $path, $query, $fragment];
        }
    }
}

# log a message to stderr if logging is enabled
method debug ($template, @args) {
    if ($self->verbose) {
        my $name = $self->app_name;
        my $message = @args ? sprintf($template, @args) : $template;
        warn "$name: $message", $/;

t/lib/Test/App/Wax.pm  view on Meta::CPAN

# can avoid network requests and make the filenames deterministic.
# XXX note this doesn't allow us to (properly) test things like --timeout
# and --user-agent
mock(
    'App::Wax::resolve' => method ($_url) {
        my ($url, $url_index) = @$_url;
        my $dir = $self->directory;
        my $filename;

        if ($dir) {
            my ($scheme, $domain, $path, $query, $fragment) = uri_split($url);
            $path =~ s{^/}{}; # /1.json => 1.json
            $filename = File::Spec->catfile($dir, "file$path");
        } else {
            $filename = $self->keep ? $FILENAME_KEEP{$url} : $FILENAME_TEMP{$url};
        }

        my @resolved = ($filename, undef); # (filename, error)

        return wantarray ? @resolved : \@resolved;
    },



( run in 1.219 second using v1.01-cache-2.11-cpan-b16cb0d3907 )