Degree Days

Degree Days

Weather Data for Energy Professionals

Access the Degree Days.net API using Ruby

This page has some sample code showing how to use Ruby to specify an JSON request, send it to our API servers (past the security system), and process the JSON response that comes back.

It's not a full client library (like we have for Java, .NET, and Python), but, thanks to Ruby's excellent support for JSON, it should be pretty easy to adapt it to suit your needs.

See the JSON API docs for more about specifying the data you want in the JSON request. There are lots of options, and the request included in the Ruby code below is just a simple example. The JSON docs also explain more about the data you can expect back in the response.

You might also find the JSON API test tool useful for testing different JSON requests and seeing the JSON responses that come back.

Many thanks to Thiago Nuic from Wattics who helped get us started with this Ruby/JSON sample by sending us a Ruby/XML sample that we published here for quite some time before we released the JSON API.

require 'time'
require 'securerandom'
require 'json'
require 'openssl'
require 'base64'
require 'net/http'

# The test API access keys are described at www.degreedays.net/api/test
# They will let you access data from the Cape Cod area only.
# To fetch data from locations worldwide, sign up for a proper API account at
# www.degreedays.net/api/ and copy your API access keys here.
account_key = 'test-test-test'
security_key = 'test-test-test-test-test-test-test-test-test-test-test-test-test'
# You can call the API over HTTP using http://apiv1.degreedays.net/json or
# over HTTPS using https://apiv1.degreedays.net/json - set the endpoint URL
# below as appropriate.
endpoint = 'http://apiv1.degreedays.net/json'



# ************* STEP 1: Create the request ************************************
# First we create a JSON request that specifies what we want from the API.
# See www.degreedays.net/api/json#request for more on this.

# You can fetch data from a station ID, a longitude/latitude position, or a 
# postal/zip code.
location = {
  'type' => 'PostalCodeLocation',
  'postalCode' => '02532',
  'countryCode' => 'US'
}
# In this example we fetch both HDD and CDD, using the same breakdown (daily
# data covering the last 7 days) for both. For more breakdown options see:
# www.degreedays.net/api/json#breakdown
breakdown = {
  'type' => 'DailyBreakdown',
  'period' => {
    'type' => 'LatestValuesPeriod',
    'numberOfValues' => 7
  }
}
location_data_request = {
  'type' => 'LocationDataRequest',
  'location' => location,
  'dataSpecs' => {
    # Here we specify 2 DataSpec items: one for HDD and one for CDD.  You can
    # specify up to 100 DataSpec items in one request (e.g. to fetch data in
    # lots of base temperatures).  Give each DataSpec a unique name so you can
    # get the corresponding DataSet from the response.
    'myHDD' => {
      'type' => 'DatedDataSpec',
      'calculation' => {
        'type' => 'HeatingDegreeDaysCalculation',
        'baseTemperature' => {
          'unit' => 'F',
          'value' => 60
        }
      },
      'breakdown' => breakdown
    },
    'myCDD' => {
      'type' => 'DatedDataSpec',
      'calculation' => {
        'type' => 'CoolingDegreeDaysCalculation',
        'baseTemperature' => {
          'unit' => 'F',
          'value' => 70
        }
      },
      'breakdown' => breakdown
    }
  }
}
full_request = {
  'securityInfo' => {
    'endpoint' => endpoint,
    'accountKey' => account_key,
    'timestamp' => Time.now.utc.iso8601,
    'random' => SecureRandom.uuid
  },
  'request' => location_data_request
}
full_request_json = JSON.generate(full_request)
# Now our JSON request is ready.  Uncomment the line below to see the JSON:
#puts full_request_json



# ************* STEP 2: Send the request to the API ***************************
# Next we sign the JSON request and package everything together into an HTTP
# request which we send to the Degree Days.net API.  This follows the spec at
# www.degreedays.net/api/json#send (though you can just copy/paste).
signature_bytes = OpenSSL::HMAC.digest(
  OpenSSL::Digest.new('sha256'), security_key, full_request_json)
# Prepare the HTTP request parameters to send to the API servers:
request_parameters = {
  'request_encoding' => 'base64url',
  'signature_method' => 'HmacSHA256',
  'signature_encoding' => 'base64url',
  'encoded_request' => Base64.urlsafe_encode64(full_request_json),
  'encoded_signature' => Base64.urlsafe_encode64(signature_bytes)
}
# Send the HTTP request to the API servers:
full_response_json = Net::HTTP.post_form(URI(endpoint), request_parameters).body



# ************* STEP 3: Process the response from the API *********************
# The JSON response is explained at www.degreedays.net/api/json#response

# Uncomment the line below to see the JSON response:
#puts full_response_json

# Use JSON.parse to turn the JSON response into an object:
full_response = JSON.parse(full_response_json, object_class: OpenStruct)
# full_response.metadata has some rate limit info, but we are mainly interested
# in full_response.response (a LocationDataResponse in this case).
response = full_response.response
if response.type == 'Failure'
  # See www.degreedays.net/api/json#failure for more about failures.
  puts "Request failure: #{response}"
else
  # The response contains a lot of useful info.  See above to view the JSON, or
  # check the JSON docs at www.degreedays.net/api/json#response
  puts "Station ID: #{response.stationId}"
  # "myHDD" is the name we gave the HDD DataSpec in our request.
  hdd_data = response.dataSets.myHDD
  if hdd_data.type == 'Failure'
    puts "Failure for HDD DataSet: #{hdd_data}"
  else
    puts 'HDD:'
    hdd_data.values.each { |v|
      puts "#{v.d}: #{v.v}"
    }
  end
  cdd_data = response.dataSets.myCDD
  if cdd_data.type == 'Failure'
    puts "Failure for CDD DataSet: #{cdd_data}"
  else
    puts 'CDD'
    cdd_data.values.each { |v|
      puts "#{v.d}: #{v.v}"
    }
  end
end

Hopefully the example code above, the JSON API docs, and the JSON API test tool will be enough to get you going, but please feel free to email us if you'd like more help.

It is also worth reading the higher-level integration guide for tips on the various approaches to integrating with the API. We have helped a lot of businesses integrate their software with our API so we are very familiar with the patterns that work well for common use cases.

Choose your Plan and Sign Up Today!

© 2019 BizEE Software Limited - About | Contact | Privacy | Web Tool | API | Integration Guide | API FAQ | API Sign-Up