Class: Anemoi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/anemoi/client.rb

Constant Summary collapse

WEATHER_URL =
"http://weather.livedoor.com/forecast/webservice/rest/v1"
AREA_TABLE_URL =
"http://weather.livedoor.com/forecast/rss/forecastmap.xml"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
# File 'lib/anemoi/client.rb', line 10

def initialize(options={})
  options[:default_city] ||= "東京"
  @default_city = AreaParser.parse(options[:default_city])
  @default_date = options[:default_date] || :today
end

Instance Method Details

#get_weather(text, opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/anemoi/client.rb', line 16

def get_weather(text, opts = {})
  specified_city = AreaParser.parse(text)
  specified_date = horai_date_parse(text)
  city  = specified_city || @default_city
  date  = specified_date || @default_date
  query = { :city => city[:id], :day => date }.merge(opts)
  hash  = get WEATHER_URL, :query => query

  {
    :specified => {
      :date => specified_date,
      :city => specified_city
    },
    :forecastday  => date,
    :forecastdate => hash["lwws"]["forecastdate"],
    :day          => hash["lwws"]["day"],
    :location     => hash["lwws"]["location"],
    :weather      => hash["lwws"]["telop"],
    :description  => hash["lwws"]["description"],
    :temperature  => hash["lwws"]["temperature"]
  }.deep_symbolize_keys
rescue ParseError
  nil
end

#horai_date_parse(text) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/anemoi/client.rb', line 41

def horai_date_parse(text)
  @jajp ||= Horai::JaJP.new
  date = @jajp.parse(text)
  now = Date.today
  case date
  when now + 0; :today
  when now + 1; :tomorrow
  when now + 2; :day_after_tomorrow
  else nil
  end
end