Skip to content

Commit

Permalink
Merge pull request #10 from adam-riha/master
Browse files Browse the repository at this point in the history
Fix logic operands + Twitter error response message
  • Loading branch information
felixdorn authored Jul 5, 2022
2 parents 7cba036 + 3b0c703 commit e680d32
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ composer require redwebcreation/twitter-stream-api

```php
use RWC\TwitterStream\Connection;
use RWC\TwitterStream\TwitterStream;
use RWC\TwitterStream\FilteredStream;
use RWC\TwitterStream\VolumeStream;

$twitterStream = new FilteredStream();
// or
$twitterStream = new VolumeStream();

$twitterStream = new TwitterStream();
$connection = new Connection(
bearerToken: '...'
);
Expand All @@ -39,9 +43,9 @@ $rule->new(tag: 'cat_filter_1')

$twitterStream
->backfill(2) // for "academic research" accounts only
->expansions(['author_id'])
->fields(['media.duration_ms', 'media.height'])
->fields(['place.full_name', 'place.geo', 'place.id'])
->expansions('author_id')
->fields('media.duration_ms', 'media.height')
->fields('place.full_name', 'place.geo', 'place.id')
->listen($connection, function (array $tweet) {
echo $tweet['data']['text'];

Expand All @@ -59,7 +63,7 @@ Tweets your stream receives. Rules are saved by Twitter and are persistent.
### Client

```php
use RWC\TwitterStream\RuleBuilder;
use RWC\TwitterStream\RuleManager;

$rules = new RuleManager($connection);
```
Expand Down Expand Up @@ -142,8 +146,9 @@ You can also group operators together :

```php
use RWC\TwitterStream\RuleBuilder;

$rules->query('#laravel')
->group(function (RluleBuilder $builder) {
->group(function (RuleBuilder $builder) {
$builder->raw('tip')->or->raw('tips')->or->raw('🔥');
});

Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/TwitterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public static function fromPayload(array $payload): TwitterException
// For most errors related to rule creation, details are provided.
$hasDetails = array_key_exists('details', $error);

$buffer .= sprintf("[%s] %s %s [%s]\n", $error['title'], $error['value'], $hasDetails ? ': ' . implode('; ', $error['details']) : '', $error['id']);
$errorIdOrType = $error['id'] ?? ($error['type'] ?? 'Generic Problem');

$buffer .= sprintf("[%s] %s %s [%s]\n", $error['title'], $error['value'], $hasDetails ? ': ' . implode('; ', $error['details']) : '', $errorIdOrType);
}

return new self(
Expand Down
2 changes: 1 addition & 1 deletion src/Operators/GroupOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function compile(): string
}

$stack->add($index, new RawOperator([
Flag::has($this->flags, self::OR_OPERATOR) ? 'or' : 'and',
Flag::has($this->flags, self::OR_OPERATOR) ? 'OR' : 'AND',
]));
}

Expand Down
4 changes: 2 additions & 2 deletions src/RuleBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function __construct(
public function __get(string $name): self
{
match ($name) {
'and' => $this->push(new RawOperator(['and'])),
'or' => $this->push(new RawOperator(['or'])),
'and' => $this->push(new RawOperator(['AND'])),
'or' => $this->push(new RawOperator(['OR'])),
/* @see https://wiki.php.net/rfc/undefined_property_error_promotion */
default => trigger_error('Undefined property: ' . static::class . '::$' . $name, PHP_MAJOR_VERSION === 8 ? E_USER_WARNING : E_USER_ERROR)
};
Expand Down
18 changes: 9 additions & 9 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,33 @@ function query(string $q = '')
});

it('can compile a query with operators linked by OR', function () {
expect(query('php')->lang('en')->or->has('images')->compile())->toBe('php lang:en or has:images');
expect(query('php')->lang('en')->or->has('images')->compile())->toBe('php lang:en OR has:images');
});

it('can compile a query with operators linked by AND', function () {
expect(query('php')->lang('en')->and->has('images')->compile())->toBe('php lang:en and has:images');
expect(query('php')->lang('en')->and->has('images')->compile())->toBe('php lang:en AND has:images');
});

it('can compile a query with operators linked by OR and AND', function () {
expect(query('php')->lang('en')->or->has('images')->and->has('videos')->compile())->toBe('php lang:en or has:images and has:videos');
expect(query('php')->lang('en')->or->has('images')->and->has('videos')->compile())->toBe('php lang:en OR has:images AND has:videos');
});

it('can compile a query with a group', function () {
expect(query('php')->group(function (RuleBuilder $b) {
$b->lang('en')->and->has('images');
})->compile())->toBe('php (lang:en and has:images)');
})->compile())->toBe('php (lang:en AND has:images)');
});

it('can compile a query with an or group', function () {
expect(query('cats')->has('images')->or->group(function (RuleBuilder $b) {
return $b->lang('en')->and->has('videos');
})->compile())->toBe('cats has:images or (lang:en and has:videos)');
})->compile())->toBe('cats has:images OR (lang:en AND has:videos)');
});

it('can compile a query with an and group', function () {
expect(query('cats')->has('images')->and->group(function (RuleBuilder $b) {
return $b->lang('en')->or->has('videos');
})->compile())->toBe('cats has:images and (lang:en or has:videos)');
})->compile())->toBe('cats has:images AND (lang:en OR has:videos)');
});

it('can compile a point radius operator', function () {
Expand Down Expand Up @@ -155,21 +155,21 @@ function query(string $q = '')
return $b->raw('cats')->raw('dogs');
})->compile();

expect($rule)->toBe('(cats or dogs)');
expect($rule)->toBe('(cats OR dogs)');

$rule = query()
->andGroup(function (RuleBuilder $b) {
return $b->raw('cats')->raw('dogs');
})->compile();

expect($rule)->toBe('(cats and dogs)');
expect($rule)->toBe('(cats AND dogs)');

$rule = query()
->andNotGroup(function (RuleBuilder $b) {
return $b->raw('cats')->raw('dogs')->isQuote();
})->compile();

expect($rule)->toBe('(cats and dogs and -is:quote)');
expect($rule)->toBe('(cats AND dogs AND -is:quote)');
});

it('can compile a sample operator', function () {
Expand Down

0 comments on commit e680d32

Please sign in to comment.