Class: Lita::Handlers::ZhihuDaily

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

Instance Method Summary collapse

Instance Method Details

#access_apiObject



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_todayObject



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