Coverage Report - com.jcabi.github.RtGists
 
Classes in this File Line Coverage Branch Coverage Complexity
RtGists
84%
16/19
11%
2/18
1.125
RtGists$1
100%
2/2
N/A
1.125
RtGists$AjcClosure1
0%
0/1
N/A
1.125
RtGists$AjcClosure3
100%
1/1
N/A
1.125
RtGists$AjcClosure5
100%
1/1
N/A
1.125
RtGists$AjcClosure7
100%
1/1
N/A
1.125
RtGists$AjcClosure9
100%
1/1
N/A
1.125
 
 1  2
 /**
 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.JsonResponse;
 36  
 import com.jcabi.http.response.RestResponse;
 37  
 import java.io.IOException;
 38  
 import java.net.HttpURLConnection;
 39  
 import java.util.Map;
 40  
 import javax.json.Json;
 41  
 import javax.json.JsonObject;
 42  
 import javax.json.JsonObjectBuilder;
 43  
 import javax.json.JsonStructure;
 44  
 import javax.validation.constraints.NotNull;
 45  
 import lombok.EqualsAndHashCode;
 46  
 
 47  
 /**
 48  
  * Github gists.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 51  
  * @version $Id$
 52  
  * @since 0.1
 53  
  * @checkstyle MultipleStringLiterals (500 lines)
 54  
  */
 55  
 @Immutable
 56  
 @Loggable(Loggable.DEBUG)
 57  0
 @EqualsAndHashCode(of = { "ghub", "request" })
 58  
 final class RtGists implements Gists {
 59  
 
 60  
     /**
 61  
      * API entry point.
 62  
      */
 63  
     private final transient Request entry;
 64  
 
 65  
     /**
 66  
      * Github.
 67  
      */
 68  
     private final transient Github ghub;
 69  
 
 70  
     /**
 71  
      * RESTful request.
 72  
      */
 73  
     private final transient Request request;
 74  
 
 75  
     /**
 76  
      * Public ctor.
 77  
      * @param github Github
 78  
      * @param req Request
 79  
      */
 80  5
     RtGists(final Github github, final Request req) {
 81  5
         this.entry = req;
 82  5
         this.ghub = github;
 83  5
         this.request = this.entry.uri().path("/gists").back();
 84  5
     }
 85  
 
 86  
     @Override
 87  
     @NotNull(message = "toString is never NULL")
 88  
     public String toString() {
 89  0
         return this.request.uri().get().toString();
 90  
     }
 91  
 
 92  
     @Override
 93  
     @NotNull(message = "Github can't be NULL")
 94  
     public Github github() {
 95  0
         return this.ghub;
 96  
     }
 97  
 
 98  
     @Override
 99  
     @NotNull(message = "Gist can't be NULL")
 100  
     public Gist create(@NotNull(message = "list of files can't be NULL")
 101  
         final Map<String, String> files, final boolean visible
 102  
     ) throws IOException {
 103  2
         JsonObjectBuilder builder = Json.createObjectBuilder();
 104  1
         for (final Map.Entry<String, String> file : files.entrySet()) {
 105  1
             builder = builder.add(
 106  
                 file.getKey(),
 107  
                 Json.createObjectBuilder().add("content", file.getValue())
 108  
             );
 109  1
         }
 110  1
         final JsonStructure json = Json.createObjectBuilder()
 111  
             .add("files", builder)
 112  
             .add("public", visible)
 113  
             .build();
 114  1
         return this.get(
 115  
             this.request.method(Request.POST)
 116  
                 .body().set(json).back()
 117  
                 .fetch().as(RestResponse.class)
 118  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 119  
                 .as(JsonResponse.class)
 120  
                 .json().readObject().getString("id")
 121  
         );
 122  
     }
 123  
 
 124  
     @Override
 125  
     @NotNull(message = "Gist can't be NULL")
 126  
     public Gist get(@NotNull(message = "gist name can't be NULL")
 127  
         final String name) {
 128  6
         return new RtGist(this.ghub, this.entry, name);
 129  
     }
 130  
 
 131  
     @Override
 132  
     @NotNull(message = "Iterable of Gist can't be NULL")
 133  
     public Iterable<Gist> iterate() {
 134  2
         return new RtPagination<Gist>(
 135  
             this.request,
 136  2
             new RtValuePagination.Mapping<Gist, JsonObject>() {
 137  
                 @Override
 138  
                 public Gist map(final JsonObject object) {
 139  1
                     return RtGists.this.get(object.getString("id"));
 140  
                 }
 141  
             }
 142  
         );
 143  
     }
 144  
 
 145  
     @Override
 146  
     public void remove(
 147  
         @NotNull(message = "identifier can't be NULL") final String identifier
 148  
     ) throws IOException {
 149  2
         this.request.method(Request.DELETE)
 150  
             .uri().path(identifier).back()
 151  
             .fetch()
 152  
             .as(RestResponse.class)
 153  
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 154  1
     }
 155  
 }