Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn committed May 26, 2021
1 parent 52d5551 commit d8e80d6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ protected static function ensureHttpClientIsLoaded(): TwitterClient

public static function all(): array
{
$rules = static::ensureHttpClientIsLoaded()
->request('GET', 'https://api.twitter.com/2/tweets/search/stream/rules');

$rules = static::ensureHttpClientIsLoaded()->request('GET', 'https://api.twitter.com/2/tweets/search/stream/rules');
return array_map(static function ($rawRule) {
$rule = new self($rawRule['value'], $rawRule['tag']);
$rule->withId($rawRule['id']);

return $rule;
}, $rules['data']);
}, $rules['data'] ?? []);
}

public function withId(string $id): static
Expand Down
3 changes: 2 additions & 1 deletion src/RuleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ public function raw(string $expression): static

public function save(string $tag = null): Rule
{
return Rule::create($compiled = $this->compile(), $tag ?? $compiled);
$compiled = $this->compile();
return Rule::create($compiled, $tag ?? $compiled);
}
}
23 changes: 12 additions & 11 deletions src/TwitterStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RWC\TwitterStream;

use Error;
use Generator;
use GuzzleHttp\Client;
use Psr\Http\Message\StreamInterface;
Expand Down Expand Up @@ -38,20 +39,14 @@ public function filteredTweets(Sets $sets = null): Generator
};

while ($shouldKeepListening()) {
$char = $this->streamConnection->read(2);
$char = $this->streamConnection->read(1) ;
$tweet = $char;

while ($char !== "\r\n") {
$char = $this->streamConnection->read(2);
while ($char !== "\n" && $tweet[-1] !== "\r") {
$char = $this->streamConnection->read(1);
$tweet .= $char;
}

$tweet = trim($tweet);

if (empty($tweet)) {
continue;
}

$decoded = json_decode($tweet, true);

if ($decoded) {
Expand All @@ -65,13 +60,19 @@ protected function connectToFilteredStream(Sets $sets = null): StreamInterface
// Could use the null object pattern
$sets ??= new Sets();

return $this->httpClient->stream('GET', 'https://api.twitter.com/2/tweets/search/stream?' . $sets)
return $this->httpClient
->stream('GET', 'https://api.twitter.com/2/tweets/search/stream?' . $sets)
->getBody();
}

public function __destruct()
{
$this->stopListening();
// If the connection was never initialized, this throws an error.
try {
$this->stopListening();
} catch (Error) {

}
}

public function stopListening(): self
Expand Down

0 comments on commit d8e80d6

Please sign in to comment.