our
$VERSION
= 0.04;
has
description
=>
'Find fresh donuts near you!'
;
has
usage
=>
<<EOF;
USAGE:
mojo donuts [OPTIONS]
OPTIONS:
--geo, -g Specify geo to be used for search (recommended)
--fresh, -f Filter results by stores serving fresh donuts
--raw, -r Print raw data structure returned from search response
EOF
has
ua
=>
sub
{ Mojo::UserAgent->new() };
has
[
qw( geo fresh raw )
];
sub
_mk_request {
my
(
$self
,
$url
,
$form
) =
@_
;
my
$tx
=
$form
?
$self
->ua->get(
$url
) :
$self
->ua->post(
$url
=>
form
=>
$form
);
unless
(
$tx
->success) {
my
$err
=
$tx
->error;
die
join
(
''
,
'Trouble finding IP address.'
,
'If problem persists, you might have to provide geo.'
,
"$url error code and response:"
,
$err
->{code},
$err
->{response},
);
}
return
$tx
->res;
}
sub
_ip2geo {
my
(
$self
,
$ip
) =
@_
;
->dom->find(
'div#data_display > table.table > tr > td'
)
->
grep
(
sub
{
$_
->text =~ /\b(?:Latitude|Longitude)\b/; })
->
map
(
sub
{
$_
->following->first->text })->to_array;
die
'Failed to scape geo from geocodeip.com. You may need to provide geo instead.'
unless
$geo
&&
$geo
->[0]
&&
$geo
->[1];
return
$geo
;
}
sub
_geocode_ip {
my
$self
=
shift
;
return
if
$self
->geo;
return
$self
->_ip2geo(
$ip
);
}
sub
run {
my
(
$self
,
@args
) =
@_
;
GetOptionsFromArray(
\
@args
,
'geo|g=s'
=>
sub
{
$self
->geo([
split
','
,
$_
[1]]) },
'fresh|f'
=>
sub
{
$self
->fresh(1) },
'raw|r'
=>
sub
{
$self
->raw(1) },
);
$self
->geo(
$self
->_geocode_ip)
unless
$self
->geo;
my
$donuts
= WWW::KrispyKreme::HotLight->new(
where
=>
$self
->geo)->locations;
my
@locations
=
grep
{
$self
->fresh ?
$_
->{Hotlight} : 1 }
@$donuts
;
if
(
$self
->raw) {
say
dumper \
@locations
;
exit
1;
}
for
my
$loc
(
@locations
) {
my
$addr
=
join
', '
,
grep
{ s/\s+$//; 1 }
$loc
->{Address1},
$loc
->{City},
$loc
->{Province};
$addr
.=
' [HOTLIGHT ON]'
if
$loc
->{Hotlight};
say
$addr
;
}
}
1;