Coverage Report - com.jcabi.github.RtBlobs
 
Classes in this File Line Coverage Branch Coverage Complexity
RtBlobs
92%
26/28
0%
0/28
1
RtBlobs$AjcClosure1
0%
0/1
N/A
1
RtBlobs$AjcClosure3
100%
1/1
N/A
1
RtBlobs$AjcClosure5
100%
1/1
N/A
1
 
 1  2
 /**
 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 lombok.EqualsAndHashCode;
 41  
 
 42  
 /**
 43  
  * Github Git Data Blobs.
 44  
  * @author Alexander Lukashevich (sanai56967@gmail.com)
 45  
  * @version $Id: 90d33a80695cacd95250a579748c95132f8324ea $
 46  
  */
 47  
 @Immutable
 48  
 @Loggable(Loggable.DEBUG)
 49  0
 @EqualsAndHashCode(of = { "entry", "owner", "request" })
 50  
 final class RtBlobs implements Blobs {
 51  
 
 52  
     /**
 53  
      * API entry point.
 54  
      */
 55  
     private final transient Request entry;
 56  
 
 57  
     /**
 58  
      * RESTful request.
 59  
      */
 60  
     private final transient Request request;
 61  
 
 62  
     /**
 63  
      * Repository.
 64  
      */
 65  
     private final transient Repo owner;
 66  
 
 67  
     /**
 68  
      * Public ctor.
 69  
      * @param req Request
 70  
      * @param repo Repository
 71  
      */
 72  
     public RtBlobs(
 73  
         final Request req,
 74  2
         final Repo repo) {
 75  2
         this.entry = req;
 76  2
         final Coordinates coords = repo.coordinates();
 77  2
         this.request = this.entry.uri()
 78  2
             .path("/repos")
 79  2
             .path(coords.user())
 80  2
             .path(coords.repo())
 81  2
             .path("/git")
 82  2
             .path("/blobs")
 83  2
             .back();
 84  2
         this.owner = repo;
 85  2
     }
 86  
 
 87  
     @Override
 88  
     public Repo repo() {
 89  0
         return this.owner;
 90  
     }
 91  
 
 92  
     @Override
 93  
     public Blob get(
 94  
         final String sha) {
 95  4
         return new RtBlob(this.entry, this.owner, sha);
 96  
     }
 97  
 
 98  
     @Override
 99  
     public Blob create(
 100  
         final String content,
 101  
         final String encoding)
 102  
         throws IOException {
 103  3
         return this.get(
 104  1
             this.request.method(Request.POST)
 105  2
                 .body().set(
 106  1
                     Json.createObjectBuilder()
 107  1
                         .add("content", content)
 108  1
                         .add("encoding", encoding)
 109  1
                         .build()
 110  1
                 ).back()
 111  1
                 .fetch().as(RestResponse.class)
 112  1
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 113  1
                 .as(JsonResponse.class)
 114  1
                 .json().readObject().getString("sha")
 115  
         );
 116  
     }
 117  
 }