lita-howlongtobeat 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb31af125c192ec7dcd13232b24d6164cd33a20a
4
- data.tar.gz: c91f5b605c190ca566e6c4cf2a311d63f1bfc775
3
+ metadata.gz: 91b6e19dc629d36b665cccd5803e18c774d03ad1
4
+ data.tar.gz: 43348a2c88c171e3b41a0f707235465cfc538020
5
5
  SHA512:
6
- metadata.gz: c2aea2755662ae358f3b8d4e6e2d288fa5f56c3d23f7acdd261df6fc75c0e0d23794af8b79e80ea888d2737a5b0cb37d16c16b58e9a4541efabd05017a895959
7
- data.tar.gz: 21c861b430922e7fa3d0804b0b7aa667c733956bb61b17e19f27f22061c798a98dafa9c533e11879fa57d3a93ae64fa3b76f1615850a51cee4a44eae5452d7e4
6
+ metadata.gz: bb6d0666bfadc04662055f9371775400fdb849257ad344c5c5b5ae1e767c1dbb8a17f22674a16d6ba6414ac23d116aa194c50e70250ea2c7881cc037baf6cc43
7
+ data.tar.gz: c9cc7264a66dc65561aa47a7444cd577d55fed7e3878f68a651e48add33b8c824962cd62d576eed235f1379091efd0048607a12aa03ed4671a0e72dc42247a94
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # lita-howlongtobeat
2
2
 
