ehbrs_ruby_utils 0.29.0 → 0.31.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress.rb +38 -0
  3. data/lib/ehbrs_ruby_utils/bga/parsers/table/active_players.rb +25 -0
  4. data/lib/ehbrs_ruby_utils/bga/parsers/table/{players.rb → ended_players.rb} +1 -1
  5. data/lib/ehbrs_ruby_utils/bga/parsers/table.rb +27 -6
  6. data/lib/ehbrs_ruby_utils/bga/session/player_tables_on_game_in_progress.rb +39 -0
  7. data/lib/ehbrs_ruby_utils/bga/session/table.rb +7 -6
  8. data/lib/ehbrs_ruby_utils/bga/table/player.rb +2 -2
  9. data/lib/ehbrs_ruby_utils/bga/urls.rb +6 -0
  10. data/lib/ehbrs_ruby_utils/version.rb +1 -1
  11. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec.rb +7 -0
  12. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_84920871_2023-06-12.source.html +8657 -0
  13. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_84920871_2023-06-12.target.yaml +41 -0
  14. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_84920871_2023-06-19.source.html +8650 -0
  15. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_84920871_2023-06-19.target.yaml +41 -0
  16. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93167144_2023-06-12.source.html +8657 -0
  17. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93167144_2023-06-12.target.yaml +5 -0
  18. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93205235_2023-06-12.source.html +8657 -0
  19. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93205235_2023-06-12.target.yaml +13 -0
  20. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93212034_2023-06-12.source.html +8657 -0
  21. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93212034_2023-06-12.target.yaml +29 -0
  22. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93223573_2023-06-12.source.html +8657 -0
  23. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93223573_2023-06-12.target.yaml +7 -0
  24. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93248308_2023-06-12.source.html +8657 -0
  25. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93248308_2023-06-12.target.yaml +1 -0
  26. data/spec/lib/ehbrs_ruby_utils/bga/parsers/table_spec_files/table_383405743.source.html +1480 -0
  27. data/spec/lib/ehbrs_ruby_utils/bga/parsers/table_spec_files/table_383405743.target.yaml +32 -0
  28. metadata +75 -38
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d3d030552c8f65af3c5c12d12466e7c0b79b17aaa1d15e2b6b34401178a4db6
4
- data.tar.gz: '05393346d1434c2ddfe1fbfdd0287cdbc324fbded63eaf69db3f910d27ae6397'
3
+ metadata.gz: 95babc114df8af9b04999592be5af6aae7865371f774c7d8b1c9e44a83900cfa
4
+ data.tar.gz: ec03e189177951420193f7ff68233db20675257f889cecc5b215fe6f10e0c43a
5
5
  SHA512:
6
- metadata.gz: 71849968b06015398fddacfa93e9751c2088426c1a09cb9f83ec67e49e657b11488e3d9b776e8326547ff19f5b18285070ad63817fe2ea382016b2c5ad75bba0
7
- data.tar.gz: e70b99ec32cffb0ab62f43fe7695bb8dc58795bcdaa385a66494025f5dcdf4445a8f86808fa43e9677526fd30d24291a90acdde806898563e55240c5403e69af
6
+ metadata.gz: bb85e43c2ff1aad41a7f9d838a44eea32d65799536bbe070ce490a95db5a9bb97e3c5b30884dce66bd6993ff148fcd47675c60d2b84f409dd4b9e61374c23bcf
7
+ data.tar.gz: 657ba445b150cb086e98e46f7b58ae0d6c11ca965ff3cf4f185d90e97e2568e67b98d0bd3249624bcf55a029da646c4600e0383ad13aaae58e785770fdd165c7
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aranha/parsers/html/item_list'
4
+ require 'eac_ruby_utils/core_ext'
5
+
6
+ module EhbrsRubyUtils
7
+ module Bga
8
+ module Parsers
9
+ class GameInProgress < ::Aranha::Parsers::Html::ItemList
10
+ ITEMS_XPATH = '//*[@id = "gametables_inprogress_all"]' \
11
+ '//*[starts-with(@id, "gametableblock_")]'
12
+ STATUS_CLASS_PATTERN = /\Agametable_status_(.+)\z/.freeze
13
+ STATUS_CLASS_PARSER = STATUS_CLASS_PATTERN.to_parser { |m| m[1] }
14
+
15
+ field :id, :integer, './@id'
16
+ field :status, :string, './@class'
17
+
18
+ def item_data(idd)
19
+ %i[status].each do |key|
20
+ idd[key] = send("process_#{key}", idd.fetch(key))
21
+ end
22
+ idd
23
+ end
24
+
25
+ def items_xpath
26
+ ITEMS_XPATH
27
+ end
28
+
29
+ private
30
+
31
+ def process_status(status)
32
+ status.split(' ').lazy.map { |s| STATUS_CLASS_PARSER.parse(s.strip) }.find(&:present?) ||
33
+ raise("No status class found in \"#{status}\"")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aranha/parsers/html/item'
4
+ require 'aranha/parsers/html/item_list'
5
+ require 'dentaku'
6
+ require 'eac_ruby_utils/core_ext'
7
+
8
+ module EhbrsRubyUtils
9
+ module Bga
10
+ module Parsers
11
+ class Table < ::Aranha::Parsers::Html::Item
12
+ class ActivePlayers < ::Aranha::Parsers::Html::ItemList
13
+ ITEMS_XPATH = '//div[starts-with(@id, "active_player_")]'
14
+
15
+ field :id, :integer, './@id'
16
+ field :name, :string, './/*[contains(@class, "active_player_fullname")]'
17
+
18
+ def items_xpath
19
+ ITEMS_XPATH
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -9,7 +9,7 @@ module EhbrsRubyUtils
9
9
  module Bga
