class Barabara::Modules::Wttr

Public Class Methods

new() click to toggle source
# File lib/barabara/modules/wttr.rb, line 10
def initialize
  options = GlobalConfig.config.module_config('weather')
  @colors = GlobalConfig.config.colors
  @location = options['location'] || 'London'
  @unit = (options['unit'] || 'c') == 'c' ? 'm' : 'u'
  @format = options['format']
  @uri = format_uri
end

Public Instance Methods

fetch() click to toggle source
# File lib/barabara/modules/wttr.rb, line 26
def fetch
  Net::HTTP.get_response(@uri)
rescue SocketError
  '⌚'
end
format_uri() click to toggle source
# File lib/barabara/modules/wttr.rb, line 19
def format_uri
  uri = URI("https://wttr.in/#{@location}")
  query = URI.encode_www_form(@unit => nil, 'format' => "%c\t%t")
  uri.query = query
  uri
end
parse!() click to toggle source
# File lib/barabara/modules/wttr.rb, line 32
def parse!
  @icon, rawtemp = @raw_data.body.split("\t")
  @temp = rawtemp.to_i
end
render() click to toggle source
# File lib/barabara/modules/wttr.rb, line 37
def render
  @raw_data = fetch
  return '⌚' unless @raw_data.is_a?(Net::HTTPSuccess)

  parse!
  sign = '+' if @temp.positive?
  format(
    @format,
    { temp: "#{sign}#{@temp}", icon: @icon }.merge(@colors)
  ).force_encoding('utf-8')
end
watch() click to toggle source
# File lib/barabara/modules/wttr.rb, line 49
def watch
  loop do
    publish(:event, 'weather', render)
    sleep 900
  end
end