Skip to content

Commit d77eaeb

Browse files
committed
Add { useragent => $ua } to parse() method
1 parent 3446a3f commit d77eaeb

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

ChangeLog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

7+
## [v1.0.0] - 2025-11-17
8+
9+
### Added
10+
11+
* Constructors (`new()` and `parse()`) accept a `{ useragent => $ua }` argument
12+
* New `useragent()` accessor/mutator
13+
714
## [0.65] - 2024-07-08
815

916
### Fixed

lib/XML/Feed.pm

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,48 @@ use Module::Pluggable search_path => "XML::Feed::Format",
1313
require => 1,
1414
sub_name => 'formatters';
1515

16-
our $VERSION = '0.65';
16+
our $VERSION = 'v1.0.0';
1717
our $MULTIPLE_ENCLOSURES = 0;
1818
our @formatters;
1919
BEGIN {
2020
@formatters = __PACKAGE__->formatters;
2121
}
2222

23-
sub new {
23+
sub _parse_args {
2424
my $class = shift;
25-
my $format = shift // 'Atom';
26-
27-
# Handle optional args hash
28-
my $args = {};
25+
my ($possible_args) = @_;
26+
2927
my @valid_args = qw[useragent];
28+
my $args = {};
3029

31-
if (@_ && ref($_[-1]) eq 'HASH') {
32-
my $possible_args = pop @_;
30+
for (@valid_args) {
31+
$args->{$_} = delete $possible_args->{$_}
32+
if exists $possible_args->{$_};
33+
}
3334

34-
for (@valid_args) {
35-
$args->{$_} = delete $possible_args->{$_}
36-
if exists $possible_args->{$_};
35+
unless (keys %$args) {
36+
Carp::croak('No valid keys passed to ' . __PACKAGE__ . 'new()');
3737
}
3838

39-
unless (keys %$args) {
40-
Carp::croak('No valid keys passed to ' . __PACKAGE__ . 'new()');
39+
# Validate useragent parameter if provided
40+
if (exists $args->{useragent}) {
41+
unless (blessed($args->{useragent}) && $args->{useragent}->isa('LWP::UserAgent')) {
42+
Carp::croak("useragent must be an LWP::UserAgent object");
4143
}
44+
}
4245

43-
# Validate useragent parameter if provided
44-
if (exists $args->{useragent}) {
45-
unless (blessed($args->{useragent}) && $args->{useragent}->isa('LWP::UserAgent')) {
46-
Carp::croak("useragent must be an LWP::UserAgent object");
47-
}
48-
}
46+
return $args;
47+
}
48+
49+
sub new {
50+
my $class = shift;
51+
my $format = shift // 'Atom';
52+
53+
# Handle optional args hash
54+
my $args = {};
55+
56+
if (@_ && ref($_[-1]) eq 'HASH') {
57+
$args = $class->_parse_args(pop @_);
4958
}
5059

5160
my $format_class = 'XML::Feed::Format::' . $format;
@@ -61,12 +70,29 @@ sub init_empty { 1 }
6170

6271
sub parse {
6372
my $class = shift;
64-
my($stream, $specified_format) = @_;
73+
my($stream) = @_;
6574
return $class->error("Stream parameter is required") unless $stream;
66-
my $feed = bless {}, $class;
75+
76+
my ($specified_format, $args) = (undef, {});
77+
78+
if (@_ == 3) {
79+
($specified_format, $args) = @_[1, 2];
80+
} elsif (@_ == 2) {
81+
if (ref($_[-1]) eq 'HASH') {
82+
$args = $_[1];
83+
} else {
84+
$specified_format = $_[1];
85+
}
86+
}
87+
88+
if (keys %$args) {
89+
$args = $class->_parse_args($args);
90+
}
91+
92+
my $feed = bless $args, $class;
6793
my $xml = '';
6894
if (blessed($stream) and $stream->isa('URI')) {
69-
$xml = $feed->get_uri($stream);
95+
$xml = $feed->get_uri($stream);
7096
} elsif (ref($stream) eq 'SCALAR') {
7197
$xml = $$stream;
7298
} elsif (ref($stream)) {
@@ -343,8 +369,12 @@ If not provided, a default UserAgent will be created when needed.
343369
344370
=head2 XML::Feed->parse($stream)
345371
372+
=head2 XML::Feed->parse($stream, \%args)
373+
346374
=head2 XML::Feed->parse($stream, $format)
347375
376+
=head2 XML::Feed->parse($stream, $format, \%args)
377+
348378
Parses a syndication feed identified by I<$stream> and returns an
349379
I<XML::Feed> object. I<$stream> can be any
350380
one of the following:
@@ -371,6 +401,23 @@ A URI from which the feed XML will be retrieved.
371401
372402
I<$format> allows you to override format guessing.
373403
404+
An optional hashref of arguments can be provided as the last parameter:
405+
406+
my $ua = LWP::UserAgent->new;
407+
$ua->timeout(30);
408+
$feed = XML::Feed->parse('https://example.com/feed', { useragent => $ua });
409+
410+
Currently supported arguments:
411+
412+
=over 4
413+
414+
=item * useragent
415+
416+
An L<LWP::UserAgent> object (or subclass) to use for fetching feeds via URI.
417+
If not provided, a default UserAgent will be created when needed.
418+
419+
=back
420+
374421
=head2 XML::Feed->get_file($filename)
375422
376423
Gets a feed from a file.

0 commit comments

Comments
 (0)