Changeset 525

Show
Ignore:
Timestamp:
10/08/08 13:56:37 (3 months ago)
Author:
ogawa
Message:

Add accessors for user_api_key and forum_api_key.
Make API methods check whether api_keys are defined or not.
Add several helper methods.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • WWW-Disqus/trunk/lib/WWW/Disqus.pm

    r524 r525  
    44use warnings; 
    55 
     6use base qw( Class::Accessor::Fast ); 
    67use Carp; 
    78use LWP::UserAgent; 
     
    1718    $param{ua}   ||= LWP::UserAgent->new; 
    1819    $param{json} ||= JSON->new; 
    19     confess 'user_api_key must be defined' 
    20       unless defined $param{user_api_key}; 
    2120    bless \%param, $class; 
    2221} 
    2322 
     23__PACKAGE__->mk_accessors(qw( user_api_key forum_api_key )); 
     24 
     25## Helper methods 
    2426sub get_forum_id { 
    2527    my $this         = shift; 
     
    3335} 
    3436 
    35 sub get_forum_list { 
     37sub set_forum_api_key_by_forum_id { 
     38    my $this = shift; 
     39    my ($forum_id) = @_; 
     40    if ( my $key = $this->get_forum_api_key($forum_id) ) { 
     41        $this->forum_api_key($key); 
     42    } 
     43} 
     44 
     45sub set_forum_api_key_by_forum_name { 
    3646    my $this = shift; 
    3747    my ($forum_name) = @_; 
    38     my $res = 
    39       $this->{ua}->get( DISQUS_API_URL 
    40           . "get_forum_list/?user_api_key=" 
    41           . $this->{user_api_key} ); 
    42     confess 'Failed to access DISQUS API: ' . $res->status_line 
    43       unless $res->is_success; 
    44     my $obj = $this->{json}->jsonToObj( $res->content ); 
    45     return $obj->{message} if is_succeeded($obj); 
    46 } 
    47  
    48 sub get_forum_api_key { 
    49     my $this = shift; 
    50     my ($forum_id) = @_; 
    51     my $res = 
    52       $this->{ua}->get( DISQUS_API_URL 
    53           . "get_forum_api_key/?user_api_key=" 
    54           . $this->{user_api_key} 
    55           . "&forum_id=$forum_id" ); 
    56     confess 'Failed to access DISQUS API: ' . $res->status_line 
    57       unless $res->is_success; 
    58     my $obj = $this->{json}->jsonToObj( $res->content ); 
    59     return $obj->{message} if is_succeeded($obj); 
    60 } 
    61  
    62 sub get_forum_api { 
    63     my $this          = shift; 
    64     my ($forum_id)    = @_; 
    65     my $forum_api_key = $this->get_forum_api_key($forum_id); 
    66     WWW::Disqus::Forum->new( forum_api_key => $forum_api_key ); 
     48    if ( my $forum_id = $this->get_forum_id($forum_name) ) { 
     49        if ( my $key = $this->get_forum_api_key($forum_id) ) { 
     50            $this->forum_api_key($key); 
     51        } 
     52    } 
    6753} 
    6854 
     
    7662} 
    7763 
    78 1; 
    79  
    80 package WWW::Disqus::Forum; 
    81  
    82 use strict; 
    83 use warnings; 
    84  
    85 use Carp; 
    86 use LWP::UserAgent; 
    87 use JSON 1.1; 
    88  
    89 use constant DISQUS_API_URL => 'http://disqus.com/api/'; 
    90  
    91 sub new { 
    92     my $class = shift; 
    93     my (%param) = @_; 
    94     $param{ua}   ||= LWP::UserAgent->new; 
    95     $param{json} ||= JSON->new; 
    96     confess 'forum_api_key must be defined' 
    97       unless defined $param{forum_api_key}; 
    98     bless \%param, $class; 
    99 } 
    100  
     64## API methods (using user_api_key) 
     65sub get_forum_list { 
     66    my $this         = shift; 
     67    my $user_api_key = $this->user_api_key 
     68      or carp 'user_api_key must be set.'; 
     69    my ($forum_name) = @_; 
     70    my $res = 
     71      $this->{ua} 
     72      ->get( DISQUS_API_URL . "get_forum_list/?user_api_key=" . $user_api_key ); 
     73    confess 'Failed to access DISQUS API: ' . $res->status_line 
     74      unless $res->is_success; 
     75    my $obj = $this->{json}->jsonToObj( $res->content ); 
     76    return $obj->{message} if is_succeeded($obj); 
     77} 
     78 
     79sub get_forum_api_key { 
     80    my $this         = shift; 
     81    my $user_api_key = $this->user_api_key 
     82      or carp 'user_api_key must be set.'; 
     83    my ($forum_id) = @_; 
     84    my $res = 
     85      $this->{ua}->get( DISQUS_API_URL 
     86          . "get_forum_api_key/?user_api_key=" 
     87          . $user_api_key 
     88          . "&forum_id=" 
     89          . $forum_id ); 
     90    confess 'Failed to access DISQUS API: ' . $res->status_line 
     91      unless $res->is_success; 
     92    my $obj = $this->{json}->jsonToObj( $res->content ); 
     93    return $obj->{message} if is_succeeded($obj); 
     94} 
     95 
     96## API methods (using forum_api_key) 
    10197sub create_post { 
    102     my $this  = shift; 
    103     my (%param) = @_; 
    104     my $res   = $this->{ua}->post( 
     98    my $this          = shift; 
     99    my $forum_api_key = $this->forum_api_key 
     100      or carp 'forum_api_key must be set.'; 
     101    my (%param) = @_; 
     102    my $res = $this->{ua}->post( 
    105103        DISQUS_API_URL . "create_post/", 
    106104        { 
    107             forum_api_key => $this->{forum_api_key}, 
     105            forum_api_key => $forum_api_key, 
    108106            %param, 
    109107        } 
     
    116114 
    117115sub get_thread_list { 
    118     my $this = shift; 
    119     my $res = 
    120       $this->{ua}->get( DISQUS_API_URL 
    121           . "get_thread_list/?forum_api_key=" 
    122           . $this->{forum_api_key} ); 
     116    my $this          = shift; 
     117    my $forum_api_key = $this->forum_api_key 
     118      or carp 'forum_api_key must be set.'; 
     119    my $res = 
     120      $this->{ua}->get( 
     121        DISQUS_API_URL . "get_thread_list/?forum_api_key=" . $forum_api_key ); 
    123122    confess 'Failed to access DISQUS API: ' . $res->status_line 
    124123      unless $res->is_success; 
     
    128127 
    129128sub get_num_posts { 
    130     my $this = shift; 
     129    my $this          = shift; 
     130    my $forum_api_key = $this->forum_api_key 
     131      or carp 'forum_api_key must be set.'; 
    131132    my ($thread_ids) = @_; 
    132133    my $res = 
    133134      $this->{ua}->get( DISQUS_API_URL 
    134135          . "get_num_posts/?forum_api_key=" 
    135           . $this->{forum_api_key} 
     136          . $forum_api_key 
    136137          . "&thread_ids=" 
    137138          . $thread_ids ); 
     
    143144 
    144145sub get_thread_by_url { 
    145     my $this = shift; 
     146    my $this          = shift; 
     147    my $forum_api_key = $this->forum_api_key 
     148      or carp 'forum_api_key must be set.'; 
    146149    my ($url) = @_; 
    147150    my $res = 
    148151      $this->{ua}->get( DISQUS_API_URL 
    149152          . "get_thread_by_url/?forum_api_key=" 
    150           . $this->{forum_api_key} 
     153          . $forum_api_key 
    151154          . "&url=$url" ); 
    152155    confess 'Failed to access DISQUS API: ' . $res->status_line 
     
    157160 
    158161sub get_thread_posts { 
    159     my $this = shift; 
     162    my $this          = shift; 
     163    my $forum_api_key = $this->forum_api_key 
     164      or carp 'forum_api_key must be set.'; 
    160165    my ($thread_id) = @_; 
    161166    my $res = 
    162167      $this->{ua}->get( DISQUS_API_URL 
    163168          . "get_thread_posts/?forum_api_key=" 
    164           . $this->{forum_api_key} 
     169          . $forum_api_key 
    165170          . "&thread_id=" 
    166171          . $thread_id ); 
     
    173178# XXX 
    174179sub thread_by_identifier { 
    175     my $this  = shift; 
    176     my (%param) = @_; 
    177     my $res   = $this->{ua}->post( 
     180    my $this          = shift; 
     181    my $forum_api_key = $this->forum_api_key 
     182      or carp 'forum_api_key must be set.'; 
     183    my (%param) = @_; 
     184    my $res = $this->{ua}->post( 
    178185        DISQUS_API_URL . "thread_by_identifier/", 
    179186        { 
    180             forum_api_key => $this->{forum_api_key}, 
     187            forum_api_key => $forum_api_key, 
    181188            %param, 
    182189        } 
     
    189196 
    190197sub update_thread { 
    191     my $this  = shift; 
    192     my (%param) = @_; 
    193     my ( $thread_id, $title, $slug ) = @_; 
     198    my $this          = shift; 
     199    my $forum_api_key = $this->forum_api_key 
     200      or carp 'forum_api_key must be set.'; 
     201    my (%param) = @_; 
    194202    my $res = $this->{ua}->post( 
    195203        DISQUS_API_URL . "update_thread/", 
    196204        { 
    197             forum_api_key => $this->{forum_api_key}, 
     205            forum_api_key => $forum_api_key, 
    198206            %param, 
    199207        } 
     
    205213} 
    206214 
    207 sub is_succeeded { 
    208     my $obj = shift; 
    209     return 1 
    210       if $obj->{succeeded} 
    211           && $obj->{succeeded}->{value} 
    212           && $obj->{succeeded}->{value} eq 'true'; 
    213     confess $obj->{message}; 
    214 } 
    215  
    2162151;