Class: WeatherUnderground::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/weather-underground/uploader.rb

Overview

This is an uploader for the weather underground personal weather station service, as found at wunderground.com. This makes it easier to build custom software to upload to the service allowing greater flexibility in the number of different weather sensors you can use.

It is build from the documentation provided at wiki.wunderground.com/index.php/PWS_-_Upload_Protocol

Constant Summary collapse

BaseURL =

This is the base url for uploading

"http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php"

Instance Method Summary collapse

Constructor Details

#initialize(station, passwd) ⇒ Uploader

Create an uploader object. Station is your registered station id from wunderground, password is the password you created for it.



20
21
22
23
# File 'lib/weather-underground/uploader.rb', line 20

def initialize(station, passwd)
    @station = station
    @passwd = passwd
end

Instance Method Details

#update(data = {}) ⇒ Object

Update with data. Data is of the form of :name => value for the parameters shown here: wiki.wunderground.com/index.php/PWS_-_Upload_Protocol.

The required parameters are automatically filled in by the module, just provide the data that you want to upload.

w.update(:temp => 67.5, :humidity => 40)


47
48
49
50
51
52
# File 'lib/weather-underground/uploader.rb', line 47

def update(data = {})
    open(url(data)) do |f|
        # TODO: check the response
        
    end
end

#url(params = {}) ⇒ Object

Build the url to use for the upload. You probably don’t want to call this directly, though there is no harm in doing so.



28
29
30
31
32
33
34
35
36
37
# File 'lib/weather-underground/uploader.rb', line 28

def url(params = {})
    base = "#{BaseURL}?ID=#{@station}&PASSWORD=#{@passwd}"
    base += "&dateutc=#{URI.escape(Time.now.utc.strftime("%Y-%m-%d %H:%M:%S"))}"
    base += "&action=updateraw"
    # we sort them just so we get predictable order for tests
    params.sort {|a, b| a.to_s <=> b.to_s}.each do |k, v|
        base += "&#{k}=#{URI.escape(v.to_s)}"
    end
    return base
end