From 9c5dce1c7b9d8e4b4c76db66976890bc088c54a8 Mon Sep 17 00:00:00 2001 From: l3r8yJ Date: Mon, 9 Dec 2024 16:06:17 +0300 Subject: [PATCH] feat(#909): fetch license from github --- pom.xml | 11 ++++++ .../eolang/jeo/representation/License.java | 36 ++++++++++++++----- .../directives/DirectivesProgram.java | 3 +- src/main/resources/LICENSE.txt | 21 ----------- .../jeo/representation/LicenseTest.java | 7 ++-- 5 files changed, 45 insertions(+), 33 deletions(-) delete mode 100644 src/main/resources/LICENSE.txt diff --git a/pom.xml b/pom.xml index d5e0aff91..650d3b854 100644 --- a/pom.xml +++ b/pom.xml @@ -196,6 +196,17 @@ SOFTWARE. jhome 0.0.5 + + com.jcabi + jcabi-http + 2.0.0 + + + com.yegor256 + jping + 0.0.3 + test + org.hamcrest hamcrest diff --git a/src/main/java/org/eolang/jeo/representation/License.java b/src/main/java/org/eolang/jeo/representation/License.java index 95facc980..a2d5f7482 100644 --- a/src/main/java/org/eolang/jeo/representation/License.java +++ b/src/main/java/org/eolang/jeo/representation/License.java @@ -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. @@ -35,29 +38,44 @@ public final class License implements Scalar { /** - * 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 diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesProgram.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesProgram.java index 93d803315..368bd4032 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesProgram.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesProgram.java @@ -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; @@ -118,7 +119,7 @@ public Iterator 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"); diff --git a/src/main/resources/LICENSE.txt b/src/main/resources/LICENSE.txt deleted file mode 100644 index 879402bb7..000000000 --- a/src/main/resources/LICENSE.txt +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2016-2024 Objectionary.com - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/src/test/java/org/eolang/jeo/representation/LicenseTest.java b/src/test/java/org/eolang/jeo/representation/LicenseTest.java index 89d1940de..0fcd617d7 100644 --- a/src/test/java/org/eolang/jeo/representation/LicenseTest.java +++ b/src/test/java/org/eolang/jeo/representation/LicenseTest.java @@ -23,16 +23,19 @@ */ 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 @@ -40,10 +43,10 @@ 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") ); }