Skip to content
This repository was archived by the owner on Aug 12, 2022. It is now read-only.

Commit 05e1173

Browse files
committed
[UPD] modif Constants && Test
1 parent c392df2 commit 05e1173

File tree

10 files changed

+108
-66
lines changed

10 files changed

+108
-66
lines changed

npm-debug.log

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
0 info it worked if it ends with ok
2+
1 verbose cli [ '/usr/bin/nodejs', '/usr/local/bin/npm', 'run', 'test' ]
3+
2 info using npm@3.5.2
4+
3 info using node@v4.2.6
5+
4 verbose stack Error: ENOENT: no such file or directory, open '/home/bgantelm/Recast/SDK-PHP/package.json'
6+
4 verbose stack at Error (native)
7+
5 verbose cwd /home/bgantelm/Recast/SDK-PHP
8+
6 error Linux 4.4.0-43-generic
9+
7 error argv "/usr/bin/nodejs" "/usr/local/bin/npm" "run" "test"
10+
8 error node v4.2.6
11+
9 error npm v3.5.2
12+
10 error path /home/bgantelm/Recast/SDK-PHP/package.json
13+
11 error code ENOENT
14+
12 error errno -2
15+
13 error syscall open
16+
14 error enoent ENOENT: no such file or directory, open '/home/bgantelm/Recast/SDK-PHP/package.json'
17+
15 error enoent ENOENT: no such file or directory, open '/home/bgantelm/Recast/SDK-PHP/package.json'
18+
15 error enoent This is most likely not a problem with npm itself
19+
15 error enoent and is related to npm not being able to find a file.
20+
16 verbose exit [ -2, true ]

src/Client.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace RecastAI;
44

