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