Class: Lita::Handlers::Time

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/time.rb

Constant Summary collapse

URL =
"http://api.worldweatheronline.com/free/v2/tz.ashx"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_config(handler_config) ⇒ Object



10
11
12
# File 'lib/lita/handlers/time.rb', line 10

def self.default_config(handler_config)
  handler_config.apikey = nil
end

Instance Method Details

#fetch(response) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lita/handlers/time.rb', line 14

def fetch(response)
  location = response.matches[0][0]

  apikey = Lita.config.handlers.time.apikey

  http_response = http.get(
    URL,
    q: location,
    key: apikey,
    format: 'json'
  )

  begin
    data = MultiJson.load(http_response.body)["data"]
    type  = data['request'][0]['type']
    query = data['request'][0]['query']
    time  = data['time_zone'][0]['localtime'].split(/ /)[1]
    response.reply t("response.success", :type => type, :query => query, :time => time)
  rescue MultiJson::ParseError
    response.reply t("response.json_failure", :location => location)
  rescue
    response.reply t("response.failure", :location => location)
  end
end