Skip to content

Commit fc3972e

Browse files
committed
Preparing new version. Pushing usable master. Missing some tests. No build for now. [ci skip]
1 parent d7ec585 commit fc3972e

24 files changed

+1574
-32
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#Changelog
22
All notable changes will be documented in this file
33

4+
## 0.2 - May 2nd, 2015
5+
6+
- added Discussion API
7+
- added basic Custom API - returns Wildcards by default
8+
- Discussion API is now returned as child of Article / Product, too
9+
- minor change in how URLs are built - trailing slash no longer enforced after base API URL
10+
411
## 0.1.3 - April 21st, 2015
512

613
- new Scrutinizer settings

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ phpunit
3131

3232
## Contributing
3333

34-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
34+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details and [TODO](TODO.md) for ideas.
3535

3636
## Credits
3737

TODO.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ Active todos, ordered by priority
44

55
## High
66

7-
- implement Discussion API
87
- implement Crawlbot
98
- implement Search API
109

1110
## Medium
1211

12+
- write usage example
1313
- add streaming to Crawlbot - make it stream the result (it constantly grows)
1414
- implement Video API (currently beta)
1515
- improve Custom API
1616
- improve Wildcard Entity - apply to Custom API
1717

1818
## Low
1919

20-
- get more mock responses and test against them
20+
- work on PhpDoc consistency ($param type vs type $param)
21+
- get more mock responses and test against them
22+
- write example with custom EntityIterator (different Entity set for different API) and custom Entity (i.e. authorProfile, which parses some of the data and prepares for further use)

src/Abstracts/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function call()
9191

9292
public function buildUrl()
9393
{
94-
$url = rtrim($this->apiUrl, '/') . '/';
94+
$url = rtrim($this->apiUrl, '/');
9595

9696
// Add Token
9797
$url .= '?token=' . $this->diffbot->getToken();

src/Abstracts/Entity.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,18 @@ public function getData()
2020
{
2121
return $this->data;
2222
}
23+
24+
public function __get($name)
25+
{
26+
return (isset($this->data[$name])) ? $this->data[$name] : null;
27+
}
28+
29+
public function __call($name, $arguments)
30+
{
31+
$isGetter = substr($name, 0, 3) == 'get';
32+
if ($isGetter) {
33+
$property = lcfirst(substr($name, 3, strlen($name) - 3));
34+
return $this->$property;
35+
}
36+
}
2337
}

src/Api/Custom.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Api;
4+
5+
use Swader\Diffbot\Abstracts\Api;
6+
7+
class Custom extends Api
8+
{
9+
/** @var string API URL to which to send the request */
10+
protected $apiUrl = 'http://api.diffbot.com/v3';
11+
12+
public function __construct($url, $name)
13+
{
14+
parent::__construct($url);
15+
$this->apiUrl .= '/' . trim($name);
16+
}
17+
}

src/Api/Discussion.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Swader\Diffbot\Api;
4+
5+
use Swader\Diffbot\Abstracts\Api;
6+
use Swader\Diffbot\Traits\StandardApi;
7+
8+
class Discussion extends Api
9+
{
10+
use StandardApi;
11+
12+
/** @var string API URL to which to send the request */
13+
protected $apiUrl = 'http://api.diffbot.com/v3/discussion';
14+
15+
/**
16+
* Set the maximum number of pages in a thread to automatically concatenate
17+
* in a single response. Default = 1 (no concatenation). Set maxPages=all
18+
* to retrieve all pages of a thread regardless of length. Each individual
19+
* page will count as a separate API call.
20+
*
21+
* @param int|string $max Integer or "all"
22+
* @return $this
23+
*/
24+
public function setMaxPages($max = 1)
25+
{
26+
if ($max == 'all' || is_numeric($max)) {
27+
$this->otherOptions['maxPages'] = $max;
28+
}
29+
30+
return $this;
31+
}
32+
33+
/**
34+
* @see Swader\Diffbot\Entity\Discussion::getSentiment()
35+
* @param $bool
36+
* @return $this
37+
*/
38+
public function setSentiment($bool)
39+
{
40+
$this->fieldSettings['sentiment'] = (bool)$bool;
41+
42+
return $this;
43+
}
44+
45+
}

src/Diffbot.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Swader\Diffbot;
44

