Coverage Report - com.jcabi.github.RtGist
 
Classes in this File Line Coverage Branch Coverage Complexity
RtGist
78%
22/28
0%
0/18
1
RtGist$AjcClosure1
0%
0/1
N/A
1
RtGist$AjcClosure11
100%
1/1
N/A
1
RtGist$AjcClosure13
0%
0/1
N/A
1
RtGist$AjcClosure15
100%
1/1
N/A
1
RtGist$AjcClosure17
100%
1/1
N/A
1
RtGist$AjcClosure19
0%
0/1
N/A
1
RtGist$AjcClosure21
100%
1/1
N/A
1
RtGist$AjcClosure3
100%
1/1
N/A
1
RtGist$AjcClosure5
100%
1/1
N/A
1
RtGist$AjcClosure7
100%
1/1
N/A
1
RtGist$AjcClosure9
0%
0/1
N/A
1
 
 1  4
 /**
 2  
  * Copyright (c) 2013-2015, 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 com.jcabi.http.Request;
 35  
 import com.jcabi.http.Response;
 36  
 import com.jcabi.http.response.JsonResponse;
 37  
 import com.jcabi.http.response.RestResponse;
 38  
 import java.io.IOException;
 39  
 import java.net.HttpURLConnection;
 40  
 import java.net.URI;
 41  
 import javax.json.Json;
 42  
 import javax.json.JsonObject;
 43  
 import javax.json.JsonObjectBuilder;
 44  
 import javax.validation.constraints.NotNull;
 45  
 import lombok.EqualsAndHashCode;
 46  
 import org.hamcrest.Matchers;
 47  
 
 48  
 /**
 49  
  * Github gist.
 50  
  *
 51  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 52  
  * @version $Id$
 53  
  * @since 0.1
 54  
  * @checkstyle MultipleStringLiterals (500 lines)
 55  
  */
 56  
 @Immutable
 57  
 @Loggable(Loggable.DEBUG)
 58  0
 @EqualsAndHashCode(of = { "ghub", "request" })
 59  
 @SuppressWarnings("PMD.TooManyMethods")
 60  
 final class RtGist implements Gist {
 61  
     /**
 62  
      * RESTful request for the gist.
 63  
      */
 64  
     private final transient Request request;
 65  
 
 66  
     /**
 67  
      * Github.
 68  
      */
 69  
     private final transient Github ghub;
 70  
 
 71  
     /**
 72  
      * RESTful entry.
 73  
      */
 74  
     private final transient Request entry;
 75  
 
 76  
     /**
 77  
      * Gist id.
 78  
      */
 79  
     private final transient String gist;
 80  
 
 81  
     /**
 82  
      * Public ctor.
 83  
      * @param github Github
 84  
      * @param req Request
 85  
      * @param name Name of gist
 86  
      */
 87  13
     RtGist(final Github github, final Request req, final String name) {
 88  13
         this.ghub = github;
 89  13
         this.entry = req;
 90  13
         this.gist = name;
 91  13
         this.request = req.uri().path("/gists").path(name).back();
 92  13
     }
 93  
 
 94  
     @Override
 95  
     @NotNull(message = "toString is never NULL")
 96  
     public String toString() {
 97  1
         return this.request.uri().get().toString();
 98  
     }
 99  
 
 100  
     @Override
 101  
     @NotNull(message = "github can't be NULL")
 102  
     public Github github() {
 103  0
         return this.ghub;
 104  
     }
 105  
 
 106  
     @Override
 107  
     @NotNull(message = "identifier can't be NULL")
 108  
     public String identifier() {
 109  4
         return this.gist;
 110  
     }
 111  
 
 112  
     @Override
 113  
     @NotNull(message = "file content can't be NULL")
 114  
     public String read(@NotNull(message = "file name can't be NULL")
 115  
         final String file) throws IOException {
 116  6
         final Response response = this.request.fetch();
 117  3
         final String url = response
 118  
             .as(RestResponse.class)
 119  
             .assertStatus(HttpURLConnection.HTTP_OK)
 120  
             .as(JsonResponse.class)
 121  
             .json().readObject().getJsonObject("files")
 122  
             .getJsonObject(file).getString("raw_url");
 123  3
         return response
 124  
             .as(RestResponse.class)
 125  
             .jump(URI.create(url))
 126  
             .fetch()
 127  
             .as(RestResponse.class)
 128  
             .assertStatus(HttpURLConnection.HTTP_OK)
 129  
             .body();
 130  
     }
 131  
 
 132  
     @Override
 133  
     public void write(
 134  
         @NotNull(message = "file name can't be NULL") final String file,
 135  
         @NotNull(message = "file content can't be NULL") final String content)
 136  
         throws IOException {
 137  2
         final JsonObjectBuilder builder = Json.createObjectBuilder()
 138  
             .add("content", content);
 139  1
         final JsonObject json = Json.createObjectBuilder()
 140  
             .add("files", Json.createObjectBuilder().add(file, builder))
 141  
             .build();
 142  1
         this.patch(json);
 143  1
     }
 144  
 
 145  
     @Override
 146  
     public void star() throws IOException {
 147  0
         this.request.uri().path("star").back()
 148  
             .method("PUT")
 149  
             .fetch().as(RestResponse.class)
 150  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 151  0
     }
 152  
 
 153  
     @Override
 154  
     public void unstar() throws IOException {
 155  2
         this.request.uri().path("star").back()
 156  
             .method(Request.DELETE)
 157  
             .fetch().as(RestResponse.class)
 158  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 159  1
     }
 160  
 
 161  
     @Override
 162  
     public boolean starred() throws IOException {
 163  0
         return this.request.uri().path("star").back()
 164  
             .method("GET").fetch()
 165  
             .as(RestResponse.class).assertStatus(
 166  
                 Matchers.isOneOf(
 167  
                     HttpURLConnection.HTTP_NO_CONTENT,
 168  
                     HttpURLConnection.HTTP_NOT_FOUND
 169  
             )
 170  
         ).status() == HttpURLConnection.HTTP_NO_CONTENT;
 171  
     }
 172  
 
 173  
     @Override
 174  
     @NotNull(message = "Gist can't be NULL")
 175  
     public Gist fork() throws IOException {
 176  2
         return new RtGist(
 177  
             this.ghub, this.entry,
 178  
             this.request.uri().path("/forks").back()
 179  
                 .method(Request.POST)
 180  
                 .fetch().as(RestResponse.class)
 181  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 182  
                 .as(JsonResponse.class)
 183  
                 .json().readObject().getString("id")
 184  
         );
 185  
     }
 186  
 
 187  
     @Override
 188  
     @NotNull(message = "JSON can't be NULL")
 189  
     public JsonObject json() throws IOException {
 190  2
         return new RtJson(this.request).fetch();
 191  
     }
 192  
 
 193  
     @Override
 194  
     @NotNull(message = "GistComments can't be NULL")
 195  
     public GistComments comments() throws IOException {
 196  0
         return new RtGistComments(this.entry, this);
 197  
     }
 198  
 
 199  
     @Override
 200  
     public void patch(@NotNull(message = "JSON can't be NULL")
 201  
         final JsonObject json) throws IOException {
 202  4
         new RtJson(this.request).patch(json);
 203  2
     }
 204  
 }