Class: Lita::Handlers::V2ex

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

Instance Method Summary collapse

Instance Method Details

#access_apiObject



18
19
20
21
22
23
24
25
# File 'lib/lita/handlers/v2ex.rb', line 18

def access_api
  url = 'https://www.v2ex.com/api/topics/hot.json'
  res = http.get url
  hash_res = MultiJson.load(res.body)
  content = filter_data(hash_res).join("\n")
  redis.set(key_of_today, content)
  content
end

#fetch_hot_topic(response) ⇒ Object



8
9
10
11
12
# File 'lib/lita/handlers/v2ex.rb', line 8

def fetch_hot_topic response
  hots = redis.get(key_of_today)
  hots = access_api unless hots
  response.reply hots
end

#filter_data(res) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/lita/handlers/v2ex.rb', line 27

def filter_data(res)
  if res
    res.map {|item| "#{item['title']}: #{item['url']}" }
  else
    []
  end
end

#key_of_todayObject



14
15
16
# File 'lib/lita/handlers/v2ex.rb', line 14

def key_of_today
  'v2ex_' + Time.now.strftime('%Y-%m-%d')
end