Changeset 507

Show
Ignore:
Timestamp:
08/28/08 19:47:20 (3 months ago)
Author:
ogawa
Message:

Refactoring API interfaces more clearly.
Add calendar_driver option.
Remove cybozu_version option.

Location:
cybozu2ical/trunk
Files:
3 added
1 removed
3 modified

Legend:

Unmodified
Added
Removed
  • cybozu2ical/trunk/config.yaml.sample

    r502 r507  
    11cybozu_url: http://www.example.com/cbag/ag.cgi 
    2 cybozu_version: 7 
    32calname: Your Calendar Name 
    43username: user 
     
    98input_encoding: shiftjis 
    109output_encoding: utf8 
     10#calendar_driver: SyncCalendar 
     11calendar_driver: ApiCalendar 
    1112date_range: 30 
  • cybozu2ical/trunk/cybozu2ical

    r506 r507  
    1313use Data::ICal::Entry::TimeZone::Standard; 
    1414use DateTime; 
     15use WWW::CybozuOffice6::Calendar; 
    1516use URI; 
    1617use Pod::Usage; 
     
    109110 
    110111# Obtain Cybozu Office 6/7 Calendar items 
    111 my $cal_class = 
    112   $cfg->{cybozu_version} == 7 
    113   ? 'WWW::CybozuOffice7::Calendar' 
    114   : 'WWW::CybozuOffice6::Calendar'; 
    115 eval "use $cal_class;"; 
    116 my $cal = $cal_class->new(%$cfg); 
     112my $cal = WWW::CybozuOffice6::Calendar->new(%$cfg); 
    117113 
    118114if ( $opt{'input-csv'} ) { 
  • cybozu2ical/trunk/lib/WWW/CybozuOffice6/Calendar.pm

    r506 r507  
    1515 
    1616sub new { 
    17     my ( $class, %param ) = @_; 
    18     $param{url} ||= delete $param{cybozu_url}; 
    19     bless \%param, $class; 
     17    my $class   = shift; 
     18    my (%param) = @_; 
     19    my $cal     = bless \%param, $class; 
     20    $cal->{url} ||= delete $cal->{cybozu_url}; 
     21    $cal->driver_init; 
     22    $cal; 
     23} 
     24 
     25sub driver_init { 
     26    my $cal = shift; 
     27    my $driver = $cal->{calendar_driver} || 'ApiCalendar'; 
     28    $driver = 'WWW::CybozuOffice6::CalendarDriver::' . $driver 
     29      if $driver !~ m/^WWW::CybozuOffice6::CalendarDriver::/; 
     30    eval "use $driver;"; 
     31    $cal->{calendar_driver} = $driver; 
     32    $cal; 
    2033} 
    2134 
     
    2437sub request { 
    2538    my $cal = shift; 
    26     my $date_range = $cal->{date_range} || 30; 
    27  
    28     my $ua         = LWP::UserAgent->new; 
    29     my $auth_param = { 
    30         _System => 'login', 
    31         _Login  => 1, 
    32         defined $cal->{username} ? ( _Account => $cal->{username} ) : (), 
    33         defined $cal->{userid}   ? ( _Id      => $cal->{userid} )   : (), 
    34         Password => $cal->{password} || '', 
    35     }; 
    36  
    37     my $res = $ua->post( $cal->{url} . '?page=SyncCalendar', $auth_param ); 
    38     confess 'Failed to access SyncCalendar API: ' . $res->status_line 
    39       unless $res->is_success; 
    40  
    41     my $content = $res->content; 
    42     from_to( $content, $cal->{input_encoding} || 'shiftjis', 'utf8' ); 
    43     my @lines = grep /^\d+,ts\.\d+,/, split( /\r?\n/, $content ); 
    44     $cal->{response} = \@lines; 
    45  
    46     scalar @lines ? \@lines : undef; 
     39    $cal->{calendar_driver}->request($cal); 
    4740} 
    4841 
     
    7063sub get_items { 
    7164    my $cal = shift; 
    72  
    73     my $csv; 
    74     if ( eval('require Text::CSV_XS') ) { 
    75         $csv = Text::CSV_XS->new( { binary => 1 } ); 
    76     } 
    77     elsif ( eval('require Text::CSV') ) { 
    78         $csv = Text::CSV->new(); 
    79     } 
    80     else { 
    81         confess 'Text::CSV_XS or Text::CSV package is required'; 
    82     } 
    83  
    84     my @items; 
    85     for my $line ( $cal->response ) { 
    86         $csv->parse($line) 
    87           or confess 'Failed to parse CSV input'; 
    88         my @fields     = $csv->fields; 
    89         my $num_fields = @fields - 1; 
    90         next if $num_fields < 13; 
    91         $fields[1] =~ s/^ts\.//;    # remove rubbish 
    92  
    93         # Cybozu Calendar CSV Format 
    94         #      GENERIC     | RECCURENT 
    95         # [ 0] id?         | id? 
    96         # [ 1] created     | created 
    97         # [ 2] <BLANK>     x start_date / end_date 
    98         # [ 3] start_date  x initial start_date? 
    99         # [ 4] end_date    x until_date 
    100         # [ 5] start_time  | start_time 
    101         # [ 6] end_time    | end_time 
    102         # [ 7] <BLANK>     | freq 
    103         # [ 8] <BLANK>     | freq_value 
    104         # [ 9] ???         | ??? 
    105         # [10] ???         | ??? 
    106         # [11] abbrev      | abbrev 
    107         # [12] summary     | summary 
    108         # [13] description | description 
    109  
    110         my %param; 
    111         @param{ 
    112             qw(id created start_time end_time freq freq_value abbrev summary description) 
    113           } = @fields[ 0, 1, 5 .. 8, 11 .. 13 ]; 
    114         $param{time_zone} = $cal->{time_zone} || 'Asia/Tokyo'; 
    115  
    116         my $item; 
    117         if ( !$param{freq} ) { 
    118             @param{qw(start_date end_date)} = @fields[ 3, 4 ]; 
    119             $item = WWW::CybozuOffice6::Calendar::Event->new(%param); 
    120         } 
    121         else { 
    122             @param{qw(start_date end_date until_date)} = @fields[ 2, 2, 4 ]; 
    123             if ( $num_fields > 13 ) { 
    124                 my @exdates = @fields[ 14 .. $num_fields ]; 
    125                 $param{exdates} = \@exdates; 
    126             } 
    127             my $freq = $param{freq}; 
    128             if ( $freq =~ /^[1-5]$/ ) { 
    129                 $param{freq} = 'm'; 
    130                 my @week_str = ( 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA' ); 
    131                 $param{freq_value} = $freq . $week_str[ $param{freq_value} ]; 
    132             } 
    133             $item = WWW::CybozuOffice6::Calendar::RecurrentEvent->new(%param); 
    134         } 
    135  
    136         next unless $item; 
    137         $item->comment($line);    # save the CSV line as for debug info. 
    138         push @items, $item; 
    139     } 
    140     wantarray ? @items : $items[0]; 
     65    $cal->{calendar_driver}->get_items($cal); 
    14166} 
    14267