]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/BeerScore.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / BeerScore.php
1 <?php
2 /**
3  * Phergie 
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie 
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
20  */
21
22 /**
23  * Handles incoming requests for beer scores.
24  *
25  * @category Phergie 
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
31  */
32 class Phergie_Plugin_BeerScore extends Phergie_Plugin_Abstract
33 {
34     /**
35      * Score result type
36      *
37      * @const string
38      */
39     const TYPE_SCORE = 'SCORE';
40
41     /**
42      * Search result type
43      *
44      * @const string
45      */
46     const TYPE_SEARCH = 'SEARCH';
47
48     /**
49      * Refine result type
50      *
51      * @const type
52      */
53     const TYPE_REFINE = 'REFINE';
54
55     /**
56      * Base API URL
57      *
58      * @const string
59      */
60     const API_BASE_URL = 'http://caedmon.net/beerscore/';
61
62     /**
63      * HTTP plugin
64      *
65      * @var Phergie_Plugin_Http
66      */
67     protected $http;
68
69     /** 
70      * Checks for dependencies.
71      *
72      * @return void
73      */
74     public function onLoad()
75     {
76         $this->http = $this->getPluginHandler()->getPlugin('Http');
77     }
78
79     /**
80      * Handles beerscore commands.
81      *
82      * @param string $searchstring String to use in seaching for beer scores
83      *
84      * @return void
85      */
86     public function onCommandBeerscore($searchstring)
87     {
88         $event = $this->getEvent();
89         $target = $event->getNick();
90         $source = $event->getSource();
91
92         $apiurl = self::API_BASE_URL . rawurlencode($searchstring);
93         $response = $this->http->get($apiurl);
94
95         if ($response->isError()) {
96             $this->doNotice($target, 'Score not found (or failed to contact API)');
97             return;
98         }
99
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)';
107                 } else {
108                     $score = $beer->score;
109                 }
110                 $str 
111                     = "{$target}: rating for {$beer->name}" . 
112                     " = {$score} ({$beer->url})";
113                 $this->doPrivmsg($source, $str);
114             }
115             break;
116
117         case self::TYPE_SEARCH:
118             // only beer names, no scores
119             $str = '';
120             $found = 0;
121             foreach ($result->beer as $beer) {
122                 if (isset($beer->score)) {
123                     ++$found;
124                     if ($beer->score === -1) {
125                         $score = '(not rated)';
126                     } else {
127                         $score = $beer->score;
128                     }
129                     $str 
130                         = "{$target}: rating for {$beer->name}" . 
131                         " = {$score} ({$beer->url})";
132                     $this->doPrivmsg($source, $str);
133                 } else {
134                     $str .= "({$beer->name} -> {$beer->url}) ";
135                 }
136             }
137             $foundnum = $result->num - $found;
138             $more = $found ? 'more ' : '';
139             $str = "{$target}: {$foundnum} {$more}results... {$str}";
140             $this->doPrivmsg($source, $str);
141             break;
142
143         case self::TYPE_REFINE:
144             // Too many results; only output search URL
145             if ($result->num < 100) {
146                 $num = $result->num;
147             } else {
148                 $num = 'at least 100';
149             }
150             $resultsword = (($result->num > 1) ? 'results' : 'result');
151             $str = "{$target}: {$num} {$resultsword}; {$result->searchurl}";
152             $this->doPrivmsg($source, $str);
153             break;
154         }
155     }
156 }