3
+ [![Circle CI](https://circleci.com/gh/mskog/lita-howlongtobeat.svg?style=svg)](https://circleci.com/gh/mskog/lita-howlongtobeat)
4
+
3
5
  **lita-howlongtobeat** is a [Lita](https://github.com/litaio/lita) handler that returns the average length of the main story for a video game.
4
6
 
5
7
  ## Installation
@@ -1,20 +1,52 @@
1
1
  module Lita
2
2
  module Handlers
3
3
  class Howlongtobeat < Handler
4
- SITE_URL = "http://howlongtobeat.com"
5
- SEARCH_URL = "http://howlongtobeat.com/search_main.php?page=1"
6
4
 
7
5
  route(/^(?:hltp|howlongtobeat)\s+(.*)/i, :howlongtobeat, command: true, help: { t("help.howlongtobeat_key") => t("help.howlongtobeat_value")})
8
6
 
9
7
  def howlongtobeat(response)
10
- term = response.matches[0][0]
11
- search = http.post(SEARCH_URL, queryString: term, t: 'games', sorthead: 'popular', sortd: 'Normal Order', detail: '0')
12
- document = Nokogiri::HTML(search.body)
13
- title = document.css('a')[0]
14
- response.reply("No results found for #{term}") && return unless title
15
- story = document.css(".search_list_tidbit.center")[0].text
16
- response.reply("It will take about #{story}to beat the main story of #{title.attr('title')}")
17
- response.reply("Source: #{SITE_URL}/#{title.attr('href')}")
8
+ HowlongtobeatSearch.new(response.matches[0][0], http).print_result(response)
9
+ end
10
+
11
+ class HowlongtobeatSearch
12
+ SEARCH_URL = "http://howlongtobeat.com/search_main.php?page=1"
13
+ SITE_URL = "http://howlongtobeat.com"
14
+
15
+ def initialize(term, client = Faraday)
16
+ @term = term
17
+ @client = client
18
+ end
19
+
20
+ def print_result(response)
21
+ response.reply("No results found for #{@term}") && return unless title
22
+ response.reply("No data found for how long it will take to complete the main story of '#{title_content}'") && return unless story
23
+ response.reply("It will take about #{story}to beat the main story of '#{title_content}'")
24
+ response.reply("Source: #{SITE_URL}/#{title_href}")
25
+ end
26
+
27
+ def story
28
+ text = doc.css(".search_list_tidbit.center")[0].text
29
+ text == '--' ? nil : text
30
+ end
31
+
32
+ def title
33
+ doc.css('a')[0]
34
+ end
35
+
36
+ def title_href
37
+ title ? title.attr('href') : nil
38
+ end
39
+
40
+ def title_content
41
+ title ? title.attr('title') : nil
42
+ end
43
+
44
+ def doc
45
+ @data ||= begin
46
+ search = @client.post(SEARCH_URL, queryString: @term, t: 'games', sorthead: 'popular', sortd: 'Normal Order', detail: '0')
47
+ Nokogiri::HTML(search.body)
48
+ end
49
+ end
18
50
  end
19
51
 
20
52
  Lita.register_handler(self)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "lita-howlongtobeat"
3
- spec.version = "0.1.0"
3
+ spec.version = "0.2.0"
4
4
  spec.authors = ["Magnus Skog"]
5
5
  spec.email = ["magnus.m.skog@gmail.com"]
6
6
  spec.description = "A Lita handler that returns the average number of hours required to beat a game from http://howlongtobeat.com"
@@ -0,0 +1,37 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Tue, 17 Nov 2015 17:13:46 GMT
3
+ Content-Type: text/html; charset=windows-1252
4
+ Transfer-Encoding: chunked
5
+ Connection: keep-alive
6
+ Expires: Thu, 19 Nov 1981 08:52:00 GMT
7
+ Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
8
+ Pragma: no-cache
9
+ Set-Cookie: hl2b_save=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=howlongtobeat.com; httponly
10
+ Vary: Accept-Encoding
11
+ Set-Cookie: _asomcnc=1; Max-Age=900; Path=/
12
+ X-NoCache: 1
13
+ Server: cloudflare-nginx
14
+ CF-RAY: 246d116e7df23ce3-CPH
15
+ Content-Encoding: gzip
16
+
17
+ <h3 class='head_padding shadow_box back_blue'>We Found 1 Games for "legacy of the void"</h3> <li class="back_white shadow_box"> <div class="search_list_image">
18
+ <a title="StarCraft II: Legacy of the Void" href="game.php?id=9154">
19
+ <img src="gameimages/StarCraft_II_Legacy_of_the_Void_logo.png"/>
20
+ </a>
21
+ </div> <div class="search_list_details"> <h3><a class="text_blue" title="StarCraft II: Legacy of the Void" href="game.php?id=9154">StarCraft II: Legacy of the Void</a></h3> <div class="search_list_details_block"> <div>
22
+ <div class="search_list_tidbit">Main Story</div>
23
+ <div class="search_list_tidbit center time_00">--</div>
24
+ </div>
25
+ <div>
26
+ <div class="search_list_tidbit">Main + Extra</div>
27
+ <div class="search_list_tidbit center time_40">15&#189; Hours </div>
28
+ </div>
29
+ <div>
30
+ <div class="search_list_tidbit">Completionist</div>
31
+ <div class="search_list_tidbit center time_00">--</div>
32
+ </div>
33
+ <div>
34
+ <div class="search_list_tidbit">Combined</div>
35
+ <div class="search_list_tidbit center time_40">15&#189; Hours </div>
36
+ </div> </div>
37
+ </div> <div class="clear"></div> </li>
@@ -6,19 +6,31 @@ describe Lita::Handlers::Howlongtobeat, lita_handler: true do
6
6
 
7
7
  describe "#howlongtobeat" do
8
8
  before :each do
9
- stub_request(:post, described_class::SEARCH_URL).with(body: {"detail"=>"0", "queryString"=>"sdfdfsfdfds", "sortd"=>"Normal Order", "sorthead"=>"popular", "t"=>"games"}).to_return(File.read('spec/fixtures/nothing_found.txt'))
9
+ stub_request(:post, described_class::HowlongtobeatSearch::SEARCH_URL).with(body: {"detail"=>"0", "queryString"=>"sdfdfsfdfds", "sortd"=>"Normal Order", "sorthead"=>"popular", "t"=>"games"}).to_return(File.read('spec/fixtures/nothing_found.txt'))
10
10
  end
11
11
 
12
12
  context "with a term with results" do
13
13
  before :each do
14
- stub_request(:post, described_class::SEARCH_URL).with(body: {"detail"=>"0", "queryString"=>"witcher 3", "sortd"=>"Normal Order", "sorthead"=>"popular", "t"=>"games"}).to_return(File.read('spec/fixtures/witcher_3.txt'))
14
+ stub_request(:post, described_class::HowlongtobeatSearch::SEARCH_URL).with(body: {"detail"=>"0", "queryString"=>"witcher 3", "sortd"=>"Normal Order", "sorthead"=>"popular", "t"=>"games"}).to_return(File.read('spec/fixtures/witcher_3.txt'))
15
15
  end
16
16
 
17
17
  it "returns the title and the main story length" do
18
18
  send_command "howlongtobeat witcher 3"
19
19
  expect(replies.count).to eq 2
20
- expect(replies[0]).to match 'It will take about 43½ Hours to beat the main story of The Witcher 3: Wild Hunt'
21
- expect(replies[1]).to match "Source: #{described_class::SITE_URL}/game.php?id=10270"
20
+ expect(replies[0]).to match "It will take about 43½ Hours to beat the main story of 'The Witcher 3: Wild Hunt'"
21
+ expect(replies[1]).to match "Source: #{described_class::HowlongtobeatSearch::SITE_URL}/game.php?id=10270"
22
+ end
23
+ end
24
+
25
+ context "with a term with no main story results" do
26
+ before :each do
27
+ stub_request(:post, described_class::HowlongtobeatSearch::SEARCH_URL).with(body: {"detail"=>"0", "queryString"=>"legacy of the void", "sortd"=>"Normal Order", "sorthead"=>"popular", "t"=>"games"}).to_return(File.read('spec/fixtures/no_main.txt'))
28
+ end
29
+
30
+ it "returns a helpful note" do
31
+ send_command "howlongtobeat legacy of the void"
32
+ expect(replies.count).to eq 1
33
+ expect(replies[0]).to match "No data found for how long it will take to complete the main story of 'StarCraft II: Legacy of the Void'"
22
34
  end
23
35
  end
24
36
 
@@ -26,7 +38,7 @@ describe Lita::Handlers::Howlongtobeat, lita_handler: true do
26
38
  let(:command){'sdfdfsfdfds'}
27
39
 
28
40
  before :each do
29
- stub_request(:post, described_class::SEARCH_URL).with(body: {"detail"=>"0", "queryString"=> command, "sortd"=>"Normal Order", "sorthead"=>"popular", "t"=>"games"}).to_return(File.read('spec/fixtures/nothing_found.txt'))
41
+ stub_request(:post, described_class::HowlongtobeatSearch::SEARCH_URL).with(body: {"detail"=>"0", "queryString"=> command, "sortd"=>"Normal Order", "sorthead"=>"popular", "t"=>"games"}).to_return(File.read('spec/fixtures/nothing_found.txt'))
30
42
  end
31
43
 
32
44
  it "returns an error" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-howlongtobeat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus Skog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-16 00:00:00.000000000 Z
11
+ date: 2015-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lita
@@ -141,6 +141,7 @@ files:
141
141
  - lib/lita/handlers/howlongtobeat.rb
142
142
  - lita-howlongtobeat.gemspec
143
143
  - locales/en.yml
144
+ - spec/fixtures/no_main.txt
144
145
  - spec/fixtures/nothing_found.txt
145
146
  - spec/fixtures/witcher_3.txt
146
147
  - spec/lita/handlers/howlongtobeat_spec.rb
@@ -173,6 +174,7 @@ specification_version: 4
173
174
  summary: A Lita handler that returns the average number of hours required to beat
174
175
  a game from http://howlongtobeat.com
175
176
  test_files:
177
+ - spec/fixtures/no_main.txt
176
178
  - spec/fixtures/nothing_found.txt
177
179
  - spec/fixtures/witcher_3.txt
178
180
  - spec/lita/handlers/howlongtobeat_spec.rb