Skip to content

Commit 3432314

Browse files
refactor: Allow URLs with {format} only (without the percentage).
Closes #16.
1 parent 73b6a4d commit 3432314

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/main/java/ac/simons/oembed/OembedEndpoint.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
*/
5757
public class OembedEndpoint {
5858

59+
private static final Pattern FORMAT_PLACE_HOLDER_PATTERN = Pattern.compile("(?i)%?\\{format}");
60+
5961
/**
6062
* The name of this provider.
6163
*/
@@ -272,8 +274,9 @@ public URI toApiUrl(final String url) {
272274
String uri;
273275
final List<NameValuePair> query = new ArrayList<>();
274276

275-
if (this.getEndpoint().toLowerCase().contains("%{format}")) {
276-
uri = this.getEndpoint().replaceAll(Pattern.quote("%{format}"), this.getFormat().toString());
277+
var matcher = FORMAT_PLACE_HOLDER_PATTERN.matcher(this.getEndpoint());
278+
if (matcher.find()) {
279+
uri = matcher.replaceAll(this.getFormat().toString());
277280
}
278281
else {
279282
uri = this.getEndpoint();

src/test/java/ac/simons/oembed/OembedEndpointTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ public void toApiUrlShouldWork() {
107107
"https://api.twitter.com/1.1/statuses/oembed.json?url=https%3A%2F%2Ftwitter.com%2Frotnroll666%2Fstatus%2F549898095853838336");
108108
}
109109

110+
@Test // GH-16
111+
public void vimeoWithoutPercentage() {
112+
OembedEndpoint oembedEndpoint = new OembedEndpoint();
113+
114+
oembedEndpoint.setEndpoint("https://vimeo.com/api/oembed.{format}");
115+
oembedEndpoint.setFormat(Format.json);
116+
oembedEndpoint.setMaxWidth(null);
117+
oembedEndpoint.setMaxHeight(null);
118+
119+
assertThat(oembedEndpoint.toApiUrl("http://vimeo.com/channels/everythinganimated/111627831")).hasToString(
120+
"https://vimeo.com/api/oembed.json?url=http%3A%2F%2Fvimeo.com%2Fchannels%2Feverythinganimated%2F111627831");
121+
}
122+
110123
@Test
111124
public void toApiUrlShouldWork2() {
112125

0 commit comments

Comments
 (0)