9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_BeerScore
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_BeerScore
23 * Handles incoming requests for beer scores.
26 * @package Phergie_Plugin_BeerScore
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie_Plugin_BeerScore
30 * @uses Phergie_Plugin_Http pear.phergie.org
32 class Phergie_Plugin_BeerScore extends Phergie_Plugin_Abstract
39 const TYPE_SCORE = 'SCORE';
46 const TYPE_SEARCH = 'SEARCH';
53 const TYPE_REFINE = 'REFINE';
60 const API_BASE_URL = 'http://caedmon.net/beerscore/';
65 * @var Phergie_Plugin_Http
70 * Checks for dependencies.
74 public function onLoad()
76 $this->http = $this->getPluginHandler()->getPlugin('Http');
80 * Handles beerscore commands.
82 * @param string $searchstring String to use in seaching for beer scores
86 public function onCommandBeerscore($searchstring)
88 $event = $this->getEvent();
89 $target = $event->getNick();
90 $source = $event->getSource();
92 $apiurl = self::API_BASE_URL . rawurlencode($searchstring);
93 $response = $this->http->get($apiurl);
95 if ($response->isError()) {
96 $this->doNotice($target, 'Score not found (or failed to contact API)');
100 $result = $response->getContent();
101 switch ($result->type) {
102 case self::TYPE_SCORE:
103 // small enough number to get scores
104 foreach ($result->beer as $beer) {
105 if ($beer->score === -1) {
106 $score = '(not rated)';
108 $score = $beer->score;
111 = "{$target}: rating for {$beer->name}" .
112 " = {$score} ({$beer->url})";
113 $this->doPrivmsg($source, $str);
117 case self::TYPE_SEARCH:
118 // only beer names, no scores
121 foreach ($result->beer as $beer) {
122 if (isset($beer->score)) {
124 if ($beer->score === -1) {
125 $score = '(not rated)';
127 $score = $beer->score;
130 = "{$target}: rating for {$beer->name}" .
131 " = {$score} ({$beer->url})";
132 $this->doPrivmsg($source, $str);
134 $str .= "({$beer->name} -> {$beer->url}) ";
137 $foundnum = $result->num - $found;
138 $more = $found ? 'more ' : '';
139 $str = "{$target}: {$foundnum} {$more}results... {$str}";
140 $this->doPrivmsg($source, $str);
143 case self::TYPE_REFINE:
144 // Too many results; only output search URL
145 if ($result->num < 100) {
148 $num = 'at least 100';
150 $resultsword = (($result->num > 1) ? 'results' : 'result');
151 $str = "{$target}: {$num} {$resultsword}; {$result->searchurl}";
152 $this->doPrivmsg($source, $str);