Sha256: 475811169d88fd48a11e7462d28291454ad5f6449387aac03230f61acee7064a

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# -*- coding: utf-8 -*-

module WeatherReport
  class Day
    attr_reader :date, :telop, :temperature_min, :temperature_max

    def initialize(forecasts, dateLabel)
      raise ArgumentError, "dateLabel must be 今日, 明日 or 明後日" unless dateLabel =~ /(今日|明日|明後日)/
      @forecast = forecast(forecasts, dateLabel)
    end

    # @return [Boolean] return true if it rains.
    def rain?
      telop =~ /[雨]/ ? true : false
    end

    # @return [Boolean] return true if it snows.
    def snow?
      telop =~ /[雪]/ ? true : false
    end

    # @return [Boolean] return true if it will be rainy or snowy or sleety or hailstorm
    def umbrella?
      telop =~ /[雨雪霙雹]/ ? true : false
    end

    # @return [Date] the date
    def date
      year, month, day = @forecast["date"].split('-')
      @date ||= Date.new(year.to_i, month.to_i, day.to_i)
    end

    # @return [String] the telop
    def telop
      @telop ||= @forecast["telop"]
    end

    # @return [Fixnum] the minimum temperature.
    # Temperature of today could be nil.
    def temperature_min
      min = @forecast["temperature"]["min"]
      @temperature ||=
        min ? min["celsius"].to_i : nil
    end

    # @return [Fixnum] the maximum temperature.
    # Temperature of today could be nil.
    def temperature_max
      max = @forecast["temperature"]["max"]
      @temperature_max ||=
        max ? max["celsius"].to_i : nil
    end

    # @return [Hash] return with hash format.
    def to_h
      {
        "date" => date.to_s,
        "telop" => telop,
        "temperature_min" => temperature_min,
        "temperature_max" => temperature_max
      }
    end

    private

    def forecast(forecasts, dateLabel)
      forecasts["forecasts"].each {|elem| return elem if elem["dateLabel"] == dateLabel}
      raise NoForecastError
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
weather-report-0.4.1 lib/weather-report/day.rb
weather-report-0.4.0 lib/weather-report/day.rb