Coverage Report - com.jcabi.github.RtGistComments
 
Classes in this File Line Coverage Branch Coverage Complexity
RtGistComments
88%
23/26
0%
0/20
1
RtGistComments$1
100%
2/2
N/A
1
RtGistComments$AjcClosure1
0%
0/1
N/A
1
RtGistComments$AjcClosure3
100%
1/1
N/A
1
RtGistComments$AjcClosure5
100%
1/1
N/A
1
RtGistComments$AjcClosure7
100%
1/1
N/A
1
 
 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 javax.json.Json;
 40  
 import javax.json.JsonObject;
 41  
 import javax.json.JsonStructure;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Github comments.
 46  
  *
 47  
  * @author Giang Le (giang@vn-smartsolutions.com)
 48  
  * @version $Id: 9d6b7b40d2ab87b321a60880f15547df2d48fb86 $
 49  
  * @since 0.8
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "request", "owner" })
 54  
 final class RtGistComments implements GistComments {
 55  
     /**
 56  
      * API entry point.
 57  
      */
 58  
     private final transient Request entry;
 59  
 
 60  
     /**
 61  
      * RESTful request.
 62  
      */
 63  
     private final transient Request request;
 64  
 
 65  
     /**
 66  
      * Owner of comments.
 67  
      */
 68  
     private final transient Gist owner;
 69  
 
 70  
     /**
 71  
      * Public ctor.
 72  
      * @param req Request
 73  
      * @param gist Gist
 74  
      */
 75  3
     RtGistComments(final Request req, final Gist gist) {
 76  3
         this.entry = req;
 77  3
         this.request = this.entry.uri()
 78  3
             .path("/gists")
 79  3
             .path(gist.identifier())
 80  3
             .path("/comments")
 81  3
             .back();
 82  3
         this.owner = gist;
 83  3
     }
 84  
 
 85  
     @Override
 86  
     public String toString() {
 87  0
         return this.request.uri().get().toString();
 88  
     }
 89  
 
 90  
     @Override
 91  
     public Gist gist() {
 92  0
         return this.owner;
 93  
     }
 94  
 
 95  
     @Override
 96  
     public GistComment get(final int number) {
 97  8
         return new RtGistComment(this.entry, this.owner, number);
 98  
     }
 99  
 
 100  
     @Override
 101  
     public GistComment post(
 102  
         final String text
 103  
     ) throws IOException {
 104  2
         final JsonStructure json = Json.createObjectBuilder()
 105  1
             .add("body", text)
 106  1
             .build();
 107  2
         return this.get(
 108  1
             this.request.method(Request.POST)
 109  1
                 .body().set(json).back()
 110  1
                 .fetch()
 111  1
                 .as(RestResponse.class)
 112  1
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 113  1
                 .as(JsonResponse.class)
 114  
                 // @checkstyle MultipleStringLiterals (1 line)
 115  1
                 .json().readObject().getInt("id")
 116  
         );
 117  
     }
 118  
 
 119  
     @Override
 120  
     public Iterable<GistComment> iterate() {
 121  2
         return new RtPagination<GistComment>(
 122  
             this.request,
 123  3
             new RtValuePagination.Mapping<GistComment, JsonObject>() {
 124  
                 @Override
 125  
                 public GistComment map(final JsonObject object) {
 126  2
                     return RtGistComments.this.get(object.getInt("id"));
 127  
                 }
 128  
             }
 129  
         );
 130  
     }
 131  
 }