Skip to content

Commit

Permalink
Use Mojo::File more
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Feb 12, 2017
1 parent d1f45ff commit 12808e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/JSON/Validator.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package JSON::Validator;
use Mojo::Base -base;

use Exporter 'import';
use JSON::Validator::Error;
use Mojo::File 'path';
Expand All @@ -8,9 +9,6 @@ use Mojo::JSON::Pointer;
use Mojo::URL;
use Mojo::Util 'deprecated';
use B;
use Cwd ();
use File::Basename ();
use File::Spec;
use Scalar::Util;

use constant VALIDATE_HOSTNAME => eval 'require Data::Validate::Domain;1';
Expand All @@ -21,7 +19,7 @@ use constant DEBUG => $ENV{JSON_VALIDATOR_DEBUG} || 0;
our $VERSION = '0.92';
our @EXPORT_OK = 'validate_json';

my $BUNDLED_CACHE_DIR = File::Spec->catdir(File::Basename::dirname(__FILE__), qw(Validator cache));
my $BUNDLED_CACHE_DIR = path(path(__FILE__)->dirname, qw(Validator cache));
my $HTTP_SCHEME_RE = qr{^https?:};

sub E { JSON::Validator::Error->new(@_) }
Expand Down Expand Up @@ -73,7 +71,7 @@ sub schema {
$schema = $self->_register_document($schema, $schema->{id});
}
else {
$schema = Cwd::abs_path($schema) if -e $schema;
$schema = path($schema)->to_abs if -e $schema;
$schema = $self->_load_schema($schema);
}

Expand Down Expand Up @@ -128,8 +126,8 @@ sub _load_schema {
}
elsif ($parent) {
$url =~ s!#.*!!;
$url = File::Spec->catfile(File::Basename::dirname($parent), split '/', $url);
$namespace = Cwd::abs_path($url) || $url;
$url = path(path($parent)->dirname, split '/', $url);
$namespace = eval { $url->to_abs->to_string } || $url;
}

# Make sure we create the correct namespace if not already done by Mojo::URL
Expand Down Expand Up @@ -173,10 +171,10 @@ sub _load_schema_from_url {
my $tx;

for (@{$self->cache_paths}) {
my $path = File::Spec->catfile($_, $cache_file);
my $path = path $_, $cache_file;
next unless -r $path;
warn "[JSON::Validator] Loading cached file $path\n" if DEBUG;
return path($path)->slurp;
return $path->slurp;
}

$tx = $self->ua->get($url);
Expand All @@ -192,11 +190,11 @@ sub _load_schema_from_url {
}

sub _default_id {
my $path = Cwd::abs_path($0);
my $path = path($0)->to_abs;
state $id = 0;
$path = File::Basename::dirname($path) if $path;
$path = Cwd::getcwd unless $path;
return File::Spec->catfile($path, sprintf 'json-validator-%s.json', ++$id);
$path = $path->dirname if $path;
$path = path() unless $path;
return path($path, sprintf 'json-validator-%s.json', ++$id)->to_string;
}

sub _register_document {
Expand Down
2 changes: 2 additions & 0 deletions t/deep-mixed-ref.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use Mojo::Base -strict;
use Test::More;
use JSON::Validator;

use Carp::Always;

my $file
= File::Spec->catfile(File::Basename::dirname(__FILE__), 'spec', 'with-deep-mixed-ref.json');
my $validator = JSON::Validator->new(cache_paths => [])->schema($file);
Expand Down

0 comments on commit 12808e9

Please sign in to comment.