Skip to content

Commit

Permalink
#6 create new catalog structure for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
xandros15 committed May 25, 2016
1 parent d49d1ac commit 2c349bd
Show file tree
Hide file tree
Showing 12 changed files with 1,348 additions and 1,430 deletions.
599 changes: 299 additions & 300 deletions module/Aigis.php → module/aigis/Aigis.php

Large diffs are not rendered by default.

126 changes: 63 additions & 63 deletions module/ApiProxy.php → module/api-proxy/ApiProxy.php
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
<?php

namespace module;

use library\Module;

class ApiProxy extends Module
{
const
URL_API = 'api.touhou.pl/external',
QUERY_REGEX = '~-(\w+?):([^ ]+)~';

public function loadSettings($object = null)
{
$this->setCommand([
'trigger' => 'api',
'action' => 'getResponseFromQuery',
'arguments' => -1,
'channels' => ['#touhoupl'],
'permit' => true
]);
parent::loadSettings($this);
}

public function saveSettings()
{
return parent::saveSettings();
}

//> !api param:value query1 query2
protected function getResponseFromQuery(array $arguments = [])
{
if (!$arguments) {
return;
}
$trzeciAgrumentDoCallbacka = implode(' ', $arguments);
$params = [];
$query = preg_replace_callback(self::QUERY_REGEX,
function($m) use (&$params) {
$params[$m[1]] = $m[2];
},$trzeciAgrumentDoCallbacka);

$url = self::URL_API . '?' . http_build_query(['q' => trim($query)] + $params);

$response = json_decode($this->loadStreamUrl($url), true);

if ($response) {
return $this->reply($this->flat($response));
}
}

private function flat(array $array)
{
$string = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = $this->flat($value);
} elseif (is_string($value)) {
$string .= $key . ':' . $value . ' ';
}
}
return rtrim($string, ' ');
}
<?php

namespace ApiProxy;

use Saya\Client\Module;

class ApiProxy extends Module
{
const
URL_API = 'api.touhou.pl/external',
QUERY_REGEX = '~-(\w+?):([^ ]+)~';

public function loadSettings($object = null)
{
$this->setCommand([
'trigger' => 'api',
'action' => 'getResponseFromQuery',
'arguments' => -1,
'channels' => ['#touhoupl'],
'permit' => true
]);
parent::loadSettings($this);
}

public function saveSettings()
{
return parent::saveSettings();
}

//> !api param:value query1 query2
protected function getResponseFromQuery(array $arguments = [])
{
if (!$arguments) {
return;
}
$trzeciAgrumentDoCallbacka = implode(' ', $arguments);
$params = [];
$query = preg_replace_callback(self::QUERY_REGEX,
function($m) use (&$params) {
$params[$m[1]] = $m[2];
},$trzeciAgrumentDoCallbacka);

$url = self::URL_API . '?' . http_build_query(['q' => trim($query)] + $params);

$response = json_decode($this->loadStreamUrl($url), true);

if ($response) {
$this->reply($this->flat($response));
}
}

private function flat(array $array)
{
$string = '';
foreach ($array as $key => $value) {
if (is_array($value)) {
$value = $this->flat($value);
} elseif (is_string($value)) {
$string .= $key . ':' . $value . ' ';
}
}
return rtrim($string, ' ');
}
}
Loading

0 comments on commit 2c349bd

Please sign in to comment.