class Ruboty::Weather::Client

Constants

LIVEDOOR_WEATHER_API_URL

Public Class Methods

new() click to toggle source
# File lib/ruboty/weather/client.rb, line 9
def initialize
  @client = Faraday.new do |connection|
    connection.adapter :net_http
    connection.response :json
  end
end

Public Instance Methods

get(city_code=default_city) click to toggle source
# File lib/ruboty/weather/client.rb, line 16
def get(city_code=default_city)
  response = @client.get("#{url}?city=#{city_code}").body

  forecasts = response['forecasts'].map do |forecast|
    "#{forecast['date']}: #{emojilize!(forecast['telop'])} (#{temperature_to_s(forecast['temperature'])})"
  end
  forecasts.join("\n")
end

Private Instance Methods

default_city() click to toggle source
# File lib/ruboty/weather/client.rb, line 31
def default_city
  ENV['RUBOTY_WEATHER_CITY'] || 130010 #tokyo
end
emojilize!(telop) click to toggle source
# File lib/ruboty/weather/client.rb, line 35
def emojilize!(telop)

  telop.gsub!(/時々/, "\u{2194}")
  telop.gsub!(/のち/, "\u{27A1}")

  emojis = {
    "晴れ{,1}" => "\u{2600}",
    "曇り{,1}" => "\u{2601}",
    "雨" => "\u{2614}",
    "雷" => "\u{26A1}",
    "雪" => "\u{26C4}",
  }.each do |k, v|
    telop.gsub!(/#{k}/, v)
  end

  telop
end
temperature_to_s(temperature) click to toggle source
# File lib/ruboty/weather/client.rb, line 53
def temperature_to_s(temperature)
  min = (temperature['min'] ? temperature['min']['celsius'] : '-')
  max = (temperature['max'] ? temperature['max']['celsius'] : '-')

  "#{min}/#{max}"
end
url() click to toggle source
# File lib/ruboty/weather/client.rb, line 27
def url
  LIVEDOOR_WEATHER_API_URL
end