Skip to content

jguthrie100/bbc_weather

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bbc_weather - A wrapper for scraping weather data from the BBC Weather website

Installation

Latest version 0.1.0

Add the following to your Gemfile

    gem 'bbc_weather'

or

    $ gem install bbc_weather

Usage

Load the weather data for a specific city

A successful search for a city returns a WeatherResult object containing forecast data

require 'bbc_weather'

# Load city using city name
weather = BBCWeather.city("Amsterdam, Netherlands")

# Load city using city id
weather = BBCWeather.city(2759794)

Access the current weather data for the city

# Get current weather in the city (in °C)
weather.current_temp
    > 17

# Get current humidity (in %)
weather.current_humidity
    > 71

# Get timestamp of the current time in the city
weather.current_time
    > "2017-06-12T13:14:56+02:00"

Access forecast data for the current and coming days

Each day of the forecast is represented as a Day object that contains all the weather data for the day, and which can be accessed in a number of ways:

# Get today's weather data (today being the selected city's today)
weather.today

# Get tomorrow's weather data
weather.tomorrow

# Get the weather data for two days time
weather.days_forward(2)

# Get the weather data for the 13th June
weather.on("2017-06-13")

# Get the weather data for Wednesday
weather.on("Wednesday")

# Get the weather data for the 12th June
weather.on(DateTime.parse("2017-06-12T23:54"))

Access general data for the selected day

Once you have the Day object, you can access various general weather data of the day

# Get the date
weather.tomorrow.date
    > <#Date: 2017-06-13>

# Get the sunrise and sunset times for the day
weather.today.sunrise
    > "05:18"

weather.on("Friday").sunset
    > 22:03

# Get the max temperature of the day (in °C)
weather.on("2017-06-13").high
    > 22

# Get the min temperature of the day (in °C)
weather.today.low
    > 13

Accessing more specific weather data

Each Day object is split into a number of TimeSlot objects. These TimeSlot objects each contain weather data for a specific time of the day (timeslots are usually in 1 hour or 3 hour intervals)

The TimeSlot objects can be accessed in the following ways..

# Get the timeslot closest to 13:00 today
weather.today.at("13:00")

# Get the timeslot closest to 23:45 on the 17th June
weather.on("2017-06-17").at(DateTime.parse("2017-06-17T23:45"))

Each TimeSlot also contains a reference to the next and previous chronologically order timeslots which can be accessed using the #next and #prev methods:

weather.today.at("23:00").next
weather.tomorrow.at("01:00").prev

weather.today.at("23:00").next == weather.tomorrow.at("01:00").prev

Each TimeSlot then contains the relevant forecast data for that time

# Get the pressure (in millibars) at 12:23 on Tuesday
weather.on("Tues").at("12:23").pressure
    > 1102

# Get the weather conditions at 03:00 on the 12th June
weather.on("2017-06-12").at("03:00").conditions
    > "Light Cloud"

The available weather selectors are:

ts = weather.on("2017-06-12").at("03:00")

# Get time
ts.time
    > #<DateTime: 2017-06-13T02:00..>

# Get temperature (in °C)
ts.temperature
    > 12

# Get humidity (in %)
ts.humidity
    > 56

# Get visibility
ts.visibility
    > "Very Good"

# Get pressure (in millibars)
ts.pressure
    > 1012

# Get windspeed (in mph)
ts.wind_speed
    > 12

# Get wind direction (as in direction wind is coming from)
ts.wind_direction
    > "SW"

# Get description of conditions
ts.conditions
    > "Light Cloud"

# Get the url of a graphical icon of the weather
ts.icon_url
    > "http://static.bbci.co.uk/weather/.../en_on_light_bg/7.gif"

Changing units from mph to km/h and from °C to °F

The units pertaining to wind speed and temperature can be changed from mph/kph and °C/°F using the BBCWeather.set_units static method (which dynamically works out which unit you want to set based on pattern matching your input). The default units are °C and mph. After setting the units, all temperature and wind speed calls will return the value according to the new units

# Get units - returns an Array
BBCWeather.units
    > ["c", "mph"]

# Set speed to kph
BBCWeather.set_units "kph"
    > ["c", "kph"]

# Set temp to °F and speed to mph
BBCWeather.set_units "f", "mph"
    > ["f", "mph"]

# Set speed to kph
BBCWeather.set_units "kph"
    > ["f", "kph"]

# Get a temperature (which is now set to °F)
weather.current_temp
    > 59

# Get windspeed (which is now set to kph)
weather.today.at("00:00").wind_speed
    > 68

About

Ruby gem to scrape weather forecast data from the BBC

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published