<commit_before>package BT::NextRide;

use Moo;
use strictures 2;
use WWW::Mechanize;
use Mojo::DOM;
use namespace::clean;

my $NEXT_STOP_PAGE_URL = 'http://nextride.brampton.ca/mob/SearchBy.aspx';

has _stop => (
    is => 'rw',
);

has _has_cache => (
    is => 'rw',
    default => sub { 0 },
);

has _mech => (
    is => 'ro',
    default => sub {
        WWW::Mechanize->new(
            agent => 'Mozilla/5.0 (X11; Linux i686; rv:25.2) Gecko'
                . '/20150112 Firefox/31.9 PaleMoon/25.2.0',
            timeout => 30,
        );
    },
);

sub init {
    my $self = shift;
    $self->_mech->get($NEXT_STOP_PAGE_URL);
    $self->_has_cache(1);
    return $self;
}

sub set_stop {
    my ( $self, $stop ) = @_;
    $self->_stop( $stop );
    return $self;
}

sub get_next_ride {
    my $self = shift;
    my $mech = $self->_mech;
return qw/Mock output when busses aren't running/;
    $self->init
        unless $self->_has_cache;

    $mech->form_number(1);
    $mech->set_fields(
        'ctl00$mainPanel$searchbyStop$txtStop' => $self->_stop,
    );
    $mech->click_button( name => 'ctl00$mainPanel$btnGetRealtimeSchedule' );

    # return qw/Mock output when busses aren't running/;

    my @times = grep length,
        Mojo::DOM->new( $mech->content )
        ->find('td + td')->map('text')->each;

    return @times ? @times : 'No service';
}


1;<commit_msg>Fixes #1: return meaningful error for not found stops<commit_after>package BT::NextRide;

use Moo;
use strictures 2;
use WWW::Mechanize;
use Mojo::DOM;
use namespace::clean;

my $NEXT_STOP_PAGE_URL = 'http://nextride.brampton.ca/mob/SearchBy.aspx';

has _stop => (
    is => 'rw',
);

has _has_cache => (
    is => 'rw',
    default => sub { 0 },
);

has _mech => (
    is => 'ro',
    default => sub {
        WWW::Mechanize->new(
            agent => 'Mozilla/5.0 (X11; Linux i686; rv:25.2) Gecko'
                . '/20150112 Firefox/31.9 PaleMoon/25.2.0',
            timeout => 30,
        );
    },
);

sub init {
    my $self = shift;
    $self->_mech->get($NEXT_STOP_PAGE_URL);
    $self->_has_cache(1);
    return $self;
}

sub set_stop {
    my ( $self, $stop ) = @_;
    $self->_stop( $stop );
    return $self;
}

sub get_next_ride {
    my $self = shift;
    my $mech = $self->_mech;
# return qw/Mock output when busses aren't running/;
    $self->init
        unless $self->_has_cache;

    $mech->form_number(1);
    $mech->set_fields(
        'ctl00$mainPanel$searchbyStop$txtStop' => $self->_stop,
    );
    $mech->click_button( name => 'ctl00$mainPanel$btnGetRealtimeSchedule' );

    # return qw/Mock output when busses aren't running/;

    return 'Stop not found'
        if $mech->content =~ /No stop found for given stop code/;

    my @times = grep length,
        Mojo::DOM->new( $mech->content )
        ->find('td + td')->map('text')->each;

    return @times ? @times : 'No service';
}


1;
{'fraction_non_alphanumeric': 0.17063081695966908, 'fraction_numerical': 0.025163736642537057, 'mean_word_length': 2.5563725490196076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1721719', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1073, 'n_words': 240}