Coverage Report - com.jcabi.github.RtRepos
 
Classes in this File Line Coverage Branch Coverage Complexity
RtRepos
88%
23/26
0%
0/20
1
RtRepos$1
100%
3/3
N/A
1
RtRepos$AjcClosure1
0%
0/1
N/A
1
RtRepos$AjcClosure3
100%
1/1
N/A
1
RtRepos$AjcClosure5
100%
1/1
N/A
1
RtRepos$AjcClosure7
100%
1/1
N/A
1
RtRepos$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.JsonObject;
 40  
 import lombok.EqualsAndHashCode;
 41  
 
 42  
 /**
 43  
  * Github repositories.
 44  
  *
 45  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 46  
  * @version $Id: 16b262db998ed00e17386703ec3d2b223b4a883c $
 47  
  * @since 0.8
 48  
  * @checkstyle ClassDataAbstractionCoupling (500 lines)
 49  
  */
 50  
 @Immutable
 51  
 @Loggable(Loggable.DEBUG)
 52  0
 @EqualsAndHashCode(of = { "ghub", "entry" })
 53  
 final class RtRepos implements Repos {
 54  
 
 55  
     /**
 56  
      * Github.
 57  
      */
 58  
     private final transient Github ghub;
 59  
 
 60  
     /**
 61  
      * RESTful entry.
 62  
      */
 63  
     private final transient Request entry;
 64  
 
 65  
     /**
 66  
      * Public ctor.
 67  
      * @param github Github
 68  
      * @param req Request
 69  
      */
 70  18
     RtRepos(final Github github, final Request req) {
 71  18
         this.ghub = github;
 72  18
         this.entry = req;
 73  18
     }
 74  
 
 75  
     @Override
 76  
     public String toString() {
 77  0
         return this.entry.uri().get().toString();
 78  
     }
 79  
 
 80  
     @Override
 81  
     public Github github() {
 82  0
         return this.ghub;
 83  
     }
 84  
 
 85  
     @Override
 86  
     public Repo create(final RepoCreate settings) throws IOException {
 87  3
         return this.get(
 88  
             new Coordinates.Simple(
 89  1
                 this.entry.uri().path("user/repos")
 90  1
                     .back().method(Request.POST)
 91  1
                     .body().set(settings.json()).back()
 92  1
                     .fetch().as(RestResponse.class)
 93  1
                     .assertStatus(HttpURLConnection.HTTP_CREATED)
 94  1
                     .as(JsonResponse.class)
 95  
                     // @checkstyle MultipleStringLiterals (1 line)
 96  1
                     .json().readObject().getString("full_name")
 97  
             )
 98  
         );
 99  
     }
 100  
 
 101  
     @Override
 102  
     public Repo get(final Coordinates name) {
 103  34
         return new RtRepo(this.ghub, this.entry, name);
 104  
     }
 105  
 
 106  
     @Override
 107  
     public void remove(
 108  
         final Coordinates coords) throws IOException {
 109  2
         this.entry.uri().path("/repos")
 110  1
             .back().method(Request.DELETE)
 111  1
             .uri().path(coords.toString()).back()
 112  1
             .fetch()
 113  1
             .as(RestResponse.class)
 114  1
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 115  1
     }
 116  
 
 117  
     @Override
 118  
     public Iterable<Repo> iterate(
 119  
         final String identifier) {
 120  3
         return new RtPagination<Repo>(
 121  1
             this.entry.uri().queryParam("since", identifier).back(),
 122  3
             new RtValuePagination.Mapping<Repo, JsonObject>() {
 123  
                 @Override
 124  
                 public Repo map(final JsonObject object) {
 125  4
                     return RtRepos.this.get(
 126  2
                         new Coordinates.Simple(object.getString("full_name"))
 127  
                     );
 128  
                 }
 129  
             }
 130  
         );
 131  
     }
 132  
 }