/[A3C]/lib/A3C/AAIEduHr.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /lib/A3C/AAIEduHr.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 93 - (show annotations)
Wed Apr 30 17:23:21 2008 UTC (11 years, 11 months ago) by dpavlin
File size: 2559 byte(s)
support usage in array context so we can add arbitrary data around original one
1 package A3C::AAIEduHr;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 AAAIEduHr
9
10 =head1 DESCRIPTION
11
12 Pull varios stuff from XML at L<http://shema.aaiedu.hr/> and cache it as
13 JSON on disk
14
15 =head1 METHODS
16
17 =cut
18
19 #use base 'Jifty::Object';
20
21 use XML::Rules;
22 use Data::Dump qw/dump/;
23 use File::Slurp;
24 use JSON::XS;
25
26 =head2 vocabulary
27
28 valid are A3C::AAIEduHr->vocabulary('hrEdu001'),
29
30 valid are [ '', A3C::AAIEduHr->vocabulary('hrEdu001') ],
31
32 =cut
33
34 sub vocabulary {
35 my $self = shift;
36
37 my $name = shift;
38 die "invalid format of name" unless $name =~ /^hrEdu\d\d\d$/;
39
40 my $url = "http://shema.aaiedu.hr/aosi/$name.xml";
41 my $xml_path = Jifty::Util->absolute_path( "var/$name.xml" );
42 my $data_path = Jifty::Util->absolute_path( "var/$name.js" );
43
44 # sanity cleanup
45 unlink $xml_path if -s $xml_path == 0;
46 unlink $data_path if -s $data_path == 0;
47
48 # FIXME expire on some other way than rm?
49 if ( ! -e $xml_path ) {
50
51 my $ua = LWP::UserAgent->new;
52 my $res = $ua->get( $url );
53
54 die "can't get $url status: ", $res->status_line
55 unless $res->is_success;
56
57 write_file( $xml_path, $res->content );
58 Jifty->log->info("saved $url into $xml_path");
59 }
60
61 die "can't find xml $xml_path: $!" unless -s $xml_path;
62
63 my $data;
64
65 if ( ! -e $data_path ) {
66 my $rules = XML::Rules->new(
67 stripspaces => 8,
68 rules => [
69 _default => sub {
70 my ($tag_name, $tag_hash, $context, $parent_data) = @_;
71 warn "_default $tag_name ",dump( $tag_hash );
72 return;
73 },
74 vocabulary => 'no content',
75 vocabularyentry => sub {
76 die "no key attribute?" unless defined( $_[1]->{key} );
77 my $v = {
78 display => $_[1]->{_content},
79 value => $_[1]->{key},
80 };
81 # if same, just store key
82 $v = $_[1]->{key} if ( $v->{display} eq $v->{value} );
83 '@' . $_[0] => $v;
84 },
85 ]);
86 $data = $rules->parsefile( $xml_path )
87 || die "can't parse $xml_path: $!";
88
89 $data = $data->{vocabulary} || die "no vocabulary in data: ", dump( $data );
90
91 write_file( $data_path, encode_json( $data )) || die "can't save into $data_path: $!";
92 Jifty->log->info("saved dump into $data_path");
93 } else {
94 $data = decode_json( read_file( $data_path ) ) || die "can't read $data_path: $!";
95 Jifty->log->info("loaded dump from $data_path");
96 }
97
98 # warn '## ',ref($data),' = ',dump($data);
99
100 die "data not for for $name" unless $data->{name} eq $name;
101 warn "no vocabularyentry for $name from $data_path" unless defined( $data->{vocabularyentry} );
102
103 return @{ $data->{vocabularyentry} } if wantarray;
104 return $data->{vocabularyentry};
105 }
106
107 warn "# AAUEduHr loaded\n";
108
109 1;

  ViewVC Help
Powered by ViewVC 1.1.26