Changeset 447 for cybozu2ical/trunk/lib

Show
Ignore:
Timestamp:
10/16/07 17:33:37 (15 months ago)
Author:
ogawa
Message:

Add --input-csv and --output-csv options.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cybozu2ical/trunk/lib/WWW/CybozuOffice6/Calendar.pm

    r446 r447  
    3535} 
    3636 
    37 sub get_items { 
    38     my $this = shift; 
    39  
    40     my $res = $this->_request(); 
     37sub request { 
     38    my $this = shift; 
     39 
     40    my $res = $this->{ua}->post($this->{url} . '?page=SyncCalendar', { 
     41        _System    => 'login', 
     42        _Login     => 1, 
     43        csv        => 1, 
     44        notimecard => 1, 
     45        defined $this->{username} ? (_Account => $this->{username}) : (), 
     46        defined $this->{userid}   ? (_Id      => $this->{userid}  ) : (), 
     47        Password   => $this->{password} || '', 
     48    }); 
    4149    die 'Failed to access Cybozu Office 6: ' . $res->status_line 
    4250        unless $res->is_success; 
     
    4553    from_to($content, $this->{input_encoding} || 'shiftjis', 'utf8'); 
    4654    my @lines = grep /^\d+,ts\.\d+,/, split(/\r?\n/, $content); 
     55    $this->{response} = \@lines; 
     56 
     57    scalar @lines ? \@lines : undef; 
     58} 
     59 
     60sub read_from_csv_file { 
     61    my $this = shift; 
     62    my ($file) = @_; 
     63    local *FH; 
     64    open FH, $file or die "Failed to read $file"; 
     65    my @lines; 
     66    while (<FH>) { 
     67        chomp; push @lines, $_; 
     68    } 
     69    close(FH); 
     70    $this->{response} = \@lines; 
     71 
     72    scalar @lines ? \@lines : undef; 
     73} 
     74 
     75sub response { 
     76    my $res = $_[0]->{response} || {}; 
     77    wantarray ? @$res : $res; 
     78} 
     79 
     80sub get_items { 
     81    my $this = shift; 
    4782 
    4883    my @items; 
    4984    my $csv = Text::CSV_XS->new({ binary => 1 }); 
    50     for my $line (@lines) { 
     85    for my $line ($this->response) { 
    5186        $csv->parse($line) 
    5287            or die 'Failed to parse CSV input'; 
     
    103138} 
    104139 
    105 sub _request { 
    106     my $this = shift; 
    107     $this->{ua}->post($this->{url} . '?page=SyncCalendar', { 
    108         _System    => 'login', 
    109         _Login     => 1, 
    110         csv        => 1, 
    111         notimecard => 1, 
    112         defined $this->{username} ? (_Account => $this->{username}) : (), 
    113         defined $this->{userid}   ? (_Id      => $this->{userid}  ) : (), 
    114         Password   => $this->{password} || '', 
    115     }); 
    116 } 
    117  
    118140package WWW::CybozuOffice6::Calendar::Event; 
    119141 
     
    268290  ); 
    269291 
     292  # request calendar contents 
     293  $calendar->request(); 
     294 
    270295  # get list of items in the calendar 
    271296  my @items = $calendar->get_items(); 
     
    346371Gets/sets the Cybozu Office 6 encoding. 
    347372 
     373=item request() 
     374 
     375Requests to obtain the contents of Cybozu Office 6 Calendar. 
     376 
     377=item read_from_csv_file($filename) 
     378 
     379Instead of requesting Cybozu Office 6 server, reads from a local CSV file. 
     380 
    348381=item get_items() 
    349382