]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Application/Demo/SimpleText.pm
Add a streaming version of the split() method in simpletext example
[OpenSRF.git] / src / perl / lib / OpenSRF / Application / Demo / SimpleText.pm
1 #!/usr/bin/perl
2
3 # Copyright (C) 2009 Dan Scott <dscott@laurentian.ca>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 =head1 NAME
20
21 OpenSRF::Application::Demo::SimpleText - simple text methods for demonstrating OpenSRF
22
23 =head1 SYNOPSIS
24
25 Via srfsh:
26   request opensrf.simple-text opensrf.simple-text.reverse "foo"
27 returns "oof"
28
29   request opensrf.simple-text opensrf.simple-text.substring "foobar", 3
30 returns "bar"
31
32   request opensrf.simple-text opensrf.simple-text.split "This is a test", " "
33 returns ["This", "is", "a", "test"]
34
35   request opensrf.simple-text opensrf.simple-text.statistics ["foo bar ala", "the cats"]
36 returns:
37
38 Received Data: {
39   "length":19,
40   "word_count":5
41 }
42
43 Via Perl:
44   my $session = OpenSRF::AppSession->create("opensrf.simple-text");
45   my $request = $session->request("opensrf.simple-text.reverse", [ "foo" ] )->gather();
46   $request = $session->request("opensrf.simple-text.substring", [ "foobar", 3 ] )->gather();
47   $request = $session->request("opensrf.simple-text.split", [ "This is a test", " " ] )->gather();
48   $session->disconnect();
49
50   # $request is a reference to the returned values
51
52 =head1 AUTHOR
53
54 Dan Scott, dscott@laurentian.ca
55
56 =cut
57
58 package OpenSRF::Application::Demo::SimpleText;
59
60 use strict;
61 use warnings;
62
63 # All OpenSRF applications must be based on OpenSRF::Application or
64 # a subclass thereof.  Makes sense, eh?
65 use OpenSRF::Application;
66 use base qw/OpenSRF::Application/;
67
68 # This is the client class, used for connecting to open-ils.storage
69 use OpenSRF::AppSession;
70
71 # This is an extension of Error.pm that supplies some error types to throw
72 use OpenSRF::EX qw(:try);
73
74 # This is a helper class for querying the OpenSRF Settings application ...
75 use OpenSRF::Utils::SettingsClient;
76
77 # ... and here we have the built in logging helper ...
78 use OpenSRF::Utils::Logger qw($logger);
79
80 # ... and this manages cached results for us ...
81 use OpenSRF::Utils::Cache;
82
83 my $prefix = "opensrf.simple-text"; # Prefix for caching values
84 my $cache;
85 my $cache_timeout;
86
87 # initialize() is invoked once, when the OpenSRF service is first started
88 # We don't need caching for these methods, but it's useful copy/paste
89 # code for more advanced services
90 sub initialize {
91     $cache = OpenSRF::Utils::Cache->new('global');
92     my $sclient = OpenSRF::Utils::SettingsClient->new();
93     $cache_timeout = $sclient->config_value(
94         "apps", "opensrf.simple-text", "app_settings", "cache_timeout" ) || 300;
95 }
96
97 # child_init() is invoked every time a new child process is created
98 # We don't need any per-child initialization, so this is empty
99 sub child_init { }
100
101 # accept and return a simple string
102 sub text_reverse {
103     my $self = shift;
104     my $conn = shift;
105     my $text = shift;
106
107     my $reversed_text = scalar reverse($text);
108     return $reversed_text;
109     
110     return undef;
111 }
112
113 __PACKAGE__->register_method(
114     method    => 'text_reverse',
115     api_name  => 'opensrf.simple-text.reverse',
116     api_level => 1,
117     argc      => 1,
118     signature => {
119         desc     => <<"         DESC",
120 Returns the input string in reverse order
121          DESC
122         'params' => [ {
123                 name => 'text',
124                 desc => 'The string to reverse',
125                 type => 'string' 
126             },
127         ],
128         'return' => {
129             desc => 'Returns the input string in reverse order',
130             type => 'string'
131         }
132     }
133 );
134
135 # accept string, return an array (note: return by reference)
136 sub text_split {
137     my $self = shift;
138     my $conn = shift;
139     my $text = shift;
140     my $delimiter = shift || ' ';
141
142     my @split_text = split $delimiter, $text;
143     foreach my $string (@split_text) {
144         $conn->respond( $string );
145     }
146     
147     return undef;
148 }
149
150 __PACKAGE__->register_method(
151     method    => 'text_split',
152     api_name  => 'opensrf.simple-text.split',
153     api_level => 1,
154     argc      => 2,
155     stream    => 1,
156     signature => {
157         desc     => <<"         DESC",
158 Splits a string by a given delimiter (space by default) and returns an array of the split strings
159          DESC
160         'params' => [ {
161                 name => 'text',
162                 desc => 'The string to split',
163                 type => 'string' 
164             }, {
165                 name => 'delimiter',
166                 desc => 'The delimiter to split the string with',
167                 type => 'string' 
168             },
169         ],
170         'return' => {
171             desc => 'Splits a string by a given delimiter (space by default) and returns an array of the split strings',
172             type => 'array'
173         }
174     }
175 );
176
177 # accept string and optional arguments, return a string
178 sub text_substring {
179     my $self = shift;
180     my $conn = shift;
181     my $text = shift || '';
182     my $start_pos = shift || 0;
183     my $end_pos = shift;
184
185     my $subtext;
186     if ($end_pos) {
187         $subtext = substr($text, $start_pos, $end_pos);
188     } else {
189         $subtext = substr($text, $start_pos);
190     }
191     return $subtext;
192 }
193
194 __PACKAGE__->register_method(
195     method    => 'text_substring',
196     api_name  => 'opensrf.simple-text.substring',
197     api_level => 1,
198     argc      => 1,
199     signature => {
200         desc     => <<"         DESC",
201 Returns a substring of the input string
202          DESC
203         'params' => [ {
204                 name => 'text',
205                 desc => 'The string to process',
206                 type => 'string' 
207             }, {
208                 name => 'start_pos',
209                 desc => 'The start position for the substring (default 0)',
210                 type => 'int' 
211             }, {
212                 name => 'end_pos',
213                 desc => 'The end position for the substring (optional)',
214                 type => 'int' 
215             },
216         ],
217         'return' => {
218             desc => 'Returns a substring of the input string',
219             type => 'string'
220         }
221     }
222 );
223
224 # accept an array, return a hash (note: return by reference)
225 sub text_statistics {
226     my $self = shift;
227     my $conn = shift;
228     my $aref = shift || '';
229
230     my %stats;
231
232     my $length = 0;
233     my $word_count = 0;
234
235     foreach my $entry (@$aref) {
236         $length += length($entry);
237         $word_count += scalar (split /\s/, $entry);
238     }
239     $stats{'length'} = $length;
240     $stats{'word_count'} = $word_count;
241
242     return \%stats;
243 }
244
245 __PACKAGE__->register_method(
246     method    => 'text_statistics',
247     api_name  => 'opensrf.simple-text.statistics',
248     api_level => 1,
249     argc      => 1,
250     signature => {
251         desc     => <<"         DESC",
252 Returns the statistics for an array of strings
253          DESC
254         'params' => [ {
255                 name => 'text',
256                 desc => 'The array of strings to process',
257                 type => 'array' 
258             }
259         ],
260         'return' => {
261             desc => 'Returns the statistics for an array of strings',
262             type => 'hash'
263         }
264     }
265 );
266
267 1;