10
10
  module Parsers
11
11
  class Table < ::Aranha::Parsers::Html::Item
12
- class Players < ::Aranha::Parsers::Html::ItemList
12
+ class EndedPlayers < ::Aranha::Parsers::Html::ItemList
13
13
  ITEMS_XPATH = '//div[starts-with(@id, "score_entry_")]'
14
14
  RANK_VALUES = { vencedor: 1, perdedor: 2 }.freeze
15
15
 
@@ -7,15 +7,23 @@ module EhbrsRubyUtils
7
7
  module Bga
8
8
  module Parsers
9
9
  class Table < ::Aranha::Parsers::Html::Item
10
+ require_sub __FILE__
11
+
10
12
  GAME_IMAGE_URL_PARSER = %r{/gamemedia/([^/]+)/box/}.to_parser { |m| m[1] }
11
13
  ITEM_XPATH = '/'
14
+ PLAYERS_IDS = {
15
+ 'game_result' => ::EhbrsRubyUtils::Bga::Parsers::Table::EndedPlayers,
16
+ 'players_at_table' => ::EhbrsRubyUtils::Bga::Parsers::Table::ActivePlayers
17
+ }.freeze
18
+ PLAYERS_IDS_XPATH = PLAYERS_IDS.keys.map { |id| "@id = \"#{id}\"" }.join(' or ')
19
+ PLAYERS_XPATH = ".//*[(#{PLAYERS_IDS_XPATH}) and count(./*) > 0]"
12
20
 
13
21
  field :game_code, :string, '//meta[@property="og:image"]/@content'
14
22
  field :game_name, :string, './/*[@id = "table_name"]/text()'
15
23
  field :creation_time, :string, './/*[@id = "creationtime"]/text()'
16
24
  field :estimated_duration, :string, './/*[@id = "estimated_duration"]/text()'
17
25
  field :options, :node, './/*[@id = "gameoptions"]'
18
- field :players, :node, './/*[@id = "game_result"]'
26
+ field :players, :node, PLAYERS_XPATH
19
27
  field :game_conceded, :node, './/*[@id = "game_conceded" and @style="display: block;"]'
20
28
 
21
29
  def item_xpath
@@ -24,10 +32,7 @@ module EhbrsRubyUtils
24
32
 
25
33
  def data
26
34
  r = super
27
- %i[options players].each do |key|
28
- r[key] = self.class.const_get(key.to_s.camelize).from_node(r.fetch(key)).data
29
- end
30
- %i[creation_time game_code game_conceded].each do |key|
35
+ %i[creation_time game_code game_conceded options players].each do |key|
31
36
  r[key] = send("process_#{key}", r.fetch(key))
32
37
  end
33
38
  r
@@ -51,7 +56,23 @@ module EhbrsRubyUtils
51
56
  node.present?
52
57
  end
53
58
 
