/* * MindTouch Dream - a distributed REST framework * Copyright (C) 2006, 2007 MindTouch, Inc. * www.mindtouch.com oss@mindtouch.com * * For community documentation and downloads visit www.opengarden.org; * please review the licensing section. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * http://www.gnu.org/copyleft/lesser.html */ namespace MindTouch.Dream.Samples { using Yield = System.Collections.Generic.IEnumerator; [DreamService("Dream Tutorial 8-Ball", "Copyright (c) 2006, 2007 MindTouch, Inc.", Info = "http://doc.opengarden.org/Dream/Samples/8-Ball", SID = new string[] { "http://services.mindtouch.com/dream/tutorial/2007/03/8ball" } )] public class EightBallService : DreamService { //--- Class Fields --- // instance for generating random numbers private static System.Random _random = new System.Random(); // list of random responses for the 8-ball private static string[] _answers = new string[] { "Reply hazy, try again", "Without a doubt", "My sources say no", "As I see it, yes" }; //--- Features --- [DreamFeature("GET:", "Returns a random 8-ball message")] public Yield GetAnswer(DreamContext context, DreamMessage request, Result response) { // compute a random number between 0 and the number of responses we have int index = _random.Next(_answers.Length); // send our response back to the requestor response.Return(DreamMessage.Ok(MimeType.TEXT, "The 8-ball says: " + _answers[index])); yield break; } } }