lib/App/SocialSKK.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/App/SocialSKK/Plugin/HatenaBookmark.pm ↑ Summary lib/App/SocialSKK/Base.pm »
1 package App::SocialSKK;
2 2 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use LWP::UserAgent::POE;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use base qw(App::SocialSKK::Base);
9  
10 use App::SocialSKK::Protocol;
11  
12 our $VERSION = '0.02';
13  
14 __PACKAGE__->mk_accessors(qw(
15 config
16 hostname address port
17 ua
18 protocol
19 plugins
20 ));
21  
22 sub new {
23 my $class = shift;
24 my $self = $class->SUPER::new(@_);
25 $self->init;
26 }
27  
28 sub init {
29 my $self = shift;
30 $self->config = {} if !$self->config;
31 $self->plugins = [] if !$self->plugins;
32  
33 my %ua_options = ref $self->config->{ua_options} eq 'HASH' ? %{$self->config->{ua_options}} : ();
34 $self->ua ||= LWP::UserAgent::POE->new(
35 timeout => 5,
36 agent => $self->get_version,
37 %ua_options,
38 );
39  
40 $self->load_plugins;
41 $self->protocol = App::SocialSKK::Protocol->new;
42 $self->protocol->on_get_version = sub { $self->get_version };
43 $self->protocol->on_get_serverinfo = sub { $self->get_serverinfo };
44 $self->protocol->on_get_candidates = sub { $self->get_candidates(shift) };
45 $self;
46 }
47  
48 sub load_plugins {
49 my $self = shift;
50 my $prefix = sprintf '%s::Plugin', __PACKAGE__;
51  
52 for my $plugin (@{$self->config->{plugins}}) {
53 my $module = join '::', $prefix, $plugin->{name};
54 $module->use or croak(qq{Couldn't load plugin: $module});
55 my %config = ref $plugin->{config} eq 'HASH' ? %{$plugin->{config}} : ();
56 push @{$self->plugins}, $module->new({(ua => $self->ua, %config)});
57 }
58 }
59  
60 sub get_version {
61 sprintf '%s/%s ' , __PACKAGE__, $VERSION;
62 }
63  
64 sub get_serverinfo {
65 my $self = shift;
66 sprintf '%s:%s: ', $self->hostname, $self->address;
67 }
68  
69 sub get_candidates {
70 my ($self, $text) = @_;
71 return if !defined $text;
72 $text =~ s/\s*$//g;
73  
74 my @candidates;
75 for my $plugin (@{$self->plugins}) {
76 push @candidates, $plugin->get_candidates($text);
77 }
78 if (@candidates) {
79 join '/', (1, @candidates, "\n")
80 }
81 else {
82 sprintf '4%s ', $text;
83 }
84 }
85  
86 1;
87  
88 __END__
89  
90 =head1 NAME
91  
92 App::SocialSKK - SKK Goes Social
93  
94 =head1 SYNOPSIS
95  
96 use App::SocialSKK;
97  
98 my $social_skk = App::SocialSKK->new({
99 plugins => [
100 { name => 'SocialIME' },
101 ],
102 });
103 my $candidates = $social_skk->get_candidates($text);
104  
105 =head1 DESCRIPTION
106  
107 App::SocialSKK provides a internal works for socialskk.pl.
108  
109 This module is basically designed to perform searches against Social
110 IME. Besides, it has pluggable mechanism, you can add other more data
111 sources into it as you like. This distribution actually provides some
112 plugins, for example, to retrieve candidates from Wikipedia suggest
113 API.
114  
115 You might want to consult the documentation of sociallskk.pl directly
116 if you're not interested in the internal of this distribution'.
117  
118 =head1 METHODS
119  
120 =head2 new ( I<\%options> )
121  
122 =over 4
123  
124 my $social_skk = App::SocialSKK->new({
125 plugins => [
126 { name => 'SocialIME' },
127 ],
128 });
129  
130 Creates and returns a new App::SocialSKK object.
131  
132 =back
133  
134 =head2 get_candidates ( I<$text> )
135  
136 =over 4
137  
138 my $candidates = $social_skk->get_candidates($text);
139  
140 Gets and returns candidates for the C<$text> from datasources using
141 plugins.
142  
143 =back
144  
145 =head1 REPOSITORY
146  
147 http://github.com/kentaro/perl-app-socialskk/tree/master
148  
149 Please give me feedbacks via GitHub repository above.
150  
151 =head1 SEE ALSO
152  
153 =over 4
154  
155 =item * Social IME
156  
157 http://www.social-ime.com/
158  
159 =item * socialskk.rb
160  
161 http://coderepos.org/share/browser/lang/ruby/misc/socialskk/socialskk.rb
162  
163 =back
164  
165 =head1 AUTHOR
166  
167 Kentaro Kuribayashi E<lt>kentaro@cpan.orgE<gt>
168  
169 =head1 COPYRIGHT AND LICENSE (The MIT License)
170  
171 Copyright (c) Kentaro Kuribayashi E<lt>kentaro@cpan.orgE<gt>
172  
173 Permission is hereby granted, free of charge, to any person obtaining
174 a copy of this software and associated documentation files (the
175 "Software"), to deal in the Software without restriction, including
176 without limitation the rights to use, copy, modify, merge, publish,
177 distribute, sublicense, and/or sell copies of the Software, and to
178 permit persons to whom the Software is furnished to do so, subject to
179 the following conditions:
180  
181 The above copyright notice and this permission notice shall be
182 included in all copies or substantial portions of the Software.
183  
184 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
185 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
186 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
187 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
188 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
189 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
190 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
191  
192 =cut
« lib/App/SocialSKK/Plugin/HatenaBookmark.pm ↑ Summary lib/App/SocialSKK/Base.pm »