5+
require 'vendor/autoload.php';
6+
7+
use Requests;
8+
59
/**
610
* Class Client
711
* @package RecastAI
812
*/
913
class Client
1014
{
11-
const API_ENDPOINT = 'https://api.recast.ai/v2/request';
12-
1315
/**
1416
* Client constructor.
1517
* @param null $token
@@ -49,7 +51,7 @@ public function textRequest($text, $options = null)
4951
} else {
5052
$headers = array('Content-Type' => 'application/json', 'Authorization' => "Token " . $token);
5153

52-
$res = $this->requestPrivate(self::API_ENDPOINT, $headers, $params);
54+
$res = $this->requestPrivate(Constants::API_ENDPOINT, $headers, $params);
5355
return (new Response($res));
5456
}
5557
}
@@ -65,7 +67,7 @@ public function textRequest($text, $options = null)
6567
*/
6668
protected function requestPrivate($url, $headers, $params)
6769
{
68-
$res = \Requests::post($url, $headers, json_encode($params));
70+
$res = Requests::post($url, $headers, json_encode($params));
6971

7072
return ($res);
7173
}
@@ -106,7 +108,7 @@ public function fileRequest($file, $options = null)
106108
if (!$token) {
107109
return ('Token is missing');
108110
} else {
109-
$url = self::API_ENDPOINT;
111+
$url = Constants::API_ENDPOINT;
110112

111113
if (!$this->language) {
112114
$params = [
@@ -168,7 +170,7 @@ public function textConverse($text, $conversation_token = null, $options = null)
168170
return ('Token is missing');
169171
} else {
170172
$headers = array('Content-Type' => 'application/json', 'Authorization' => "Token " . $token);
171-
$res = $this->requestPrivate(Conversation::API_ENDPOINT_CONVERSATION, $headers, $params);
173+
$res = $this->requestPrivate(Constants::API_ENDPOINT_CONVERSATION, $headers, $params);
172174

173175
return (new Conversation(($res)));
174176
}

src/Constants.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace RecastAI;
4+
5+
/**
6+
* Class Constants
7+
* @package RecastAI
8+
*/
9+
class Constants
10+
{
11+
const API_ENDPOINT = 'https://api.recast.ai/v2/request';
12+
const API_ENDPOINT_CONVERSATION = 'https://api.recast.ai/v2/converse';
13+
14+
const ACT_ASSERT = 'assert';
15+
const ACT_COMMAND = 'command';
16+
const ACT_WH_QUERY = 'wh-query';
17+
const ACT_YN_QUERY = 'yn-query';
18+
19+
const TYPE_ABBREVIATION = 'abbr:';
20+
const TYPE_ENTITY = 'enty:';
21+
const TYPE_DESCRIPTION = 'desc:';
22+
const TYPE_HUMAN = 'hum:';
23+
const TYPE_LOCATION = 'loc:';
24+
const TYPE_NUMBER = 'num:';
25+
26+
const SENTIMENT_POSITIVE = 'positive';
27+
const SENTIMENT_NEUTRAL = 'neutral';
28+
const SENTIMENT_NEGATIVE = 'negative';
29+
const SENTIMENT_VPOSITIVE = 'vpositive';
30+
const SENTIMENT_VNEGATIVE = 'vnegative';
31+
}

src/Conversation.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
*/
99
class Conversation
1010
{
11-
const API_ENDPOINT_CONVERSATION = 'https://api.recast.ai/v2/converse';
12-
11+
1312
public function __construct($json)
1413
{
15-
$response = json_decode($json);
14+
// var_dump($json);
15+
$response = json_decode($json->body);
1616

1717
$this->replies = $response->results->replies;
1818
$this->action = $response->results->action;
@@ -101,7 +101,7 @@ static public function setMemory($token, $conversation_token, $memory)
101101
$params = array('conversation_token' => $conversation_token, 'memory' => $memory);
102102
$headers = array('Content-Type' => 'application/json', 'Authorization' => "Token " . $token);
103103

104-
$request = \Requests::put(self::API_ENDPOINT_CONVERSATION, $headers, json_encode($params));
104+
$request = \Requests::put(Constants::API_ENDPOINT_CONVERSATION, $headers, json_encode($params));
105105
$res = (json_decode($request->body));
106106
return ($res->{'results'}->{'memory'});
107107
}
@@ -121,7 +121,7 @@ static public function resetMemory($token, $conversation_token, $alias = null)
121121
];
122122
$params = array('conversation_token' => $conversation_token, 'memory' => $memory);
123123
}
124-
$request = \Requests::put(self::API_ENDPOINT_CONVERSATION, $headers, json_encode($params));
124+
$request = \Requests::put(Constants::API_ENDPOINT_CONVERSATION, $headers, json_encode($params));
125125
return ($request);
126126
}
127127

@@ -134,7 +134,7 @@ static public function resetConversation($token, $conversation_token)
134134
$curl = curl_init();
135135

136136
curl_setopt_array($curl, array(
137-
CURLOPT_URL => self::API_ENDPOINT_CONVERSATION,
137+
CURLOPT_URL => Constants::API_ENDPOINT_CONVERSATION,
138138
CURLOPT_RETURNTRANSFER => true,
139139
CURLOPT_ENCODING => "",
140140
CURLOPT_MAXREDIRS => 10,
@@ -160,4 +160,4 @@ static public function resetConversation($token, $conversation_token)
160160
return ($response);
161161
}
162162
}
163-
}
163+
}

