Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RtForks |
|
| 1.0;1 | ||||
RtForks$1 |
|
| 1.0;1 | ||||
RtForks$AjcClosure1 |
|
| 1.0;1 | ||||
RtForks$AjcClosure3 |
|
| 1.0;1 | ||||
RtForks$AjcClosure5 |
|
| 1.0;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 javax.json.JsonObject; | |
41 | import javax.json.JsonStructure; | |
42 | import lombok.EqualsAndHashCode; | |
43 | ||
44 | /** | |
45 | * Github forks. | |
46 | * | |
47 | * @author Carlos Miranda (miranda.cma@gmail.com) | |
48 | * @version $Id: 8a77d658208004861e35d4db7a161543f3dc6ae8 $ | |
49 | * @since 0.8 | |
50 | * @see <a href="http://developer.github.com/v3/repos/forks/">Forks API</a> | |
51 | */ | |
52 | @Immutable | |
53 | @Loggable(Loggable.DEBUG) | |
54 | 0 | @EqualsAndHashCode(of = {"request", "owner" }) |
55 | 0 | final class RtForks implements Forks { |
56 | ||
57 | /** | |
58 | * Fork's id name in JSON object. | |
59 | */ | |
60 | public static final String ID = "id"; | |
61 | ||
62 | /** | |
63 | * Restful Request. | |
64 | */ | |
65 | private final transient Request request; | |
66 | /** | |
67 | * Repository. | |
68 | */ | |
69 | private final transient Repo owner; | |
70 | ||
71 | /** | |
72 | * Public ctor. | |
73 | * @param req Request | |
74 | * @param repo Repository | |
75 | */ | |
76 | 2 | public RtForks(final Request req, final Repo repo) { |
77 | 2 | this.request = req.uri() |
78 | 2 | .path("/repos") |
79 | 2 | .path(repo.coordinates().user()) |
80 | 2 | .path(repo.coordinates().repo()) |
81 | 2 | .path("forks") |
82 | 2 | .back(); |
83 | 2 | this.owner = repo; |
84 | 2 | } |
85 | ||
86 | @Override | |
87 | public Repo repo() { | |
88 | 0 | return this.owner; |
89 | } | |
90 | ||
91 | @Override | |
92 | public Iterable<Fork> iterate( | |
93 | final String sort) { | |
94 | 3 | return new RtPagination<Fork>( |
95 | 1 | this.request.uri().queryParam("sort", sort).back(), |
96 | 1 | new RtValuePagination.Mapping<Fork, JsonObject>() { |
97 | @Override | |
98 | public Fork map(final JsonObject object) { | |
99 | 0 | return RtForks.this.get(object.getInt(ID)); |
100 | } | |
101 | } | |
102 | ); | |
103 | } | |
104 | ||
105 | @Override | |
106 | public Fork create( | |
107 | final String organization) throws IOException { | |
108 | 2 | final JsonStructure json = Json.createObjectBuilder() |
109 | 1 | .add("organization", organization) |
110 | 1 | .build(); |
111 | 2 | return this.get( |
112 | 1 | this.request.method(Request.POST) |
113 | 1 | .body().set(json).back() |
114 | 1 | .fetch().as(RestResponse.class) |
115 | 1 | .assertStatus(HttpURLConnection.HTTP_ACCEPTED) |
116 | 2 | .as(JsonResponse.class) |
117 | 1 | .json().readObject().getInt(ID) |
118 | ); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Get fork by number. | |
123 | * @param number Fork number | |
124 | * @return Fork | |
125 | */ | |
126 | private RtFork get(final Integer number) { | |
127 | 1 | return new RtFork(this.request, this.owner, number); |
128 | } | |
129 | } |