| 1 | |||
| 2 | #----------------------------------------------------------------- | ||
| 3 | # Service name: Mabuhay | ||
| 4 | # Authority: samples.jmoby.net | ||
| 5 | # Created: 14-Oct-2006 14:42:12 BST | ||
| 6 | # Contact: martin.senger@gmail.com | ||
| 7 | # Description: How to say "Hello" in many languages. Heavily based on a web resource "Greetings in more than 800 languages", maintained at http://www.elite.net/~runner/jennifers/hello.htm by Jennifer Runner. | ||
| 8 | #----------------------------------------------------------------- | ||
| 9 | |||
| 10 | package Service::Mabuhay; | ||
| 11 | |||
| 12 | use FindBin qw( $Bin ); | ||
| 13 | use lib $Bin; | ||
| 14 | |||
| 15 | #----------------------------------------------------------------- | ||
| 16 | # This is a mandatory section - but you can still choose one of | ||
| 17 | # the two options (keep one and commented out the other): | ||
| 18 | #----------------------------------------------------------------- | ||
| 19 | use MOSES::MOBY::Base; | ||
| 20 | # --- (1) this option loads dynamically everything | ||
| 21 | BEGIN { | ||
| 22 | use MOSES::MOBY::Generators::GenServices; | ||
| 23 | new MOSES::MOBY::Generators::GenServices->load | ||
| 24 | (authority => 'samples.jmoby.net', | ||
| 25 | service_names => ['Mabuhay']); | ||
| 26 | } | ||
| 27 | |||
| 28 | # --- (2) this option uses pre-generated module | ||
| 29 | # You can generate the module by calling a script: | ||
| 30 | # cd jMoby/src/Perl | ||
| 31 | # ../scripts/generate-services.pl -b samples.jmoby.net Mabuhay | ||
| 32 | # then comment out the whole option above, and uncomment | ||
| 33 | # the following line (and make sure that Perl can find it): | ||
| 34 | #use net::jmoby::samples::MabuhayBase; | ||
| 35 | |||
| 36 | # (this to stay here with any of the options above) | ||
| 37 | use vars qw( @ISA ); | ||
| 38 | @ISA = qw( net::jmoby::samples::MabuhayBase ); | ||
| 39 | use MOSES::MOBY::Package; | ||
| 40 | use MOSES::MOBY::ServiceException; | ||
| 41 | use strict; | ||
| 42 | |||
| 43 | #----------------------------------------------------------------- | ||
| 44 | # process_it | ||
| 45 | # This method is called for every job in the client request. | ||
| 46 | # Input data are in $request, this method creates a response | ||
| 47 | # into $response. The $context tells about all other jobs | ||
| 48 | # from the same request, and it can be used to fill there | ||
| 49 | # exceptions and service notes. | ||
| 50 | #----------------------------------------------------------------- | ||
| 51 | sub process_it { | ||
| 52 | my ($self, $request, $response, $context) = @_; | ||
| 53 | |||
| 54 | |||
| 55 | # read (some) input data | ||
| 56 | # (use eval to protect against missing data) | ||
| 57 | my $language = eval { $request->language }; | ||
| 58 | my $regex = eval { $language->regex->value }; | ||
| 59 | unless ($language and $regex) { | ||
| 60 | $response->record_error ( { code => INPUTS_INVALID, | ||
| 61 | msg => 'Input regular expression is missing.' } ); | ||
| 62 | return; | ||
| 63 | } | ||
| 64 | my $ignore_cases = eval { $language->case_insensitive->value }; | ||
| 65 | |||
| 66 | my @result_hellos = (); | ||
| 67 | my @result_langs = (); | ||
| 68 | open HELLO, $MOBYCFG::MABUHAY_RESOURCE_FILE | ||
| 69 | or $self->throw ('Mabuhay resource file not found.'); | ||
| 70 | while (<HELLO>) { | ||
| 71 | chomp; | ||
| 72 | my ($lang, $hello) = split (/\t+/, $_, 2); | ||
| 73 | if ( $ignore_cases ? | ||
| 74 | $lang =~ /$regex/i : | ||
| 75 | $lang =~ /$regex/ ) { | ||
| 76 | push (@result_hellos, $hello); | ||
| 77 | push (@result_langs, $lang); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | close HELLO; | ||
| 81 | |||
| 82 | foreach my $idx (0 .. $#result_hellos) { | ||
| 83 | $response->add_hello (new MOSES::MOBY::Data::simple_key_value_pair | ||
| 84 | ( key => $self->as_uni_string ($result_langs[$idx]), | ||
| 85 | value => $self->as_uni_string ($result_hellos[$idx]), | ||
| 86 | )); | ||
| 87 | } | ||
| 88 | |||
| 89 | # fill service notes (if you wish) | ||
| 90 | $context->serviceNotes | ||
| 91 | ('Response created at ' . gmtime() . ' (GMT), by the service \'Mabuhay\'.'); | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | __END__ |