Coverage Report - com.jcabi.github.RtComments
 
Classes in this File Line Coverage Branch Coverage Complexity
RtComments
42%
14/33
0%
0/20
1
RtComments$1
0%
0/2
N/A
1
RtComments$AjcClosure1
0%
0/1
N/A
1
RtComments$AjcClosure3
0%
0/1
N/A
1
RtComments$AjcClosure5
0%
0/1
N/A
1
RtComments$AjcClosure7
0%
0/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 java.util.Date;
 40  
 import javax.json.Json;
 41  
 import javax.json.JsonObject;
 42  
 import javax.json.JsonStructure;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github comments.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id: a04ee0f3817d55a9aa6d3e2f679da76d87ac0ea1 $
 50  
  * @since 0.1
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = { "request", "owner" })
 55  
 final class RtComments implements Comments {
 56  
 
 57  
     /**
 58  
      * API entry point.
 59  
      */
 60  
     private final transient Request entry;
 61  
 
 62  
     /**
 63  
      * RESTful request.
 64  
      */
 65  
     private final transient Request request;
 66  
 
 67  
     /**
 68  
      * Owner of comments.
 69  
      */
 70  
     private final transient Issue owner;
 71  
 
 72  
     /**
 73  
      * Public ctor.
 74  
      * @param req Request
 75  
      * @param issue Issue
 76  
      */
 77  1
     RtComments(final Request req, final Issue issue) {
 78  1
         this.entry = req;
 79  1
         final Coordinates coords = issue.repo().coordinates();
 80  1
         this.request = this.entry.uri()
 81  1
             .path("/repos")
 82  1
             .path(coords.user())
 83  1
             .path(coords.repo())
 84  1
             .path("/issues")
 85  1
             .path(Integer.toString(issue.number()))
 86  1
             .path("/comments")
 87  1
             .back();
 88  1
         this.owner = issue;
 89  1
     }
 90  
 
 91  
     @Override
 92  
     public String toString() {
 93  0
         return this.request.uri().get().toString();
 94  
     }
 95  
 
 96  
     @Override
 97  
     public Issue issue() {
 98  0
         return this.owner;
 99  
     }
 100  
 
 101  
     @Override
 102  
     public Comment get(final int number) {
 103  0
         return new RtComment(this.entry, this.owner, number);
 104  
     }
 105  
 
 106  
     @Override
 107  
     public Comment post(final String text) throws IOException {
 108  0
         final JsonStructure json = Json.createObjectBuilder()
 109  0
             .add("body", text)
 110  0
             .build();
 111  0
         return this.get(
 112  0
             this.request.method(Request.POST)
 113  0
                 .body().set(json).back()
 114  0
                 .fetch()
 115  0
                 .as(RestResponse.class)
 116  0
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 117  0
                 .as(JsonResponse.class)
 118  
                 // @checkstyle MultipleStringLiterals (1 line)
 119  0
                 .json().readObject().getInt("id")
 120  
         );
 121  
     }
 122  
 
 123  
     @Override
 124  
     public Iterable<Comment> iterate(final Date since) {
 125  0
         return new RtPagination<Comment>(
 126  0
             this.request.uri()
 127  0
                 .queryParam("since", new Github.Time(since))
 128  0
                 .back(),
 129  0
             new RtValuePagination.Mapping<Comment, JsonObject>() {
 130  
                 @Override
 131  
                 public Comment map(final JsonObject object) {
 132  0
                     return RtComments.this.get(object.getInt("id"));
 133  
                 }
 134  
             }
 135  
         );
 136  
     }
 137  
 
 138  
 }