module Snotel

Constants

VERSION

Public Class Methods

build_uri(triplet, readings, granularity = :daily) click to toggle source
# File lib/snotel.rb, line 60
def self.build_uri(triplet, readings, granularity = :daily)
  URI(
    "http://www.wcc.nrcs.usda.gov/reportGenerator/view_csv/customSingleStationReport"\
    "/#{granularity}/#{triplet}%7Cid%3D%22%22%7Cname/-#{readings}%2C0/WTEQ%3A%3Avalue%2CWTEQ%3A%3A"\
    "delta%2CSNWD%3A%3Avalue%2CSNWD%3A%3Adelta"
  )
end
daily(token, readings = 1) click to toggle source
# File lib/snotel.rb, line 18
def self.daily(token, readings = 1)
  get_daily_data(get_triplet_from_token(token), readings-1)  # Subtract to account for delta.
end
get_daily_data(triplet, readings) click to toggle source
# File lib/snotel.rb, line 26
def self.get_daily_data(triplet, readings)
  get_data(triplet, readings, :daily)
end
get_data(triplet, readings, granularity = :daily) click to toggle source

The majority of this functionality is taken directly from: github.com/bobbymarko/powderlines-api

# File lib/snotel.rb, line 36
def self.get_data(triplet, readings, granularity = :daily)
  uri  = build_uri(triplet, readings, granularity)
  json = Net::HTTP.get(uri)

  lines = CSV.parse(remove_comments(json))

  if lines.empty?
    puts "WARNING: No data found for #{ triplet }."
    return
  end

  keys = lines.delete(lines.first).map do |key|
    key.gsub(/[^0-9a-z ]+/i, '').gsub(/ /, '_').downcase.to_sym
  end

  parse_csv_data(keys, lines)
end
get_hourly_data(triplet, readings) click to toggle source
# File lib/snotel.rb, line 30
def self.get_hourly_data(triplet, readings)
  get_data(triplet, readings, :hourly)
end
get_station_data() click to toggle source
# File lib/snotel.rb, line 10
def self.get_station_data
  Snotel::Stations::ALL
end
get_triplet_from_token(token) click to toggle source
# File lib/snotel.rb, line 14
def self.get_triplet_from_token(token)
  Snotel::Tokens::TOKENS[token] || raise("Token #{ token } not found.")
end
hourly(token, readings = 24) click to toggle source
# File lib/snotel.rb, line 22
def self.hourly(token, readings = 24)
  get_hourly_data(get_triplet_from_token(token), readings-1)  # Subtract to account for delta.
end
parse_csv_data(keys, lines) click to toggle source
# File lib/snotel.rb, line 54
def self.parse_csv_data(keys, lines)
  lines.map do |values|
    hash = Hash[keys.zip(values.map { |v| v || 0 })]
  end
end
remove_comments(json) click to toggle source
# File lib/snotel.rb, line 68
def self.remove_comments(json)
  json.gsub(/(^#.+|#)/, '').gsub(/^\s+/, "")
end