From b6dae853c757cbbe5a389c7c574e8a39a832d960 Mon Sep 17 00:00:00 2001 From: Jeremiah Foster Date: Fri, 23 Oct 2009 02:52:04 +0300 Subject: [PATCH] Added Parson module to parse json. --- Parson.pm | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ minimae-2.0 | 8 +++++++- t/parson | 20 ++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100755 Parson.pm create mode 100644 t/parson diff --git a/Parson.pm b/Parson.pm new file mode 100755 index 0000000..a588f48 --- /dev/null +++ b/Parson.pm @@ -0,0 +1,52 @@ +package Parson; + +# Copyright (C) Jeremiah C. Foster 2009 + +=head1 NAME + +parson.pl - parse json + +=head1 VERSION + +This document describes version 0.1 + +=head1 PURPOSE + +Create something readable from a json file + +=head1 SYNOPSIS + +=cut + +use Moose; + +has 'file' => ( isa => 'Str', is => 'rw', default => '/home/jeremiah/maemian/json.txt' ); + +__PACKAGE__->meta->make_immutable; + +sub parse_json { + use JSON; + use Perl6::Slurp; + use File::Basename; + my ($self, $location) = @_; + my @json = slurp $self->file; + my $text = decode_json $json[0]; + + while( my ($k, $v) = each %$text ) { + if (ref($v) eq "ARRAY") { + if ($k eq 'targets') { + # print map { "ARRAY key: $k, value: @$_\n" } @$v; + } + elsif ($k eq "fnames") { + my $dsc = "@$v"; + return basename $dsc; + } + else { + # print "ARRAY key: $k, value: @$v.\n"; + } + } + } +} + + +1; diff --git a/minimae-2.0 b/minimae-2.0 index cfc50ff..6af4525 100755 --- a/minimae-2.0 +++ b/minimae-2.0 @@ -41,6 +41,12 @@ use Archive::Tar; use Perl6::Slurp; use RepoUtils; use File::Basename; +use Parson; + +# find our tarball +my $json_object = Parson->new(); # parse json file +my $dsc = $json_object->parse_json(); + my $tarball = $ARGV[0]; # Orignal tarball my $repo = RepoUtils->new(); @@ -57,7 +63,7 @@ else { } $package_name =~ tr/_/-/; # translate underscore into dash $package_name =~ s/-[\d]$//; # trim package version -$package_name =~ s/-.?maemo.$//; # trim package version +$package_name =~ s/-.?maemo.$//; # trim maemo version chdir("lab/"); # We're in /lab now! $tar->extract(); # extracted tarball now in lab/ diff --git a/t/parson b/t/parson new file mode 100644 index 0000000..9d22a48 --- /dev/null +++ b/t/parson @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +# Standard testing of the Parson package which is desinged to +# receive a JSON stream and pull out date from it. + +use strict; +use warnings; +use lib('/home/jeremiah/maemian/'); +use Parson; +use Test::More tests => 6; + +BEGIN { use_ok( 'Parson' ); } +require_ok( 'Parson' ); +my $json_object = Parson->new(); +isa_ok($json_object, 'Parson'); +like($json_object->file, qr(\/home\/jeremiah\/maemian\/json\.txt), 'Files identical.'); +can_ok($json_object, 'parse_json'); +my $dsc = $json_object->parse_json(); +like($dsc, qr(.*\.dsc$), 'Likely a dsc file'); +print "$dsc\n"; -- 1.7.9.5