-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathapi2js.pl
More file actions
executable file
·65 lines (47 loc) · 1.39 KB
/
api2js.pl
File metadata and controls
executable file
·65 lines (47 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use JSON;
use LWP::UserAgent;
sub TO_JSON { return { %{ shift() } }; }
sub usage {
print "api2js.pl >>> create a JSON structure file from a REST API\n";
print "api2js.pl -url <url to api> -outfile <file for js output>\n";
}
# read in parameters
my $url = '';
my $outfile = '';
GetOptions ( 'url=s' => \$url,
'outfile=s' => \$outfile );
unless ($url and $outfile) {
&usage();
exit 0;
}
my $json = new JSON;
my $ua = LWP::UserAgent->new;
print "\nconnecting to API...\n\n";
my $data = $json->decode($ua->get($url)->content);
my $numres = scalar(@{$data->{resources}});
print "got basic data, retrieving detail information for $numres resources...\n\n";
my $structure = { service => { url => $data->{url},
name => $data->{service},
version => $data->{version},
description => $data->{description} } };
my $resources = [];
my $i = 1;
foreach my $resource (@{$data->{resources}}) {
my $retval = $json->decode($ua->get($resource->{url})->content);
push(@$resources, $retval);
print "received resource ".$resource->{name}." [$i/$numres]\n";
$i++
}
$structure->{resources} = $resources;
if (open(FH, ">$outfile")) {
print FH $json->pretty->encode($structure);
close FH;
} else {
die "could not open outfile for writing ($outfile): $@";
}
print "\nall done.\n\nHave a nice day :)\n\n";
exit;