Skip to content

Commit

Permalink
feat(#909): fetch license from github
Browse files Browse the repository at this point in the history
  • Loading branch information
l3r8yJ committed Dec 9, 2024
1 parent 0be896a commit 9c5dce1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ SOFTWARE.
<artifactId>jhome</artifactId>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-http</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.yegor256</groupId>
<artifactId>jping</artifactId>
<version>0.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/org/eolang/jeo/representation/License.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
*/
package org.eolang.jeo.representation;

import com.jcabi.http.request.JdkRequest;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.core.HttpHeaders;
import java.io.IOException;
import org.cactoos.Scalar;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.cactoos.text.FormattedText;

/**
* Representation of project license.
Expand All @@ -35,29 +38,44 @@
public final class License implements Scalar<String> {

/**
* The name of file with license.
* Request url.
*/
private final String name;
private final String url;

/**
* Ctor with default value.
*/
public License() {
this("LICENSE.txt");
this(
new FormattedText(
"%s://%s%s",
"https",
"raw.githubusercontent.com",
"/objectionary/jeo-maven-plugin/refs/heads/master/LICENSE.txt"
).toString()
);
}

/**
* Primary ctor.
*
* @param name The name of file with license.
* @param url The url to fetch the license.
*/
public License(final String name) {
this.name = name;
public License(final String url) {
this.url = url;
}

@Override
public String value() {
return new TextOf(new ResourceOf(this.name)).toString();
try {
return new JdkRequest(this.url)
.method(HttpMethod.GET)
.header(HttpHeaders.ACCEPT, "text/html")
.fetch()
.body();
} catch (final IOException exc) {
throw new IllegalStateException("Can't fetch the license", exc);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import org.cactoos.scalar.Sticky;
import org.eolang.jeo.representation.License;
import org.xembly.Directive;
import org.xembly.Directives;
Expand Down Expand Up @@ -118,7 +119,7 @@ public Iterator<Directive> iterator() {
.add("listing")
.set(this.listing)
.up()
.add("license").set(new License()).up()
.add("license").set(new Sticky<>(new License())).up()
.append(this.metas)
.attr("ms", this.milliseconds)
.add("objects");
Expand Down
21 changes: 0 additions & 21 deletions src/main/resources/LICENSE.txt

This file was deleted.

7 changes: 5 additions & 2 deletions src/test/java/org/eolang/jeo/representation/LicenseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,30 @@
*/
package org.eolang.jeo.representation;

import com.yegor256.WeAreOnline;
import org.cactoos.text.FormattedText;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Test suite for {@link License}.
*
* @since 0.6.27
*/
@ExtendWith(WeAreOnline.class)
final class LicenseTest {

@Test
void readsLicenseFileCorrectly() throws Exception {
final String name = "LICENSE.txt";
MatcherAssert.assertThat(
new FormattedText(
"Unexpected file:'%s' content",
"Unexpected license content",
name
).asString(),
new License(name).value(),
new License().value(),
Matchers.containsString("MIT")
);
}
Expand Down

0 comments on commit 9c5dce1

Please sign in to comment.