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