Macros - matical/nana GitHub Wiki

Consume is macroable.

Examples

Custom status code asserts

Consume::macro('isTeapot', function () {
    return $this->status() === 418;
});

$response = Nana::get('https://httpbin.org/status/418');

$response->isTeapot(); // true

http2 Parsing

Consume::macro('preloadHeaders', function () {
    return \GuzzleHttp\Psr7\parse_header($this->header('Link'));
});

$response = Nana::get('<h2 site with asset push>');

$response->preloadHeaders();
// => [                                                 
//      [                                               
//        0 => "<https://.../favicon.png>",    
//        "rel" => "preload",                           
//        "as" => "image",                              
//      ],                                              
//      [                                               
//        0 => "</css/app.css?id=...>",
//        "rel" => "preload",                           
//        "as" => "style",                              
//      ],                                              
//      [                                               
//        0 => "</js/app.js?id=...>",  
//        "rel" => "preload",                           
//        "as" => "script",                             
//      ],                                              
//    ]                                                 

Storing files with Laravel/flysystem

Instead of using saveTo() (which makes use of Guzzle's sink), you can pass the resource directly to flysystem.

AppServiceProvider

use ksmz\nana\Consume;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Storage;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Consume::macro('store', function ($name) {
            // https://laravel.com/docs/5.6/filesystem#storing-files
            return Storage::put($name, $this->stream());
        });
    }
}

somewhere else

Nana::get('https://picsum.photos/200')
    ->store('picture.jpg')