1 /** 2 * Copyright (c) 2013-2023, jcabi.com 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 1) Redistributions of source code must retain the above 8 * copyright notice, this list of conditions and the following 9 * disclaimer. 2) Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 3) Neither the name of the jcabi.com nor 13 * the names of its contributors may be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 19 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 20 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 28 * OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.jcabi.github; 31 32 import com.jcabi.aspects.Immutable; 33 import com.jcabi.aspects.Loggable; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.Collection; 37 import javax.json.JsonObject; 38 import javax.json.JsonValue; 39 import lombok.EqualsAndHashCode; 40 import lombok.ToString; 41 42 /** 43 * Github gist. 44 * 45 * @author Yegor Bugayenko (yegor256@gmail.com) 46 * @version $Id: a12cd3c4d79308fe16729bb0343521354d9eb24d $ 47 * @since 0.1 48 * @see <a href="https://developer.github.com/v3/gists/">Gists API</a> 49 */ 50 @Immutable 51 @SuppressWarnings("PMD.TooManyMethods") 52 public interface Gist extends JsonReadable, JsonPatchable { 53 54 /** 55 * Github we're in. 56 * @return Github 57 */ 58 Github github(); 59 60 /** 61 * Get gist identifier. 62 * @return Gist identifier 63 */ 64 String identifier(); 65 66 /** 67 * Read file content. 68 * @param name Name of it 69 * @return File content 70 * @throws IOException If there is any I/O problem 71 * @see <a href="https://developer.github.com/v3/gists/#get-a-single-gist">Get a Single Gist</a> 72 */ 73 String read(String name) throws IOException; 74 75 /** 76 * Write file content. 77 * @param name Name of it 78 * @param content Content to write 79 * @throws IOException If there is any I/O problem 80 * @see <a href="https://developer.github.com/v3/gists/#edit-a-gist">Edit a Gist</a> 81 */ 82 void write(String name, String content) throws IOException; 83 84 /** 85 * Star a gist. 86 * @throws IOException If there is any I/O problem 87 */ 88 void star() throws IOException; 89 90 /** 91 * Unstar a gist. 92 * @throws IOException If there is any I/O problem 93 */ 94 void unstar() throws IOException; 95 96 /** 97 * Checks if Gist is starred. 98 * @throws IOException If there is any I/O problem 99 * @return True if gist is starred 100 */ 101 boolean starred() throws IOException; 102 103 /** 104 * Fork the gist. 105 * @return Forked gist 106 * @throws IOException If there is any I/O problem 107 */ 108 Gist fork() throws IOException; 109 110 /** 111 * Get all comments of the gist. 112 * @return GistComments 113 * @throws IOException If there is any I/O problem 114 * @see <a href="https://developer.github.com/v3/gists/comments/">Gist Comments API</a> 115 */ 116 GistComments comments() throws IOException; 117 118 /** 119 * Smart Gist with extra features. 120 */ 121 @Immutable 122 @ToString 123 @Loggable(Loggable.DEBUG) 124 @EqualsAndHashCode(of = "gist") 125 final class Smart implements Gist { 126 /** 127 * Encapsulated gist. 128 */ 129 private final transient Gist gist; 130 /** 131 * Public ctor. 132 * @param gst Gist 133 */ 134 public Smart(final Gist gst) { 135 this.gist = gst; 136 } 137 138 /** 139 * Get gist id. 140 * @return Gist id 141 */ 142 @Override 143 public String identifier() { 144 return this.gist.identifier(); 145 } 146 147 /** 148 * Get a list of all file names in the gist. 149 * @return File names 150 * @throws IOException If there is any I/O problem 151 */ 152 public Iterable<String> files() throws IOException { 153 final JsonObject array = this.gist.json().getJsonObject("files"); 154 final Collection<String> files = 155 new ArrayList<>(array.size()); 156 for (final JsonValue value : array.values()) { 157 files.add(JsonObject.class.cast(value).getString("filename")); 158 } 159 return files; 160 } 161 @Override 162 public Github github() { 163 return this.gist.github(); 164 } 165 @Override 166 public String read(final String name) throws IOException { 167 return this.gist.read(name); 168 } 169 @Override 170 public void write(final String name, final String content) 171 throws IOException { 172 this.gist.write(name, content); 173 } 174 175 @Override 176 public void star() throws IOException { 177 this.gist.star(); 178 } 179 180 @Override 181 public void unstar() throws IOException { 182 this.gist.unstar(); 183 } 184 185 @Override 186 public boolean starred() throws IOException { 187 return this.gist.starred(); 188 } 189 190 @Override 191 public Gist fork() throws IOException { 192 return this.gist.fork(); 193 } 194 195 @Override 196 public GistComments comments() throws IOException { 197 return this.gist.comments(); 198 } 199 200 @Override 201 public JsonObject json() throws IOException { 202 return this.gist.json(); 203 } 204 205 @Override 206 public void patch(final JsonObject json) throws IOException { 207 this.gist.patch(json); 208 } 209 } 210 211 }