|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -module Matches |
4 | | - # Fetches match IDs from Riot API for a player and enqueues SyncMatchJob |
5 | | - # for each new match. Returns counts synchronously so the caller can |
6 | | - # respond with meaningful feedback without waiting for individual syncs. |
7 | | - class ImportMatchesService |
8 | | - def initialize(player:, organization:, count: 20, force_update: false) |
9 | | - @player = player |
10 | | - @organization = organization |
11 | | - @count = count |
12 | | - @force_update = force_update |
13 | | - end |
| 3 | +# Fetches match IDs from Riot API for a player and enqueues SyncMatchJob |
| 4 | +# for each new match. Returns counts synchronously so the caller can |
| 5 | +# respond with meaningful feedback without waiting for individual syncs. |
| 6 | +class ImportMatchesService |
| 7 | + def initialize(player:, organization:, count: 20, force_update: false) |
| 8 | + @player = player |
| 9 | + @organization = organization |
| 10 | + @count = count |
| 11 | + @force_update = force_update |
| 12 | + end |
14 | 13 |
|
15 | | - # @return [Hash] counts: total_matches_found, imported, already_imported, updated |
16 | | - def call |
17 | | - match_ids = fetch_match_ids |
18 | | - tally = { imported: 0, already_imported: 0, updated: 0 } |
| 14 | + # @return [Hash] counts: total_matches_found, imported, already_imported, updated |
| 15 | + def call |
| 16 | + match_ids = fetch_match_ids |
| 17 | + tally = { imported: 0, already_imported: 0, updated: 0 } |
19 | 18 |
|
20 | | - match_ids.each { |id| process_match(id, tally) } |
| 19 | + match_ids.each { |id| process_match(id, tally) } |
21 | 20 |
|
22 | | - tally.merge(total_matches_found: match_ids.size) |
23 | | - end |
| 21 | + tally.merge(total_matches_found: match_ids.size) |
| 22 | + end |
24 | 23 |
|
25 | | - private |
| 24 | + private |
26 | 25 |
|
27 | | - def fetch_match_ids |
28 | | - RiotApiService.new.get_match_history( |
29 | | - puuid: @player.riot_puuid, |
30 | | - region: region, |
31 | | - count: @count |
32 | | - ) |
33 | | - end |
| 26 | + def fetch_match_ids |
| 27 | + RiotApiService.new.get_match_history( |
| 28 | + puuid: @player.riot_puuid, |
| 29 | + region: region, |
| 30 | + count: @count |
| 31 | + ) |
| 32 | + end |
34 | 33 |
|
35 | | - def process_match(match_id, tally) |
36 | | - if Match.exists?(riot_match_id: match_id) |
37 | | - handle_existing_match(match_id, tally) |
38 | | - else |
39 | | - SyncMatchJob.perform_later(match_id, @organization.id, region) |
40 | | - tally[:imported] += 1 |
41 | | - end |
| 34 | + def process_match(match_id, tally) |
| 35 | + if Match.exists?(riot_match_id: match_id) |
| 36 | + handle_existing_match(match_id, tally) |
| 37 | + else |
| 38 | + SyncMatchJob.perform_later(match_id, @organization.id, region) |
| 39 | + tally[:imported] += 1 |
42 | 40 | end |
| 41 | + end |
43 | 42 |
|
44 | | - def handle_existing_match(match_id, tally) |
45 | | - if @force_update |
46 | | - SyncMatchJob.perform_later(match_id, @organization.id, region, force_update: true) |
47 | | - tally[:updated] += 1 |
48 | | - else |
49 | | - tally[:already_imported] += 1 |
50 | | - end |
| 43 | + def handle_existing_match(match_id, tally) |
| 44 | + if @force_update |
| 45 | + SyncMatchJob.perform_later(match_id, @organization.id, region, force_update: true) |
| 46 | + tally[:updated] += 1 |
| 47 | + else |
| 48 | + tally[:already_imported] += 1 |
51 | 49 | end |
| 50 | + end |
52 | 51 |
|
53 | | - def region |
54 | | - @player.region || 'BR' |
55 | | - end |
| 52 | + def region |
| 53 | + @player.region || 'BR' |
56 | 54 | end |
57 | 55 | end |
0 commit comments