-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cf: allow for trailing slash on CF modpack page URL (#430)
- Loading branch information
Showing
4 changed files
with
105 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/me/itzg/helpers/curseforge/ModpacksPageUrlParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package me.itzg.helpers.curseforge; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import me.itzg.helpers.errors.InvalidParameterException; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
class ModpacksPageUrlParser { | ||
private static final Pattern PAGE_URL_PATTERN = Pattern.compile( | ||
"https://(www|beta)\\.curseforge\\.com/minecraft/modpacks/(?<slug>[^/]+?)(/((files|download)(/(?<fileId>\\d+)?)?)?)?"); | ||
|
||
@Data @Builder | ||
public static class Parsed { | ||
String slug; | ||
Integer fileId; | ||
} | ||
|
||
@NotNull | ||
public static Parsed parse(String pageUrl) { | ||
if (pageUrl == null) { | ||
return Parsed.builder().build(); | ||
} | ||
|
||
final Matcher m = PAGE_URL_PATTERN.matcher(pageUrl); | ||
if (m.matches()) { | ||
final String slug = m.group("slug"); | ||
final String fileIdStr = m.group("fileId"); | ||
if (fileIdStr != null) { | ||
return Parsed.builder() | ||
.slug(slug) | ||
.fileId(Integer.parseInt(fileIdStr)) | ||
.build(); | ||
} | ||
else { | ||
return Parsed.builder() | ||
.slug(slug) | ||
.build(); | ||
} | ||
} | ||
else { | ||
throw new InvalidParameterException("Unexpected CF page URL structure: " + pageUrl); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/me/itzg/helpers/curseforge/ModpacksPageUrlParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package me.itzg.helpers.curseforge; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import me.itzg.helpers.curseforge.ModpacksPageUrlParser.Parsed; | ||
import me.itzg.helpers.errors.InvalidParameterException; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class ModpacksPageUrlParserTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"https://www.curseforge.com/minecraft/modpacks/create-perfect-world", | ||
"https://beta.curseforge.com/minecraft/modpacks/create-perfect-world", | ||
"https://www.curseforge.com/minecraft/modpacks/create-perfect-world/", | ||
"https://www.curseforge.com/minecraft/modpacks/create-perfect-world/files", | ||
"https://www.curseforge.com/minecraft/modpacks/create-perfect-world/files/", | ||
}) | ||
void justSlug(String url) { | ||
final Parsed parsed = ModpacksPageUrlParser.parse(url); | ||
assertThat(parsed).isNotNull(); | ||
assertThat(parsed.getSlug()).isEqualTo("create-perfect-world"); | ||
assertThat(parsed.getFileId()).isNull(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"https://www.curseforge.com/minecraft/modpacks/create-perfect-world/files/5181367" | ||
}) | ||
void slugAndFileId(String url) { | ||
final Parsed parsed = ModpacksPageUrlParser.parse(url); | ||
assertThat(parsed).isNotNull(); | ||
assertThat(parsed.getSlug()).isEqualTo("create-perfect-world"); | ||
assertThat(parsed.getFileId()).isEqualTo(5181367); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"", | ||
"https://www.google.com", | ||
"https://www.curseforge.com/minecraft/modpacks/create-perfect-world/wrong/", | ||
"https://www.curseforge.com/minecraft/modpacks/create-perfect-world/invalid/5181367" | ||
}) | ||
void invalid(String url) { | ||
assertThatThrownBy(() -> ModpacksPageUrlParser.parse(url)) | ||
.isInstanceOf(InvalidParameterException.class); | ||
} | ||
} |