Class: Lita::Handlers::Howlongtobeat::HowlongtobeatSearch

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

Constant Summary collapse

SEARCH_URL =
"https://howlongtobeat.com/search_main.php?page=1"
SITE_URL =
"https://howlongtobeat.com"

Instance Method Summary collapse

Constructor Details

#initialize(term, client = Faraday) ⇒ HowlongtobeatSearch

Returns a new instance of HowlongtobeatSearch.



15
16
17
18
# File 'lib/lita/handlers/howlongtobeat.rb', line 15

def initialize(term, client = Faraday)
  @term = term
  @client = client
end

Instance Method Details

#docObject



44
45
46
47
48
49
# File 'lib/lita/handlers/howlongtobeat.rb', line 44

def doc
  @data ||= begin
    search = @client.post(SEARCH_URL, queryString: @term, t: 'games', sorthead: 'popular', sortd: 'Normal Order', detail: '0')
    Nokogiri::HTML(search.body)
  end
end


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

def print_result(response)
  response.reply("No results found for #{@term}") && return unless title
  response.reply("No data found for how long it will take to complete the main story of '#{title_content}'") && return unless story
  response.reply("It will take about #{story}to beat the main story of '#{title_content}'")
  response.reply("Source: #{SITE_URL}/#{title_href}")
end

#storyObject



27
28
29
30
# File 'lib/lita/handlers/howlongtobeat.rb', line 27

def story
  text = doc.css(".search_list_tidbit.center")[0].text
  text == '--' ? nil : text
end

#titleObject



32
33
34
# File 'lib/lita/handlers/howlongtobeat.rb', line 32

def title
  doc.css('a')[0]
end

#title_contentObject



40
41
42
# File 'lib/lita/handlers/howlongtobeat.rb', line 40

def title_content
  title ? title.attr('title') : nil
end

#title_hrefObject



36
37
38
# File 'lib/lita/handlers/howlongtobeat.rb', line 36

def title_href
  title ? title.attr('href') : nil
end