ehbrs_ruby_utils 0.28.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress.rb +21 -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/gjt1/manager.rb +10 -5
  11. data/lib/ehbrs_ruby_utils/mudslide/message.rb +44 -0
  12. data/lib/ehbrs_ruby_utils/mudslide.rb +9 -0
  13. data/lib/ehbrs_ruby_utils/version.rb +1 -1
  14. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec.rb +7 -0
  15. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_84920871_2023-06-12.source.html +8657 -0
  16. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_84920871_2023-06-12.target.yaml +21 -0
  17. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93167144_2023-06-12.source.html +8657 -0
  18. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93167144_2023-06-12.target.yaml +3 -0
  19. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93205235_2023-06-12.source.html +8657 -0
  20. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93205235_2023-06-12.target.yaml +7 -0
  21. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93212034_2023-06-12.source.html +8657 -0
  22. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93212034_2023-06-12.target.yaml +15 -0
  23. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93223573_2023-06-12.source.html +8657 -0
  24. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93223573_2023-06-12.target.yaml +4 -0
  25. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93248308_2023-06-12.source.html +8657 -0
  26. data/spec/lib/ehbrs_ruby_utils/bga/parsers/game_in_progress_spec_files/player_93248308_2023-06-12.target.yaml +1 -0
  27. data/spec/lib/ehbrs_ruby_utils/bga/parsers/table_spec_files/table_383405743.source.html +1480 -0
  28. data/spec/lib/ehbrs_ruby_utils/bga/parsers/table_spec_files/table_383405743.target.yaml +32 -0
  29. metadata +44 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 242dda28e0e3dab83a2b68d030273e0335b54f45a070980bcd7de340fdb53f1e
4
- data.tar.gz: 76442e6655256f61039a009e53b241023e01b2e592bb42227d113ab81105430d
3
+ metadata.gz: 3802f6228037cca931f88ce749cf512aa19c4c2e62a6ce91f35e81a8ea3e5928
4
+ data.tar.gz: 433ae6d2b1dc7133920a5a6c10dddf6ae2170aff8a1f5d5e03dd88dd978de1fd
5
5
  SHA512:
6
- metadata.gz: 3dc4de45f3322ab74030397d7440c3d6fe226d6f7d15ac93fec21846f6281975855344432c60a909ae8a01e222afb206dffc0ad91ee63f2a683b1749b053e93a
7
- data.tar.gz: 0df77e54c42753a50ff8fab054e18dd5ae27593036cbb6cefca2aea45d1e21fe012a34163f2660b4c120618360525d0f46174c63d397faa556eb97e0eab4a9ed
6
+ metadata.gz: 6c084798e36730906270e5ce96e9086527a75be3e5474af382ae03a67d2784aca2eb8ef110048f02fc5501f9113474eacbe3fd19f49491fbaf44f90393361fea
7
+ data.tar.gz: 6fc1efe60241280dfe67da246075424285262d9ad1c24b3c467ac50aafd7b448388c5687e28b344e5061085ab55e0255ea885810bbfc0af4a7ca2d1aefc15abc
@@ -0,0 +1,21 @@
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
+
13
+ field :id, :integer, './@id'
14
+
15
+ def items_xpath
16
+ ITEMS_XPATH
17
+ end
18
+ end
19
+ end
20
+ end
21
+ 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)
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'ehbrs_ruby_utils/bga/session'
4
4
  require 'ehbrs_ruby_utils/executables'
5
+ require 'ehbrs_ruby_utils/mudslide/message'
5
6
  require 'eac_ruby_utils/core_ext'
6
7
 
7
8
  module EhbrsRubyUtils
@@ -10,6 +11,13 @@ module EhbrsRubyUtils
10
11
  include ::Singleton
11
12
  acts_as_abstract :bga_usernam, :bga_password, :whatsapp_recipient
12
13
 
14
+ # @param table [EhbrsRubyUtils::Bga::Table]
15
+ # @return [void]
16
+ def bga_table_notify(table)
17
+ formatter = ::EhbrsRubyUtils::Bga::TableWhatsappFormatter.new(table)
18
+ whatsapp_send(formatter.to_s, formatter.image_local_path)
19
+ end
20
+
13
21
  def on_bga_logged_session(&block)
14
22
  bga_session = new_bga_session
15
23
  begin
@@ -29,11 +37,8 @@ module EhbrsRubyUtils
29
37
  # @param image_path [Pathname]
30
38
  # @return [void]
31
39
  def whatsapp_send(message, image_path = nil)
32
- if image_path.present?
33
- mudslide_run('send-image', '--caption', message, whatsapp_recipient, image_path)
34
- else
35
- mudslide_run('send', whatsapp_recipient, message)
36
- end
40
+ ::EhbrsRubyUtils::Mudslide::Message.new.text(message).image_path(image_path)
41
+ .recipient(whatsapp_recipient).deliver
37
42
  end
38
43
 
39
44
  private
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'ehbrs_ruby_utils/executables'
5
+
6
+ module EhbrsRubyUtils
7
+ module Mudslide
8
+ class Message
9
+ acts_as_immutable
10
+ immutable_accessor :image_path, :recipient, :text
11
+
12
+ def deliver
13
+ raise 'No recipient set' if recipient.blank?
14
+
15
+ if image_path.present?
16
+ deliver_image
17
+ elsif text.present?
18
+ deliver_text
19
+ else
20
+ deliver
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ # @return [void]
27
+ def deliver_image
28
+ args = ['send-image']
29
+ text.if_present { |_v| args += ['--caption', text] }
30
+ mudslide_run(*args, recipient, image_path)
31
+ end
32
+
33
+ # @return [void]
34
+ def deliver_text
35
+ mudslide_run('send', recipient, text)
36
+ end
37
+
38
+ # @return [void]
39
+ def mudslide_run(*args)
40
+ ::EhbrsRubyUtils::Executables.mudslide.command(*args).system!
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EhbrsRubyUtils
6
+ module Mudslide
7
+ require_sub __FILE__
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EhbrsRubyUtils
4
- VERSION = '0.28.0'
4
+ VERSION = '0.30.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