|
| 1 | +/* |
| 2 | + * MMDBot - https://github.com/MinecraftModDevelopment/MMDBot |
| 3 | + * Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment> |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2.1 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + * USA |
| 19 | + * https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html |
| 20 | + */ |
| 21 | +package com.mcmoddev.mmdbot.gist; |
| 22 | + |
| 23 | +import com.google.gson.JsonObject; |
| 24 | +import com.mcmoddev.mmdbot.core.References; |
| 25 | +import com.mcmoddev.mmdbot.utilities.Utils; |
| 26 | +import kotlinx.serialization.json.Json; |
| 27 | + |
| 28 | +import javax.annotation.Nullable; |
| 29 | +import java.util.LinkedHashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +/** |
| 33 | + * Represents a gist which was got from an HTTP request |
| 34 | + * |
| 35 | + * @author matyrobbrt |
| 36 | + */ |
| 37 | +public record ExistingGist(JsonObject json, String url, String forksUrl, String commitsUrl, |
| 38 | + String id, boolean isPublic, String commentsUrl, int comments, |
| 39 | + String htmlUrl, String gitPullUrl, String gitPushUrl, String createdAt, |
| 40 | + String updatedAt, Map<String, File> files, @Nullable String description) { |
| 41 | + |
| 42 | + public static ExistingGist fromJson(final JsonObject json) { |
| 43 | + var url = getFromJson(json, "url"); |
| 44 | + var id = getFromJson(json, "id"); |
| 45 | + var isPublic = json.get("public").getAsBoolean(); |
| 46 | + var commentsUrl = getFromJson(json, "comments_url"); |
| 47 | + var comments = json.get("comments").getAsInt(); |
| 48 | + var htmlUrl = getFromJson(json, "html_url"); |
| 49 | + var gitPullUrl = getFromJson(json, "git_pull_url"); |
| 50 | + var gitPushUrl = getFromJson(json, "git_push_url"); |
| 51 | + var createdAt = getFromJson(json, "created_at"); |
| 52 | + var updatedAt = getFromJson(json, "updated_at"); |
| 53 | + var forksUrl = getFromJson(json, "forks_url"); |
| 54 | + var commitsUrl = getFromJson(json, "commits_url"); |
| 55 | + var description = getFromJson(json, "description"); |
| 56 | + |
| 57 | + var files = new LinkedHashMap<String, File>(); |
| 58 | + |
| 59 | + final var filesJson = json.get("files").getAsJsonObject(); |
| 60 | + for (final String key : filesJson.keySet()) { |
| 61 | + files.put(key, File.fromJson(filesJson.get(key).getAsJsonObject())); |
| 62 | + } |
| 63 | + return new ExistingGist(json, url, forksUrl, commitsUrl, id, isPublic, commentsUrl, comments, htmlUrl, gitPullUrl, |
| 64 | + gitPushUrl, createdAt, updatedAt, files, description); |
| 65 | + } |
| 66 | + |
| 67 | + private static String getFromJson(final JsonObject json, final String key) { |
| 68 | + return json.get(key).isJsonPrimitive() && json.get(key).getAsJsonPrimitive().isString() ? json.get(key).getAsString() : null; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String toString() { |
| 73 | + return json.toString(); |
| 74 | + } |
| 75 | + |
| 76 | + public record File(int size, String url, String type, boolean isTruncated) { |
| 77 | + |
| 78 | + public static File fromJson(final JsonObject json) { |
| 79 | + final var size = json.get("size").getAsInt(); |
| 80 | + final var url = json.get("raw_url").getAsString(); |
| 81 | + final var type = json.get("type").getAsString(); |
| 82 | + final var truncated = json.has("truncated") && json.get("truncated").getAsBoolean(); |
| 83 | + return new File(size, url, type, truncated); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments