OSDN Git Service

split Common class and change to use it on FB class
authorISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Wed, 31 Oct 2012 16:53:24 +0000 (01:53 +0900)
committerISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
Wed, 31 Oct 2012 16:53:24 +0000 (01:53 +0900)
add Queue class

lib/Mubot4FB.pm
lib/Mubot4FB/Common.pm [new file with mode: 0644]
lib/Mubot4FB/FB.pm
lib/Mubot4FB/Queue.pm [new file with mode: 0644]

index b9a4b70..9ea9c28 100644 (file)
@@ -12,6 +12,7 @@ use POSIX 'strftime';
 
 use Mubot4FB::DB;
 use Mubot4FB::FB;
+use Mubot4FB::Queue;
 
 use Data::Dumper;
 
@@ -31,6 +32,7 @@ sub init {
        my ($me) = @_;
        $me->{fb} = Mubot4FB::FB->new($me->{cfg});
        $me->{db} = Mubot4FB::DB->new($me->{cfg});
+       $me->{queue} = Mubot4FB::Queue->new($me->{cfg}, $me->{db}->{dbh});
        $me->misc_init;
        return 1;
 }
@@ -76,11 +78,18 @@ sub _add {
 
                        if ($post_ok) {
                                my (undef, $post_id) = split(/_/, $resp->{id});
-                               $me->{db}->add({submitter => $args->{who},
-                                               fb_post_id => $post_id,
-                                               uri => $uri,
-                                               prefix => $prefix,
-                                               comment => $comment});
+                               my $db_args = {submitter => $args->{who},
+                                              fb_post_id => $post_id,
+                                              uri => $uri,
+                                              prefix => $prefix,
+                                              comment => $comment,
+                                              _command => 'add'};
+                               $me->{db}->add($db_args);
+                               if (defined $me->{cfg}->{queues}) {
+                                       foreach my $funcname (@{$me->{cfg}->{queues}}) {
+                                               $me->{queue}->insert($funcname, $db_args);
+                                       }
+                               }
                                $resp_msg = $args->{who} . ': うい  '.$me->{fb}->post_uri($post_id).' で登録';
                        } else {
                                $resp_msg = 'can not post to facebook';
@@ -114,6 +123,11 @@ sub _remove {
                # fb 側のエントリを削除しないといけない
                if ($me->{fb}->remove($post_id)) {
                        $me->{db}->commit;
+                       if (defined $me->{cfg}->{queues}) {
+                               foreach my $funcname (@{$me->{cfg}->{queues}}) {
+                                       $me->{queue}->insert($funcname, {_command => 'remove', post_id => $post_id});
+                               }
+                       }
                        $resp_msg = $args->{who} . ': 削除しました ' . $me->{fb}->post_uri($post_id);
                } else {
                        $me->{db}->rollback;
diff --git a/lib/Mubot4FB/Common.pm b/lib/Mubot4FB/Common.pm
new file mode 100644 (file)
index 0000000..e48d9a8
--- /dev/null
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+#
+# Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
+# This program is covered by the GNU General Public License 2
+#
+package Mubot4FB::Common;
+
+use strict;
+use utf8;
+
+use Data::Dumper;
+
+sub new {
+       my ($proto, $cfg) = @_;
+       my $class = ref $proto || $proto;
+       my $self = {cfg => $cfg};
+       bless $self, $class;
+
+       $self->init();
+       return $self;
+}
+
+sub init {
+       return 1;
+}
+
+sub post_uri {
+       my ($me, $post_id) = @_;
+
+       return $me->{cfg}->{fb_page_url} . '/posts/' . $post_id;
+}
+
+1;
index bb99490..5cdd89e 100644 (file)
@@ -12,17 +12,9 @@ use LWP::UserAgent;
 use HTTP::Request::Common;
 use URI::Split qw(uri_split uri_join);
 
-use Data::Dumper;
-
-sub new {
-       my $proto = shift;
-       my $class = ref $proto || $proto;
-       my $self = {cfg => shift};
-       bless $self, $class;
+use base 'Mubot4FB::Common';
 
-       $self->init();
-       return $self;
-}
+use Data::Dumper;
 
 sub init {
        my ($me) = @_;
@@ -47,7 +39,6 @@ sub init {
        return $me->{fbo} = Facebook::Graph->new(access_token => $page_access_token);
 }
 
-
 sub uri_fixer {
        my ($me, $uri) = @_;
 
@@ -75,12 +66,6 @@ sub publish {
            ->as_hashref();
 }
 
-sub post_uri {
-       my ($me, $post_id) = @_;
-
-       return $me->{cfg}->{fb_page_url} . '/posts/' . $post_id;
-}
-
 sub remove {
        my ($me, $post_id) = @_;
 
diff --git a/lib/Mubot4FB/Queue.pm b/lib/Mubot4FB/Queue.pm
new file mode 100644 (file)
index 0000000..6854355
--- /dev/null
@@ -0,0 +1,36 @@
+#!/usr/bin/perl
+#
+# Copyright (c) 2012 ISHIKAWA Mutsumi <ishikawa@hanzubon.jp>
+# This program is covered by the GNU General Public License 2
+#
+package Mubot4FB::Queue;
+use strict;
+use utf8;
+
+use TheSchwartz::Simple;
+
+use Data::Dumper;
+
+sub new {
+       my ($proto, $cfg, $dbh) = @_;
+       my $class = ref $proto || $proto;
+       my $self = {cfg => $cfg, dbh => $dbh};
+       bless $self, $class;
+
+       $self->init();
+       return $self;
+}
+
+sub init {
+       my ($me) = @_;
+
+       return $me->{queue} = TheSchwartz::Simple->new([ $me->{dbh} ]);
+}
+
+sub insert {
+       my ($me, $funcname, $arg) = @_;
+
+       $me->{queue}->insert($funcname, $arg);
+}
+
+1;