54
- require_sub __FILE__
59
+ # @param node [Nokogiri::XML::Element]
60
+ # @return [Hash]
61
+ def process_options(node)
62
+ ::EhbrsRubyUtils::Bga::Parsers::Table::Options.from_node(node).data
63
+ end
64
+
65
+ # @param node [Nokogiri::XML::Element]
66
+ # @return [Hash]
67
+ def process_players(node)
68
+ process_players_parser(node).from_node(node).data
69
+ end
70
+
71
+ # @param node [Nokogiri::XML::Element]
72
+ # @return [Class]
73
+ def process_players_parser(node)
74
+ PLAYERS_IDS.fetch(node.attribute('id').value)
75
+ end
55
76
  end
56
77
  end
57
78
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ehbrs_ruby_utils/bga/parsers/game_in_progress'
4
+ require 'ehbrs_ruby_utils/bga/urls'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module EhbrsRubyUtils
8
+ module Bga
9
+ class Session < ::SimpleDelegator
10
+ class PlayerTablesOnGameInProgress
11
+ include ::EhbrsRubyUtils::Bga::Urls
12
+ enable_method_class
13
+ enable_simple_cache
14
+ common_constructor :session, :player_id
15
+
16
+ def result
17
+ navigate_to_page
18
+ result_data
19
+ end
20
+
21
+ # @return [Addressable::URI]
22
+ def url
23
+ player_game_in_progress_url(player_id)
24
+ end
25
+
26
+ private
27
+
28
+ # @return [Array<Hash>]
29
+ def result_data
30
+ ::EhbrsRubyUtils::Bga::Parsers::GameInProgress.from_content(session.current_source).data
31
+ end
32
+
33
+ def navigate_to_page
34
+ session.navigate.to url
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -14,8 +14,6 @@ module EhbrsRubyUtils
14
14
  enable_simple_cache
15
15
  common_constructor :session, :table_id
16
16
 
17
- RESULTS_XPATH = '//*[@id = "game_result"]'
18
-
19
17
  def result
20
18
  ::EhbrsRubyUtils::Bga::Table.new(
21
19
  fetch_data
@@ -31,10 +29,13 @@ module EhbrsRubyUtils
31
29
 
32
30
  def fetch_data
33
31
  session.navigate.to url
34
- session.wait_for_element(xpath: RESULTS_XPATH)
35
- { id: table_id }.merge(
36
- ::EhbrsRubyUtils::Bga::Parsers::Table.from_content(session.current_source).data
37
- )
32
+ session.wait_for_element(xpath: parser_class.const_get('PLAYERS_XPATH'))
33
+ { id: table_id }.merge(parser_class.from_content(session.current_source).data)
34
+ end
35
+
36
+ # @return [Class]
37
+ def parser_class
38
+ ::EhbrsRubyUtils::Bga::Parsers::Table
38
39
  end
39
40
  end
40
41
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ehbrs_ruby_utils/bga/parsers/table/players'
3
+ require 'ehbrs_ruby_utils/bga/parsers/table/ended_players'
4
4
  require 'eac_ruby_utils/core_ext'
5
5
 
6
6
  module EhbrsRubyUtils
@@ -9,7 +9,7 @@ module EhbrsRubyUtils
9
9
  class Player
10
10
  common_constructor :table, :data
11
11
 
12
- ::EhbrsRubyUtils::Bga::Parsers::Table::Players.fields.each do |field|
12
+ ::EhbrsRubyUtils::Bga::Parsers::Table::EndedPlayers.fields.each do |field|
13
13
  define_method field.name do
14
14
  data.fetch(field.name)
15
15
  end
@@ -17,6 +17,12 @@ module EhbrsRubyUtils
17
17
  root_url + suffix
18
18
  end
19
19
 
20
+ # @para player_id [Integer]
21
+ # @return [Addressable::URI]
22
+ def player_game_in_progress_url(player_id)
23
+ build_url("/gameinprogress?player=#{player_id}&all")
24
+ end
25
+
20
26
  # @para player_id [Integer]
21
27
  # @return [Addressable::URI]
22
28
  def player_game_stats_url(player_id)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EhbrsRubyUtils
4
- VERSION = '0.29.0'
4
+ VERSION = '0.31.0'
5
5
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ehbrs_ruby_utils/bga/parsers/game_in_progress'
4
+
5
+ ::RSpec.describe ::EhbrsRubyUtils::Bga::Parsers::GameInProgress do
6
+ include_examples 'source_target_fixtures', __FILE__
7
+ end