Coverage Report - com.jcabi.github.RtContent
 
Classes in this File Line Coverage Branch Coverage Complexity
RtContent
88%
24/27
0%
0/28
1
RtContent$AjcClosure1
0%
0/1
N/A
1
RtContent$AjcClosure11
100%
1/1
N/A
1
RtContent$AjcClosure3
100%
1/1
N/A
1
RtContent$AjcClosure5
100%
1/1
N/A
1
RtContent$AjcClosure7
100%
1/1
N/A
1
RtContent$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.RestResponse;
 36  
 import java.io.ByteArrayInputStream;
 37  
 import java.io.IOException;
 38  
 import java.io.InputStream;
 39  
 import java.net.HttpURLConnection;
 40  
 import javax.json.JsonObject;
 41  
 import javax.ws.rs.core.HttpHeaders;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Github content.
 46  
  *
 47  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 48  
  * @version $Id: cd4787bfde44efdc60aa52971ede32056d2c481b $
 49  
  */
 50  0
 @Immutable
 51  
 @Loggable(Loggable.DEBUG)
 52  0
 @EqualsAndHashCode(of = { "location", "request", "owner" })
 53  
 final class RtContent implements Content {
 54  
 
 55  
     /**
 56  
      * RESTful request.
 57  
      */
 58  
     private final transient Request request;
 59  
 
 60  
     /**
 61  
      * Repository we're in.
 62  
      */
 63  
     private final transient Repo owner;
 64  
 
 65  
     /**
 66  
      * Path of this Content.
 67  
      */
 68  
     private final transient String location;
 69  
 
 70  
     /**
 71  
      * Public ctor.
 72  
      * @param req Request
 73  
      * @param repo Repository
 74  
      * @param path Path of the content
 75  
      */
 76  13
     RtContent(final Request req, final Repo repo, final String path) {
 77  13
         final Coordinates coords = repo.coordinates();
 78  13
         this.request = req.uri()
 79  13
             .path("/repos")
 80  13
             .path(coords.user())
 81  13
             .path(coords.repo())
 82  13
             .path("/contents")
 83  13
             .path(path)
 84  13
             .back();
 85  13
         this.owner = repo;
 86  13
         this.location = path;
 87  13
     }
 88  
 
 89  
     @Override
 90  
     public Repo repo() {
 91  0
         return this.owner;
 92  
     }
 93  
 
 94  
     @Override
 95  
     public String path() {
 96  20
         return this.location;
 97  
     }
 98  
 
 99  
     @Override
 100  
     public int compareTo(
 101  
         final Content other
 102  
     ) {
 103  6
         return this.path().compareTo(other.path());
 104  
     }
 105  
 
 106  
     @Override
 107  
     public JsonObject json() throws IOException {
 108  6
         return new RtJson(this.request).fetch();
 109  
     }
 110  
 
 111  
     @Override
 112  
     public void patch(final JsonObject json) throws IOException {
 113  2
         new RtJson(this.request).patch(json);
 114  1
     }
 115  
 
 116  
     @Override
 117  
     public InputStream raw() throws IOException {
 118  3
         return new ByteArrayInputStream(
 119  1
             this.request.reset(HttpHeaders.ACCEPT)
 120  1
                 .header(
 121  
                     HttpHeaders.ACCEPT,
 122  
                     "application/vnd.github.v3.raw"
 123  1
                 ).fetch()
 124  1
                 .as(RestResponse.class)
 125  1
                 .assertStatus(HttpURLConnection.HTTP_OK).binary()
 126  
         );
 127  
     }
 128  
 }