Class: Lita::Handlers::ZhihuDaily
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::ZhihuDaily
- Defined in:
- lib/lita/handlers/zhihu_daily.rb
Instance Method Summary collapse
- #access_api ⇒ Object
- #fetch_zhihu(response) ⇒ Object
- #key_of_today ⇒ Object
- #parse_data(html) ⇒ Object
Instance Method Details
#access_api ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/lita/handlers/zhihu_daily.rb', line 20 def access_api url = 'http://www.zhihu.com/explore' res = http.get url content = parse_data(res.body) redis.set(key_of_today, content) content end |
#fetch_zhihu(response) ⇒ Object
10 11 12 13 14 |
# File 'lib/lita/handlers/zhihu_daily.rb', line 10 def fetch_zhihu response posts = redis.get(key_of_today) posts = access_api unless posts response.reply posts end |
#key_of_today ⇒ Object
16 17 18 |
# File 'lib/lita/handlers/zhihu_daily.rb', line 16 def key_of_today 'zhihu_' + Time.now.strftime('%Y-%m-%d') end |
#parse_data(html) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/lita/handlers/zhihu_daily.rb', line 28 def parse_data(html) posts = [] doc = ::Nokogiri::HTML(html) doc.css('#js-explore-tab .question_link').each do |post| posts << "*#{post.text.strip}*: (http://www.zhihu.com#{post[:href]})" end posts.join("\n") end |