Coverage Report - com.jcabi.github.RtReleases
 
Classes in this File Line Coverage Branch Coverage Complexity
RtReleases
93%
30/32
0%
0/12
1
RtReleases$1
100%
5/5
N/A
1
RtReleases$AjcClosure1
0%
0/1
N/A
1
RtReleases$AjcClosure3
100%
1/1
N/A
1
RtReleases$AjcClosure5
100%
1/1
N/A
1
RtReleases$AjcClosure7
100%
1/1
N/A
1
RtReleases$AjcClosure9
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 releases.
 46  
  *
 47  
  * @author Paul Polishchuk (ppol@ua.fm)
 48  
  * @version $Id: 8d057938cc69e56b3e8aac14440247da80091919 $
 49  
  * @since 0.8
 50  
  */
 51  
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = "request")
 54  2
 final class RtReleases implements Releases {
 55  
 
 56  
     /**
 57  
      * RESTful API entry point.
 58  
      */
 59  
     private final transient Request entry;
 60  
 
 61  
     /**
 62  
      * RESTful API releases request.
 63  
      */
 64  
     private final transient Request request;
 65  
 
 66  
     /**
 67  
      * Repository.
 68  
      */
 69  
     private final transient Repo owner;
 70  
 
 71  
     /**
 72  
      * Public ctor.
 73  
      * @param req RESTful API entry point
 74  
      * @param repo Repository
 75  
      */
 76  6
     public RtReleases(final Request req, final Repo repo) {
 77  6
         this.entry = req;
 78  6
         this.owner = repo;
 79  6
         this.request = this.entry.uri()
 80  6
             .path("/repos")
 81  6
             .path(repo.coordinates().user())
 82  6
             .path(repo.coordinates().repo())
 83  6
             .path("/releases")
 84  6
             .back();
 85  6
     }
 86  
 
 87  
     @Override
 88  
     public Repo repo() {
 89  0
         return this.owner;
 90  
     }
 91  
 
 92  
     @Override
 93  
     public Iterable<Release> iterate() {
 94  4
         return new RtPagination<Release>(
 95  
             this.request,
 96  3
             new RtValuePagination.Mapping<Release, JsonObject>() {
 97  
                 @Override
 98  
                 public Release map(final JsonObject object) {
 99  2
                     return new RtRelease(
 100  1
                         RtReleases.this.entry,
 101  1
                         RtReleases.this.owner,
 102  
                         // @checkstyle MultipleStringLiterals (1 line)
 103  1
                         object.getInt("id")
 104  
                     );
 105  
                 }
 106  
             }
 107  
         );
 108  
     }
 109  
 
 110  
     @Override
 111  
     public Release get(final int number) {
 112  4
         return new RtRelease(this.entry, this.owner, number);
 113  
     }
 114  
 
 115  
     @Override
 116  
     public Release create(
 117  
         final String tag
 118  
     ) throws IOException {
 119  2
         final JsonStructure json = Json.createObjectBuilder()
 120  1
             .add("tag_name", tag)
 121  1
             .build();
 122  2
         return this.get(
 123  1
             this.request.method(Request.POST)
 124  1
                 .body().set(json).back()
 125  1
                 .fetch().as(RestResponse.class)
 126  1
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 127  1
                 .as(JsonResponse.class)
 128  1
                 .json().readObject().getInt("id")
 129  
         );
 130  
     }
 131  
 
 132  
     @Override
 133  
     public void remove(final int number) throws IOException {
 134  2
         this.request.method(Request.DELETE)
 135  1
             .uri().path(Integer.toString(number)).back()
 136  1
             .fetch()
 137  1
             .as(RestResponse.class)
 138  1
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 139  1
     }
 140  
 
 141  
 }