/** * @return Response */ public function cryptCmd() { $command = $this->getRequest()->getPackage(); $args = $this->getRequest()->getArgs(); $type = strtolower(array_shift($args)); $method = $command . ucfirst($type); if (!method_exists($this, $method)) { return Response::createError('Operation not supported!'); } try { $result = $this->{$method}(implode('', $args)); } catch (Exception\CorruptDataException $e) { return Response::createError(sprintf('Could not %s the %s data.', $command, $type)); } return Response::create(sprintf('%s: %s', strtoupper($type), $result)); }
/** * @return Response */ public function wikiCmd() { $language = $this->getRequest()->getArg(1) ?: $this->getOption('default_wiki_language', 'en'); if (strlen($language) !== 2) { return Response::createError('The language must be 2 characters.'); } $url = sprintf('https://%s.wikipedia.org/w/api.php?action=query&generator=random&grnnamespace=0&prop=info&inprop=url&formatversion=2&format=json', $language); $response = $this->getHttpClient()->get($url); if ($response->getStatusCode() !== 200) { return Response::createError('There was an error while talking to Wikipedia. Please try again.'); } $json = $response->json(); $title = $json['query']['pages'][0]['title']; $fullUrl = $json['query']['pages'][0]['fullurl']; return Response::create(sprintf('%s - %s', $title, $fullUrl), null, Response::COLOR_RANDOM); }
/** * @return Response */ public function ackCmd() { $user = $this->getRequest()->getArg(1); $userVotes = $this->getVotes(); if ($user === null) { if (empty($userVotes)) { return Response::createError('Nobody voted yet, looks like you`ll have to choose on your own this time!'); } $lastUserVotes = array_keys($userVotes); $user = end($lastUserVotes); } if (!isset($userVotes[$user])) { return Response::createError(sprintf('Could not find any vote for user `%s`', $user)); } $userMention = $this->getRequest()->getUser()->getMentionName(); $userVotes[$userMention] = $userVotes[$user]; $this->saveVotes($userVotes); $this->removeAbstain($userMention); return $this->statusCmd(); }
public function testCreateError() { $response = Api\Response::createError('msg', Api\Response::FORMAT_TEXT); $this->assertSame(['color' => Api\Response::COLOR_RED, 'message' => '(failed) msg', 'message_format' => Api\Response::FORMAT_TEXT, 'notify' => false], $response->toArray()); }