Bump version and update Changes
[zeal.git] / lib / Zeal / Feed.pm
CommitLineData
320536b7
MG
1package Zeal::Feed;
2
3use 5.014000;
4use strict;
5use warnings;
6use re '/s';
7
164f653c 8our $VERSION = '0.000_002';
320536b7
MG
9
10use parent qw/Class::Accessor::Fast/;
11__PACKAGE__->mk_ro_accessors(qw/version/);
12
13use Cwd qw/getcwd/;
14use File::Spec::Functions qw/catfile rel2abs/;
15use HTTP::Tiny;
16
17use Archive::Tar;
18use File::Slurp qw/read_file/;
19use File::Which;
20use XML::Rules;
21
22sub new {
23 my ($class, $url) = @_;
24 $class->new_from_content(HTTP::Tiny->new->get($url)->{content});
25}
26
27sub new_from_file {
28 my ($class, $file) = @_;
29 $class->new_from_content(scalar read_file $file);
30}
31
32sub new_from_content {
33 my ($class, $xml) = @_;
34 my ($version, @urls) = @_;
35
36 my $self = XML::Rules->parse(
37 rules => {
38 _default => 'content',
39 entry => 'pass',
40 url => 'content array',
41 'other-versions' => undef,
42 },
43 stripspaces => 3|4,
44 )->($xml);
45 bless $self, $class
46}
47
48sub urls {
49 my ($self) = @_;
50 @{$self->{url}}
51}
52
53sub url {
54 my ($self) = @_;
55 my @urls = $self->urls;
56 $urls[int rand @urls]
57}
58
59sub _unpack_tar_to_dir {
60 my ($file, $dir) = @_;
61 my $tar = which 'tar' or which 'gtar';
62 if ($tar && !$ENV{ZEAL_USE_INTERNAL_TAR}) {
63 my $arg = '-xf';
64 $arg = '-xzf' if $file =~ /[.]t?gz$/;
65 $arg = '-xjf' if $file =~ /[.]bz2$/;
66 system $tar, -C => $dir, $arg => $file
67 } else {
68 $file = rel2abs $file;
69 my $oldwd = getcwd;
70 chdir $dir;
71 Archive::Tar->extract_archive($file);
72 chdir $oldwd;
73 }
74}
75
76sub download {
77 my ($self, $path) = @_;
78 my ($name) = $self->url =~ /([^\/])+$/;
79 my $file = catfile $path, $name;
80 HTTP::Tiny->new->mirror($self->url, $file);
81 _unpack_tar_to_dir $file, $path;
82 unlink $file;
83}
84
851;
86__END__
87
88=encoding utf-8
89
90=head1 NAME
91
92Zeal::Feed - Class representing a Dash/Zeal documentation feed
93
94=head1 SYNOPSIS
95
96 use Zeal::Feed;
97 my $feed = Zeal::Feed->new('http://example.com/feed.xml');
98 say $feed->version; # 12.2.3
99 say $feed->url; # http://another.example.com/file.tar.gz
100
101 # Download to /home/mgv/docsets/file.docset
102 $feed->download('/home/mgv/docsets/');
103
104=head1 DESCRIPTION
105
106Dash is an offline API documentation browser. Zeal::Feed is a class
107representing a Dash/Zeal documentation feed.
108
109A documentation feed is an XML file describing a docset. It contains
110the version of the docset and one or more URLs to a (typically
111.tar.gz) archive of the docset.
112
113Available methods:
114
115=over
116
117=item Zeal::Feed->B<new>(I<$url>)
118
119Create a Zeal::Feed object from an HTTP URL.
120
121=item Zeal::Feed->B<new_from_file>(I<$file>)
122
123Create a Zeal::Feed object from a file.
124
125=item Zeal::Feed->B<new_from_content>(I<$xml>)
126
127Create a Zeal::Feed object from a string.
128
129=item $feed->B<version>
130
131The version of this feed.
132
133=item $feed->B<urls>
134
135A list of URLs to this docset.
136
137=item $feed->B<url>
138
139An URL to this docset, randomly chosen from the list returned by B<urls>.
140
141=item $feed->B<download>(I<$path>)
142
143Download and unpack the docset inside the I<$path> directory.
144
145Uses the F<tar> binary for unpacking if availablem, L<Archive::Tar>
146otherwise. You can set the ZEAL_USE_INTERNAL_TAR environment variable
147to a true value to force the use of L<Archive::Tar>.
148
149=back
150
151=head1 ENVIRONMENT
152
153=over
154
155=item ZEAL_USE_INTERNAL_TAR
156
157If true, B<download> will always use L<Archive::Tar>.
158
159=back
160
161=head1 SEE ALSO
162
163L<Zeal>, L<http://kapeli.com/dash>, L<http://zealdocs.org>
164
165=head1 AUTHOR
166
167Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
168
169=head1 COPYRIGHT AND LICENSE
170
171Copyright (C) 2014 by Marius Gavrilescu
172
173This library is free software; you can redistribute it and/or modify
174it under the same terms as Perl itself, either Perl version 5.20.1 or,
175at your option, any later version of Perl 5 you may have available.
176
177
178=cut
This page took 0.019053 seconds and 4 git commands to generate.