Coverage Report - com.jcabi.github.RtGitignores
 
Classes in this File Line Coverage Branch Coverage Complexity
RtGitignores
93%
14/15
11%
2/18
1.25
 
 1  2
 /**
 2  
  * Copyright (c) 2013-2015, 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.http.Request;
 34  
 import com.jcabi.http.response.JsonResponse;
 35  
 import com.jcabi.http.response.RestResponse;
 36  
 import java.io.IOException;
 37  
 import java.net.HttpURLConnection;
 38  
 import java.util.ArrayList;
 39  
 import java.util.List;
 40  
 import javax.json.JsonString;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import javax.ws.rs.core.HttpHeaders;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github Gitignore.
 47  
  * <p>Defines storage of .gitignore templates
 48  
  *
 49  
  * @author Paul Polishchuk (ppol@ua.fm)
 50  
  * @version $Id$
 51  
  * @since 0.8
 52  
  */
 53  
 @Immutable
 54  0
 @EqualsAndHashCode(of = { "ghub" , "request" })
 55  
 final class RtGitignores implements Gitignores {
 56  
 
 57  
     /**
 58  
      * Github.
 59  
      */
 60  
     private final transient Github ghub;
 61  
 
 62  
     /**
 63  
      * RESTful request.
 64  
      */
 65  
     private final transient Request request;
 66  
 
 67  
     /**
 68  
      * Public CTOR.
 69  
      * @param github Github
 70  
      */
 71  
     public RtGitignores(
 72  3
         @NotNull(message = "github can't be NULL") final Github github) {
 73  3
         this.ghub = github;
 74  3
         this.request = github().entry().uri()
 75  
             .path("/gitignore/templates").back();
 76  3
     }
 77  
 
 78  
     @Override
 79  
     @NotNull(message = "Github is never NULL")
 80  
     public Github github() {
 81  3
         return this.ghub;
 82  
     }
 83  
 
 84  
     @Override
 85  
     @NotNull(message = "iterable is never NULL")
 86  
     public Iterable<String> iterate() throws IOException {
 87  1
         final RestResponse response = this.request.fetch()
 88  
             .as(RestResponse.class)
 89  
             .assertStatus(HttpURLConnection.HTTP_OK);
 90  1
         final List<JsonString> list = response.as(JsonResponse.class)
 91  
             .json().readArray().getValuesAs(JsonString.class);
 92  1
         final List<String> templates = new ArrayList<String>(list.size());
 93  1
         for (final JsonString value : list) {
 94  2
             templates.add(value.getString());
 95  2
         }
 96  1
         return templates;
 97  
     }
 98  
 
 99  
     @Override
 100  
     @NotNull(message = "template is never NULL")
 101  
     public String template(
 102  
         @NotNull(message = "Template name can't be NULL")
 103  
         final String name)
 104  
         throws IOException {
 105  1
         return this.request.reset(HttpHeaders.ACCEPT)
 106  
             .header(HttpHeaders.ACCEPT, "application/vnd.github.v3.raw")
 107  
             .uri().path(name).back().fetch()
 108  
             .as(RestResponse.class)
 109  
             .assertStatus(HttpURLConnection.HTTP_OK)
 110  
             .body();
 111  
     }
 112  
 }