1 /++
2  + Authors: Stephan Dilly, lastname dot firstname at gmail dot com
3  + Copyright: MIT
4  +/
5 module ask.baseintent;
6 
7 import ask.alexaskill;
8 import ask.locale;
9 import ask.types;
10 
11 /// abstract base class for a separate intent
12 abstract class BaseIntent : ITextManager
13 {
14 	///
15 	private immutable string _name;
16 	///
17 	private ITextManager _texts;
18 	/// allows to query for the intents string representation that needs to match intent schema
19 	public @property string name() const nothrow pure { return _name; }
20 	/// allows to define used textManager
21 	public @property void textManager(ITextManager _mgr) nothrow { _texts = _mgr; }
22 
23 	/// c'tor
24 	public this()
25 	{
26 		import std.array:split;
27 
28 		TypeInfo_Class info = cast(TypeInfo_Class)typeid(this);
29 		auto fullnameParts = info.name.split(".");
30 		_name = fullnameParts[$-1];
31 	}
32 
33 	/// forwards to currently active `ITextManager.getText`
34 	protected string getText(int _key) const pure nothrow
35 	{
36 		return _texts.getText(_key);
37 	}
38 
39 	/// handler that needs to be implemented in inheriting Intent
40 	public AlexaResult onIntent(AlexaEvent, AlexaContext);
41 }
42 
43 ///
44 unittest
45 {
46 	class TestIntent : BaseIntent{
47 		override AlexaResult onIntent(AlexaEvent, AlexaContext){
48 			AlexaResult res;
49 			res._version = "v3";
50 			return res;
51 		}
52 	}
53 
54 	class TestSkill : AlexaSkill!TestSkill{
55 		this(){
56 			super([]);
57 			addIntent(new TestIntent);
58 		}
59 	}
60 
61 	auto skill = new TestSkill();
62 	AlexaEvent ev;
63 	ev.request.type = AlexaRequest.Type.IntentRequest;
64 	ev.request.intent.name = "TestIntent";
65 	auto res = skill.executeEvent(ev,AlexaContext());
66 	assert(res._version == "v3");
67 }