Class: Lita::Handlers::Toutiao

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

Instance Method Summary collapse

Instance Method Details

#access_apiObject



21
22
23
24
25
26
27
# File 'lib/lita/handlers/toutiao.rb', line 21

def access_api
  url = 'http://toutiao.io/'
  res = http.get url
  content = parse_data(res.body)
  redis.set(key_of_today, content) 
  content
end

#fetch_toutiao(response) ⇒ Object



11
12
13
14
15
# File 'lib/lita/handlers/toutiao.rb', line 11

def fetch_toutiao response
  posts = redis.get(key_of_today)
  posts = access_api unless posts
  response.reply posts
end

#key_of_todayObject



17
18
19
# File 'lib/lita/handlers/toutiao.rb', line 17

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

#parse_data(html) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/lita/handlers/toutiao.rb', line 29

def parse_data(html)
  posts = []
  doc = ::Nokogiri::HTML(html)
  doc.css('.daily .title > a').each_with_index do |post, index|
    posts << "#{index + 1}.#{post.text.strip}: (#{post[:href]})"
  end
  posts.join("\n")
end