module Forecast::Utils

Public Class Methods

celsius_to_fahrenheit(celsius) click to toggle source
# File lib/forecast/utils.rb, line 41
def celsius_to_fahrenheit(celsius)
  celsius * 1.8 + 32 
end
celsius_to_kelvin(celsius) click to toggle source
# File lib/forecast/utils.rb, line 45
def celsius_to_kelvin(celsius)
  celsius + 273.15
end
fahrenheit_to_celsius(fahrenheit) click to toggle source
# File lib/forecast/utils.rb, line 29
def fahrenheit_to_celsius(fahrenheit)
  ((fahrenheit - 32) / 1.8)
end
fahrenheit_to_kelvin(fahrenheit) click to toggle source
# File lib/forecast/utils.rb, line 25
def fahrenheit_to_kelvin(fahrenheit)
  ((fahrenheit - 32) / 1.8) - 273.15
end
get_doc(url, params = {}) click to toggle source
# File lib/forecast/utils.rb, line 63
def get_doc(url, params = {})
  if params.keys.count > 0
    query_string = URI.encode_www_form(params)
    url = url + "?" + query_string
  end
  xml_data = Net::HTTP.get_response(URI.parse(url)).body
  doc = REXML::Document.new(xml_data)
  return doc
end
get_json(url, params = {}) click to toggle source
# File lib/forecast/utils.rb, line 49
def get_json(url, params = {})
  if params.keys.count > 0
    query_string = URI.encode_www_form(params)
    url = url + "?" + query_string
  end
  resp = Net::HTTP.get_response(URI.parse(url))
  data = resp.body
  result = JSON.parse(data)
  if result && result['cod'] != "404"
    return result
  end
  return nil
end
kelvin_to_celsius(kelvin) click to toggle source
# File lib/forecast/utils.rb, line 37
def kelvin_to_celsius(kelvin)
  kelvin - 273.15
end
kelvin_to_fahrenheit(kelvin) click to toggle source
# File lib/forecast/utils.rb, line 33
def kelvin_to_fahrenheit(kelvin)
  return ((kelvin - 273.15) * 1.8 + 32)
end
levenshtein(first, second) click to toggle source
# File lib/forecast/utils.rb, line 104
def levenshtein(first, second)
  matrix = [(0..first.length).to_a]
  (1..second.length).each do |j|
    matrix << [j] + [0] * (first.length)
  end
  (1..second.length).each do |i|
    (1..first.length).each do |j|
      if first[j-1] == second[i-1]
        matrix[i][j] = matrix[i-1][j-1]
      else
        matrix[i][j] = [
          matrix[i-1][j],
          matrix[i][j-1],
          matrix[i-1][j-1],
        ].min + 1
      end
    end
  end
  return matrix.last.last
end
underscore(string) click to toggle source
# File lib/forecast/utils.rb, line 11
def underscore(string)
  if string.is_a?(String)
    return string.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
  elsif string.is_a?(Hash)
    return Hash[string.map { |k, v| [Forecast::Utils.underscore(k.to_s).to_sym, v.is_a?(Hash) ? Forecast::Utils.underscore(v) : v] }]
  else
    string
  end
end
word_similarity(first, second) click to toggle source
# File lib/forecast/utils.rb, line 73
def word_similarity(first, second)
  
  if !first.is_a?(String) || !second.is_a?(String)
    return 0
  end
  similar_words = 0.0
  first_words = first.downcase.split(/\W+/)
  second_words = second.downcase.split(/\W+/)
  
  first_words.each do |first_word|
    second_words.each do |second_word|
      similar = 0.0
      if first_word == second_word
        similar = 1.0
      else
        l1 = levenshtein(first_word, second_word)
        l = 1 - (l1.to_f / ([first_word.length, second_word.length].max))
        l = [0, similar].max
        l = [similar, 1].min
        if l1 > 0.6
          similar = 0.1
        end
      end
      similar_words+= similar
    end
  end
  count = first_words.concat(second_words).uniq.length
  similarity = similar_words / count
  return similarity
end