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