Class: Ruboty::Handlers::Rurema

Inherits:
Base
  • Object
show all
Defined in:
lib/ruboty/rurema.rb

Constant Summary collapse

URL_BASE =
'https://docs.ruby-lang.org/ja/latest/'
SEARCH_API_URL_BASE =
'https://docs.ruby-lang.org/ja/search/api:v1/'
CLASS_RE =
/[A-Z][\w:]+/

Instance Method Summary collapse

Instance Method Details

#rurema(message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruboty/rurema.rb', line 24

def rurema(message)
  query = message.match_data['query']
  query, params = parse_query(query)
  url = build_url(query)
  if url && exist_resource?(url)
    message.reply url
    return
  end

  # Is CGI.escape correct way?
  resp = get SEARCH_API_URL_BASE + query.split.map {|q| "query:#{CGI.escape(q)}"}.join('/')
  if resp.nil? || resp[:entries].empty?
    message.reply "No document for #{query}"
    return
  end

  resp = params[:n].to_i.times.map do |idx|
    url = resp.dig(:entries, idx, :documents, -1, :url)
    next unless url
    # BUG: rurema searcher returns URL with 'https://docs.ruby-lang.org/ja/search/', so we need removing it.
    url.sub('docs.ruby-lang.org/ja/search/http://', '')
  end.compact
  message.reply resp.join("\n")
end