3 # Copyright (C) 2009 Dan Scott <dscott@laurentian.ca>
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.
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.
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.
21 OpenSRF::Application::Demo::SimpleText - simple text methods for demonstrating OpenSRF
26 request opensrf.simple-text opensrf.simple-text.reverse "foo"
29 request opensrf.simple-text opensrf.simple-text.substring "foobar", 3
32 request opensrf.simple-text opensrf.simple-text.split "This is a test", " "
33 returns ["This", "is", "a", "test"]
35 request opensrf.simple-text opensrf.simple-text.statistics ["foo bar ala", "the cats"]
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();
50 # $request is a reference to the returned values
54 Dan Scott, dscott@laurentian.ca
58 package OpenSRF::Application::Demo::SimpleText;
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/;
68 # This is the client class, used for connecting to open-ils.storage
69 use OpenSRF::AppSession;
71 # This is an extension of Error.pm that supplies some error types to throw
72 use OpenSRF::EX qw(:try);
74 # This is a helper class for querying the OpenSRF Settings application ...
75 use OpenSRF::Utils::SettingsClient;
77 # ... and here we have the built in logging helper ...
78 use OpenSRF::Utils::Logger qw($logger);
80 # ... and this manages cached results for us ...
81 use OpenSRF::Utils::Cache;
83 my $prefix = "opensrf.simple-text"; # Prefix for caching values
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
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;
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
101 # accept and return a simple string
107 my $reversed_text = scalar reverse($text);
108 return $reversed_text;
113 __PACKAGE__->register_method(
114 method => 'text_reverse',
115 api_name => 'opensrf.simple-text.reverse',
120 Returns the input string in reverse order
124 desc => 'The string to reverse',
129 desc => 'Returns the input string in reverse order',
135 # accept string, return an array (note: return by reference)
140 my $delimiter = shift || ' ';
142 my @split_text = split $delimiter, $text;
148 __PACKAGE__->register_method(
149 method => 'text_split',
150 api_name => 'opensrf.simple-text.split',
155 Splits a string by a given delimiter (space by default) and returns an array of the split strings
159 desc => 'The string to split',
163 desc => 'The delimiter to split the string with',
168 desc => 'Splits a string by a given delimiter (space by default) and returns an array of the split strings',
174 # accept string and optional arguments, return a string
178 my $text = shift || '';
179 my $start_pos = shift || 0;
184 $subtext = substr($text, $start_pos, $end_pos);
186 $subtext = substr($text, $start_pos);
191 __PACKAGE__->register_method(
192 method => 'text_substring',
193 api_name => 'opensrf.simple-text.substring',
198 Returns a substring of the input string
202 desc => 'The string to process',
206 desc => 'The start position for the substring (default 0)',
210 desc => 'The end position for the substring (optional)',
215 desc => 'Returns a substring of the input string',
221 # accept an array, return a hash (note: return by reference)
222 sub text_statistics {
225 my $aref = shift || '';
232 foreach my $entry (@$aref) {
233 $length += length($entry);
234 $word_count += scalar (split /\s/, $entry);
236 $stats{'length'} = $length;
237 $stats{'word_count'} = $word_count;
242 __PACKAGE__->register_method(
243 method => 'text_statistics',
244 api_name => 'opensrf.simple-text.statistics',
249 Returns the statistics for an array of strings
253 desc => 'The array of strings to process',
258 desc => 'Returns the statistics for an array of strings',