5+
use Swader\Diffbot\Api\Custom;
56
use Swader\Diffbot\Exceptions\DiffbotException;
67
use Swader\Diffbot\Api\Product;
78
use Swader\Diffbot\Api\Image;
89
use Swader\Diffbot\Api\Analyze;
910
use Swader\Diffbot\Api\Article;
11+
use Swader\Diffbot\Api\Discussion;
1012
use GuzzleHttp\Client;
1113
use Swader\Diffbot\Factory\Entity;
1214
use Swader\Diffbot\Interfaces\EntityFactory;
@@ -52,7 +54,7 @@ public function __construct($token = null)
5254

5355
/**
5456
* Sets the token for all future new instances
55-
* @param $token string The API access token, as obtained on diffbot.com/dev
57+
* @param string $token The API access token, as obtained on diffbot.com/dev
5658
* @return void
5759
*/
5860
public static function setToken($token)
@@ -132,62 +134,100 @@ public function getEntityFactory()
132134
/**
133135
* Creates a Product API interface
134136
*
135-
* @param $url string Url to analyze
137+
* @param string $url Url to analyze
136138
* @return Product
137139
*/
138140
public function createProductAPI($url)
139141
{
140142
$api = new Product($url);
141143
if (!$this->getHttpClient()) {
142144
$this->setHttpClient();
145+
$this->setEntityFactory();
143146
}
144147
return $api->registerDiffbot($this);
145148
}
146149

147150
/**
148151
* Creates an Article API interface
149152
*
150-
* @param $url string Url to analyze
153+
* @param string $url Url to analyze
151154
* @return Article
152155
*/
153156
public function createArticleAPI($url)
154157
{
155158
$api = new Article($url);
156159
if (!$this->getHttpClient()) {
157160
$this->setHttpClient();
161+
$this->setEntityFactory();
158162
}
159163
return $api->registerDiffbot($this);
160164
}
161165

162166
/**
163167
* Creates an Image API interface
164168
*
165-
* @param $url string Url to analyze
169+
* @param string $url Url to analyze
166170
* @return Image
167171
*/
168172
public function createImageAPI($url)
169173
{
170174
$api = new Image($url);
171175
if (!$this->getHttpClient()) {
172176
$this->setHttpClient();
177+
$this->setEntityFactory();
173178
}
174179
return $api->registerDiffbot($this);
175180
}
176181

177182
/**
178183
* Creates an Analyze API interface
179184
*
180-
* @param $url string Url to analyze
185+
* @param string $url Url to analyze
181186
* @return Analyze
182187
*/
183188
public function createAnalyzeAPI($url)
184189
{
185190
$api = new Analyze($url);
186191
if (!$this->getHttpClient()) {
187192
$this->setHttpClient();
193+
$this->setEntityFactory();
188194
}
189195
return $api->registerDiffbot($this);
190196
}
191197

198+
/**
199+
* Creates an Discussion API interface
200+
*
201+
* @param string $url Url to analyze
202+
* @return Discussion
203+
*/
204+
public function createDiscussionAPI($url)
205+
{
206+
$api = new Discussion($url);
207+
if (!$this->getHttpClient()) {
208+
$this->setHttpClient();
209+
$this->setEntityFactory();
210+
}
211+
return $api->registerDiffbot($this);
212+
}
213+
214+
/**
215+
* Creates a generic Custom API
216+
*
217+
* Does not have predefined Entity, so by default returns Wildcards
218+
*
219+
* @param string $url Url to analyze
220+
* @param string $name Name of the custom API, required to finalize URL
221+
* @return Custom
222+
*/
223+
public function createCustomAPI($url, $name)
224+
{
225+
$api = new Custom($url, $name);
226+
if (!$this->getHttpClient()) {
227+
$this->setHttpClient();
228+
$this->setEntityFactory();
229+
}
230+
return $api->registerDiffbot($this);
231+
}
192232

193233
}

src/Entity/Article.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ class Article extends Entity
99
{
1010
use StandardEntity;
1111

12+
/** @var Discussion */
13+
protected $discussion = null;
14+
15+
public function __construct(array $data)
16+
{
17+
parent::__construct($data);
18+
if (isset($this->data['discussion'])) {
19+
$this->discussion = new Discussion($this->data['discussion']);
20+
}
21+
}
22+
1223
/**
1324
* Should always return "article"
1425
* @return string
@@ -181,4 +192,13 @@ public function getVideos()
181192
{
182193
return (isset($this->data['videos'])) ? $this->data['videos'] : [];
183194
}
195+
196+
/**
197+
* Returns the Discussion entity - comments of the article
198+
* @return Discussion
199+
*/
200+
public function getDiscussion()
201+
{
202+
return $this->discussion;
203+
}
184204
}

0 commit comments

Comments
 (0)