src/Response.php

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,15 @@
88
*/
99
class Response
1010
{
11-
const ACT_ASSERT = 'assert';
12-
const ACT_COMMAND = 'command';
13-
const ACT_WH_QUERY = 'wh-query';
14-
const ACT_YN_QUERY = 'yn-query';
15-
16-
const TYPE_ABBREVIATION = 'abbr:';
17-
const TYPE_ENTITY = 'enty:';
18-
const TYPE_DESCRIPTION = 'desc:';
19-
const TYPE_HUMAN = 'hum:';
20-
const TYPE_LOCATION = 'loc:';
21-
const TYPE_NUMBER = 'num:';
22-
23-
const SENTIMENT_POSITIVE = 'positive';
24-
const SENTIMENT_NEUTRAL = 'neutral';
25-
const SENTIMENT_NEGATIVE = 'negative';
26-
const SENTIMENT_VPOSITIVE = 'vpositive';
27-
const SENTIMENT_VNEGATIVE = 'vnegative';
2811

2912
/**
3013
* Response constructor.
3114
* @param $response
3215
*/
3316
public function __construct($json)
3417
{
35-
$response = json_decode($json);
36-
18+
var_dump($json);
19+
$response = json_decode($json->body);
3720
$this->entities = [];
3821

3922
$this->act = $response->results->act;
@@ -112,31 +95,31 @@ public function intent()
11295
*/
11396
public function isAssert()
11497
{
115-
return ($this->act === self::ACT_ASSERT);
98+
return ($this->act === Constants::ACT_ASSERT);
11699
}
117100

118101
/**
119102
* @return bool
120103
*/
121104
public function isCommand()
122105
{
123-
return ($this->act === self::ACT_COMMAND);
106+
return ($this->act === Constants::ACT_COMMAND);
124107
}
125108

126109
/**
127110
* @return bool
128111
*/
129112
public function isWhQuery()
130113
{
131-
return ($this->act === self::ACT_WH_QUERY);
114+
return ($this->act === Constants::ACT_WH_QUERY);
132115
}
133116

134117
/**
135118
* @return bool
136119
*/
137120
public function isYnQuery()
138121
{
139-
return ($this->act === self::ACT_YN_QUERY);
122+
return ($this->act === Constants::ACT_YN_QUERY);
140123
}
141124

142125
/**
@@ -146,7 +129,7 @@ public function isYnQuery()
146129
*/
147130
public function isAbbreviation()
148131
{
149-
if (strstr($this->type, self::TYPE_ABBREVIATION)) {
132+
if (strstr($this->type, Constants::TYPE_ABBREVIATION)) {
150133
return (true);
151134
}
152135
return (false);
@@ -157,7 +140,7 @@ public function isAbbreviation()
157140
*/
158141
public function isEntity()
159142
{
160-
if (strstr($this->type, self::TYPE_ENTITY)) {
143+
if (strstr($this->type, Constants::TYPE_ENTITY)) {
161144
return (true);
162145
}
163146
return (false);
@@ -168,7 +151,7 @@ public function isEntity()
168151
*/
169152
public function isDescription()
170153
{
171-
if (strstr($this->type, self::TYPE_DESCRIPTION)) {
154+
if (strstr($this->type, Constants::TYPE_DESCRIPTION)) {
172155
return (true);
173156
}
174157
return (false);
@@ -179,7 +162,7 @@ public function isDescription()
179162
*/
180163
public function isHuman()
181164
{
182-
if (strstr($this->type, self::TYPE_HUMAN)) {
165+
if (strstr($this->type, Constants::TYPE_HUMAN)) {
183166
return (true);
184167
}
185168
return (false);
@@ -190,7 +173,7 @@ public function isHuman()
190173
*/
191174
public function isLocation()
192175
{
193-
if (strstr($this->type, self::TYPE_LOCATION)) {
176+
if (strstr($this->type, Constants::TYPE_LOCATION)) {
194177
return (true);
195178
}
196179
return (false);
@@ -201,7 +184,7 @@ public function isLocation()
201184
*/
202185
public function isNumber()
203186
{
204-
if (strstr($this->type, self::TYPE_NUMBER)) {
187+
if (strstr($this->type, Constants::TYPE_NUMBER)) {
205188
return (true);
206189
}
207190
return (false);
@@ -215,38 +198,38 @@ public function isNumber()
215198

216199
public function isPositive()
217200
{
218-
return ($this->sentiment === self::SENTIMENT_POSITIVE);
201+
return ($this->sentiment === Constants::SENTIMENT_POSITIVE);
219202
}
220203

221204
/**
222205
* @return bool
223206
*/
224207
public function isNeutral()
225208
{
226-
return ($this->sentiment === self::SENTIMENT_NEUTRAL);
209+
return ($this->sentiment === Constants::SENTIMENT_NEUTRAL);
227210
}
228211

229212
/**
230213
* @return bool
231214
*/
232215
public function isNegative()
233216
{
234-
return ($this->sentiment === self::SENTIMENT_NEGATIVE);
217+
return ($this->sentiment === Constants::SENTIMENT_NEGATIVE);
235218
}
236219

237220
/**
238221
* @return bool
239222
*/
240223
public function isVPositive()
241224
{
242-
return ($this->sentiment === self::SENTIMENT_VPOSITIVE);
225+
return ($this->sentiment === Constants::SENTIMENT_VPOSITIVE);
243226
}
244227

245228
/**
246229
* @return bool
247230
*/
248231
public function isVNegative()
249232
{
250-
return ($this->sentiment === self::SENTIMENT_VNEGATIVE);
233+
return ($this->sentiment === Constants::SENTIMENT_VNEGATIVE);
251234
}
252235
}

tests/ClientTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function testClientClassIfAttributesAreOkay()
5050
public function testTextRequestIfAllOkay()
5151
{
5252
$callResult = self::jsonResponse();
53+
$res = (Object)[ "body" => ($callResult) ];
5354
$token = 'TestToken';
5455
$language = 'en';
5556

@@ -60,9 +61,9 @@ public function testTextRequestIfAllOkay()
6061

6162
$stub->expects($this->once())
6263
->method('requestPrivate')
63-
->will($this->returnValue($callResult));
64-
65-
$response = $stub->textRequest($callResult);
64+
->will($this->returnValue($res));
65+
$result = json_decode($res->body);
66+
$response = $stub->textRequest($result->results->source);
6667

6768
$this->assertEquals('200', $response->status);
6869
}
@@ -107,4 +108,4 @@ public function testFileRequestIfNoToken()
107108
$res = $client->fileRequest(__DIR__ . '/data/file.wav');
108109
$this->assertEquals($res, 'Token is missing');
109110
}
110-
}
111+
}

tests/ConversationTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ protected static function jsonResponse()
1414
public function testConversationClassWithAllOkay()
1515
{
1616
$jsonResult = self::jsonResponse();
17-
$this->assertInstanceOf('RecastAI\Conversation', new Conversation($jsonResult));
17+
$res = (Object)[ "body" => ($jsonResult) ];
18+
$this->assertInstanceOf('RecastAI\Conversation', new Conversation(($res)));
1819
}
1920

2021
public function testConversationClassAttributes()
2122
{
2223
$jsonResult = self::jsonResponse();
23-
$result = json_decode($jsonResult);
24-
25-
$conversation = new Conversation($jsonResult);
24+
$res = (Object)[ "body" => ($jsonResult) ];
25+
$conversation = new Conversation(($res));
26+
$result = json_decode($res->body);
2627

2728
$this->assertEquals($conversation->conversationToken, $result->results->conversation_token);
2829
$this->assertEquals($conversation->replies, $result->results->replies);
@@ -34,14 +35,15 @@ public function testConversationClassAttributes()
3435
public function testResponseClassMethods()
3536
{
3637
$jsonResult = self::jsonResponse();
37-
$result = json_decode($jsonResult);
38+
$res = (Object)[ "body" => ($jsonResult) ];
39+
$result = json_decode($res->body);
3840

39-
$conversation = new Conversation($jsonResult);
41+
$conversation = new Conversation($res);
4042

4143
$this->assertEquals($conversation->reply(), $result->results->replies[0]);
4244
$this->assertEquals($conversation->joinedReplies(), join(' ', $result->results->replies));
4345
$this->assertEquals($conversation->joinedReplies('\n'), join('\n', $result->results->replies));
4446
$this->assertEquals($conversation->memory(), $result->results->memory);
4547
$this->assertEquals($conversation->memory('loc'), $result->results->memory->loc);
4648
}
47-
}
49+
}

0 commit comments

Comments
 (0)