Skip to content

Commit c7deb47

Browse files
committed
fix(series import): extract RUB when it's written in Russian.
1 parent a5ba1b6 commit c7deb47

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/main/java/ru/mystamps/web/feature/series/importing/SeriesInfoExtractorServiceImpl.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public class SeriesInfoExtractorServiceImpl implements SeriesInfoExtractorServic
6464
private static final Pattern MICHEL_NUMBERS_REGEXP =
6565
Pattern.compile("#[ ]?(?<begin>[1-9][0-9]{0,3})-(?<end>[1-9][0-9]{0,3})");
6666

67+
// Regular expression that matches Rubles (Russian currency).
68+
private static final Pattern RUB_CURRENCY_REGEXP = Pattern.compile("[0-9][ ]?руб");
69+
6770
// CheckStyle: ignore LineLength for next 4 lines
6871
private static final Pattern VALID_CATEGORY_NAME_EN = Pattern.compile(CategoryValidation.NAME_EN_REGEXP);
6972
private static final Pattern VALID_CATEGORY_NAME_RU = Pattern.compile(CategoryValidation.NAME_RU_REGEXP);
@@ -394,10 +397,18 @@ public SeriesExtractedInfo extract(String pageUrl, RawParsedDataDto data) {
394397
log.debug("Currency is {}", currency);
395398
return currency.toString();
396399

397-
} catch (IllegalArgumentException ex) {
398-
log.debug("Could not extract currency: {}", ex.getMessage());
399-
return null;
400+
} catch (IllegalArgumentException ignored) {
401+
}
402+
403+
Matcher matcher = RUB_CURRENCY_REGEXP.matcher(fragment);
404+
if (matcher.find()) {
405+
log.debug("Currency is RUB");
406+
return Currency.RUB.toString();
400407
}
408+
409+
log.debug("Could not extract currency from a fragment");
410+
411+
return null;
401412
}
402413

403414
private Integer extractSellerByNameAndUrl(String name, String url) {

src/test/groovy/ru/mystamps/web/feature/series/importing/SeriesInfoExtractorServiceImplTest.groovy

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -611,16 +611,17 @@ class SeriesInfoExtractorServiceImplTest extends Specification {
611611
result == null
612612
}
613613

614-
def 'extractCurrency() should return null for unknown currency'() {
615-
given:
616-
String invalidCurrency = 'CAD'
617-
when:
618-
String result = service.extractCurrency(invalidCurrency)
619-
then:
620-
result == null
614+
@Unroll
615+
def 'extractCurrency() should return null for "#fragment"'(String fragment) {
616+
expect:
617+
service.extractCurrency(fragment) == null
618+
where:
619+
fragment | _
620+
'CAD' | _
621+
'труб' | _
621622
}
622623

623-
def 'extractCurrency() should extract currency from a fragment'() {
624+
def 'extractCurrency() should extract exactly specified currency'() {
624625
given:
625626
String validCurrency = Random.currency()
626627
when:
@@ -629,4 +630,17 @@ class SeriesInfoExtractorServiceImplTest extends Specification {
629630
result == validCurrency
630631
}
631632

633+
@Unroll
634+
def 'extractCurrency() should extract RUB currency from "#fragment"'(String fragment) {
635+
expect:
636+
service.extractCurrency(fragment) == 'RUB'
637+
where:
638+
fragment | _
639+
'1 рубль' | _
640+
'10 рублей' | _
641+
'100 руб' | _
642+
'200руб' | _
643+
'660 руб.' | _
644+
}
645+
632646
}

0 commit comments

Comments
 (0)