]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Application/Demo/SimpleText.pm
Simple demo of text services; returning a string, an array, a hash
[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     return \@split_text;
144     
145     return undef;
146 }
147
148 __PACKAGE__->register_method(
149     method    => 'text_split',
150     api_name  => 'opensrf.simple-text.split',
151     api_level => 1,
152     argc      => 2,
153     signature => {
154         desc     => <<"         DESC",
155 Splits a string by a given delimiter (space by default) and returns an array of the split strings
156          DESC
157         'params' => [ {
158                 name => 'text',
159                 desc => 'The string to split',
160                 type => 'string' 
161             }, {
162                 name => 'delimiter',
163                 desc => 'The delimiter to split the string with',
164                 type => 'string' 
165             },
166         ],
167         'return' => {
168             desc => 'Splits a string by a given delimiter (space by default) and returns an array of the split strings',
169             type => 'array'
170         }
171     }
172 );
173
174 # accept string and optional arguments, return a string
175 sub text_substring {
176     my $self = shift;
177     my $conn = shift;
178     my $text = shift || '';
179     my $start_pos = shift || 0;
180     my $end_pos = shift;
181
182     my $subtext;
183     if ($end_pos) {
184         $subtext = substr($text, $start_pos, $end_pos);
185     } else {
186         $subtext = substr($text, $start_pos);
187     }
188     return $subtext;
189 }
190
191 __PACKAGE__->register_method(
192     method    => 'text_substring',
193     api_name  => 'opensrf.simple-text.substring',
194     api_level => 1,
195     argc      => 1,
196     signature => {
197         desc     => <<"         DESC",
198 Returns a substring of the input string
199          DESC
200         'params' => [ {
201                 name => 'text',
202                 desc => 'The string to process',
203                 type => 'string' 
204             }, {
205                 name => 'start_pos',
206                 desc => 'The start position for the substring (default 0)',
207                 type => 'int' 
208             }, {
209                 name => 'end_pos',
210                 desc => 'The end position for the substring (optional)',
211                 type => 'int' 
212             },
213         ],
214         'return' => {
215             desc => 'Returns a substring of the input string',
216             type => 'string'
217         }
218     }
219 );
220
221 # accept an array, return a hash (note: return by reference)
222 sub text_statistics {
223     my $self = shift;
224     my $conn = shift;
225     my $aref = shift || '';
226
227     my %stats;
228
229     my $length = 0;
230     my $word_count = 0;
231
232     foreach my $entry (@$aref) {
233         $length += length($entry);
234         $word_count += scalar (split /\s/, $entry);
235     }
236     $stats{'length'} = $length;
237     $stats{'word_count'} = $word_count;
238
239     return \%stats;
240 }
241
242 __PACKAGE__->register_method(
243     method    => 'text_statistics',
244     api_name  => 'opensrf.simple-text.statistics',
245     api_level => 1,
246     argc      => 1,
247     signature => {
248         desc     => <<"         DESC",
249 Returns the statistics for an array of strings
250          DESC
251         'params' => [ {
252                 name => 'text',
253                 desc => 'The array of strings to process',
254                 type => 'array' 
255             }
256         ],
257         'return' => {
258             desc => 'Returns the statistics for an array of strings',
259             type => 'hash'
260         }
261     }
262 );
263
264 1;