App-Chart

 view release on metacpan or  search on metacpan

lib/App/Chart/IntradayHandler.pm  view on Meta::CPAN

sub handlers_for_symbol {
  my ($class, $symbol) = @_;
  App::Chart::symbol_setups ($symbol);
  return grep { $_->{'pred'}->match($symbol) } @handler_list;
}

sub handler_for_symbol_and_mode {
  my ($class, $symbol, $mode) = @_;
  my @found
    = grep {$_->{'mode'} eq $mode} $class->handlers_for_symbol ($symbol);
  return $found[0];
}

sub download {
  my ($self, $symbol) = @_;
  ### IntradayHandler download: $symbol
  my ($image, $error, $resp);

  my $mode = $self->{'mode'};
  my $proc = $self->{'proc'};
  my ($url, %options);
  if (! eval { ($url, %options) = $proc->($self, $symbol, $mode); 1 }) {
    # mainly errors downloading procs like Barchart.pm
    $error = $@;
  } else {
    require App::Chart::Download;
    require App::Chart::Intraday;

    App::Chart::Download::status
        (__x('Intraday image {symbol} {mode}',
             symbol => $symbol,
             mode   => $mode));
    App::Chart::Download::verbose_message ("Intraday image", $url);

    require App::Chart::UserAgent;
    my $ua = App::Chart::UserAgent->instance;

    if (exists $options{'cookie_jar'}) {
      ### IntradayHandler cookie_jar: $options{'cookie_jar'}->as_string
      $ua->cookie_jar ($options{'cookie_jar'});
    } else {
      $ua->cookie_jar ({});
    }

    require HTTP::Request;
    my @headers = (Referer => $options{'referer'});
    my $req = HTTP::Request->new ('GET', $url, \@headers);
    $ua->prepare_request ($req);
    ### IntradayHandler request: $req->as_string

    my $resp = $ua->request
      ($req,
       sub {
         my ($chunk, $resp, $protobj) = @_;
         $resp->add_content($chunk);

         # if the message is deflate/gzip/etc compressed then decoded_content
         # returns undef until the whole image -- but don't worry about that
         # until we get a server sending us compressed images
         #
         my $image = $resp->decoded_content(charset=>'none');
         if ($image && length $image >= 256) {
           App::Chart::Intraday::write_intraday_image (symbol => $symbol,
                                                       mode   => $mode,
                                                       image  => $image);
         }
       });

    if ($resp->is_success) {
      $image = $resp->decoded_content(charset=>'none',raise_error=>1);
    } else {
      $error = __x('Error: {status_line}',
                   status_line => $resp->status_line);
    }
  }
  require App::Chart::Intraday;
  App::Chart::Intraday::write_intraday_image (symbol => $symbol,
                                              mode   => $mode,
                                              image  => $image,
                                              resp   => $resp,
                                              error  => $error);
  ### response: $resp->status_line
}


sub name_sans_mnemonic {
  my ($self) = @_;
  my $name = $self->{'name'};
  $name =~ s/_//;
  return $name;
}

sub name_as_markup {
  my ($self) = @_;
  my $name = $self->{'name'};
  $name =~ s{_(.)}{<u>$1</u>};
  return $name;
}
sub name_mnemonic_key {
  my ($self) = @_;
  my $name = $self->{'name'};
  $name =~ s/__//;
  return ($name =~ /_(.)/ && $1);
}

1;
__END__

# =for stopwords intraday
# 
# =head1 NAME
# 
# App::Chart::IntradayHandler -- intraday download handlers
# 
# =for test_synopsis my (@handlers)
# 
# =head1 SYNOPSIS
# 
#  use App::Chart::IntradayHandler;
# 
#  # register new
#  App::Chart::IntradayHandler->new ();
# 
#  # find
#  @handlers = App::Chart::IntradayHandler->handlers_for_symbol ('GM');
# 
# =head1 FUNCTIONS
# 
# =over 4
# 



( run in 0.556 second using v1.01-cache-2.11-cpan-39bf76dae61 )