Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

lawnstarter/laravel-darksky

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel DarkSky

Software License

This provides a Laravel style wrapper for the DarkSky api and simplifies writing tests against ever changing weather data. For more information regarding request and response formats, visit: https://darksky.net/dev/docs

Install

Require this package with composer using the following command:

$ composer require lawnstarter/laravel-darksky

After updating composer, add the service provider to the providers array in config/app.php

Lawnstarter\LaravelDarkSky\LaravelDarkSkyServiceProvider::class,

To register a facade accessor, add the following to config/app.php aliases array

'DarkSky' => \Lawnstarter\LaravelDarkSky\Facades\DarkSky::class,

Configuration

Add the following line to the .env file:

DARKSKY_API_KEY=<your_darksky_api_key>

Usage

For full details of response formats, visit: https://darksky.net/dev/docs/response

Required

location(lat, lon)

Pass in latitude and longitude coordinates for a basic response

DarkSky::location(lat, lon)->get();

Optional Parameters

For full details of optional parameters, visit: https://darksky.net/dev/docs/forecast

excludes([]) / includes([])

Specify which data blocks to exclude/include to reduce data transfer

DarkSky::location(lat, lon)->excludes(['minutely','hourly', 'daily', 'alerts', 'flags'])->get();
DarkSky::location(lat, lon)->includes(['currently'])->get();
// Same output
atTime(t)

Pass in a unix timestamp to get forecast for that time. Note: the timezone is relative to the given location

DarkSky::location(lat, lon)->atTime(timestamp)->get();
language(l)

Specify a language for text based responses

DarkSky::location(lat, lon)->language(lang)->get();
units(u)

Specify units for unit based responses

DarkSky::location(lat, lon)->units(units)->get();
extend()

Extend the "hourly" response from 48 to 168 hours. Note: Does not work if used with an atTime() timestamp. Please see: https://darksky.net/dev/docs/time-machine

DarkSky::location(lat, lon)->extend()->get();

Helpers

The following are shorthand helpers to add readability equal to using includes() with only one parameter. Note: only one may be used per query and only temperature specific data is returned

->currently()
->minutely()
->hourly()
->daily()
->flags()

For example, these two statements are the same

DarkSky::location(lat, lon)->hourly()
DarkSky::location(lat, lon)->includes(['hourly'])->get()->hourly

DarkSky & Testing

To simplyfiy testing the static method to force the response to be a certain payload without actually hitting the DarkSky API was added.

DarkSky::setTestResponse($testResponseValue);

When this value is not null, the DarkSky wrapper will always return this data. If the value is null, the DarkSky API will be queried in real-time. To simplify testing further, sample test responses are available for you to use in your test classes

<?php

use Lawnstarter\LaravelDarkSky\DarkSkySampleResponse;
use Lawnstarter\LaravelDarkSky\DarkSky;

class MyTestsDependOnDarksky extends TestCase {

    public function test_forecast() {
        ...
        $fakeForecast = DarkSkySampleResponse::forecast();
        DarkSky::setTestResponse($fakeForecast);
        ...
    }

    public function test_forecast_extended_hourly() {
        ...
        $fakeForecast = DarkSkySampleResponse::forecastExtendedHourly();
        DarkSky::setTestResponse($fakeForecast);
        ...
    }

    public function test_timemachine() {
        ...
        $fakeForecast = DarkSkySampleResponse::timemachine();
        DarkSky::setTestResponse($fakeForecast);
        ...
    }

}
Method Sample Payload
DarkSkySampleResponse::forecast() See Sample Forecast JSON
DarkSkySampleResponse::forecastExtendedHourly() See Sample Forecast Extended Hourly JSON
DarkSkySampleResponse::timemachine() See Sample Time Machine JSON

Credits

License

The MIT License (MIT). Please see License File for more information.

About

DarkSky Wrapper for Laravel

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%