Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RtStars |
|
| 1.0;1 | ||||
RtStars$AjcClosure1 |
|
| 1.0;1 | ||||
RtStars$AjcClosure3 |
|
| 1.0;1 | ||||
RtStars$AjcClosure5 |
|
| 1.0;1 | ||||
RtStars$AjcClosure7 |
|
| 1.0;1 |
1 | 4 | /** |
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.IOException; | |
37 | import java.net.HttpURLConnection; | |
38 | import lombok.EqualsAndHashCode; | |
39 | import org.hamcrest.Matchers; | |
40 | ||
41 | /** | |
42 | * Github starring API. | |
43 | * | |
44 | * @author Paul Polishchuk (ppol@ua.fm) | |
45 | * @version $Id: bcdcdc3e0273726f8a52d390c2d58a72e4c95dd1 $ | |
46 | * @since 0.15 | |
47 | * @see <a href="https://developer.github.com/v3/activity/starring/">Starring API</a> | |
48 | */ | |
49 | @Immutable | |
50 | @Loggable(Loggable.DEBUG) | |
51 | 0 | @EqualsAndHashCode(of = { "owner", "request" }) |
52 | final class RtStars implements Stars { | |
53 | ||
54 | /** | |
55 | * RESTful request. | |
56 | */ | |
57 | private final transient Request request; | |
58 | ||
59 | /** | |
60 | * Repository. | |
61 | */ | |
62 | private final transient Repo owner; | |
63 | ||
64 | /** | |
65 | * Public ctor. | |
66 | * @param req Request | |
67 | * @param repo Repository | |
68 | */ | |
69 | 5 | RtStars(final Request req, final Repo repo) { |
70 | 5 | final Coordinates coords = repo.coordinates(); |
71 | 5 | this.request = req.uri() |
72 | 5 | .path("/user/starred") |
73 | 5 | .path(coords.user()) |
74 | 5 | .path(coords.repo()) |
75 | 5 | .back(); |
76 | 5 | this.owner = repo; |
77 | 5 | } |
78 | ||
79 | @Override | |
80 | public Repo repo() { | |
81 | 0 | return this.owner; |
82 | } | |
83 | ||
84 | @Override | |
85 | public boolean starred() throws IOException { | |
86 | 6 | return this.request |
87 | 2 | .fetch().as(RestResponse.class) |
88 | 2 | .assertStatus( |
89 | 2 | Matchers.isOneOf( |
90 | 2 | HttpURLConnection.HTTP_NO_CONTENT, |
91 | 2 | HttpURLConnection.HTTP_NOT_FOUND |
92 | ) | |
93 | 2 | ).status() == HttpURLConnection.HTTP_NO_CONTENT; |
94 | } | |
95 | ||
96 | @Override | |
97 | public void star() throws IOException { | |
98 | 2 | this.request |
99 | 1 | .method(Request.PUT) |
100 | 1 | .fetch().as(RestResponse.class) |
101 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
102 | 1 | } |
103 | ||
104 | @Override | |
105 | public void unstar() throws IOException { | |
106 | 2 | this.request |
107 | 1 | .method(Request.DELETE) |
108 | 1 | .fetch().as(RestResponse.class) |
109 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
110 | 1 | } |
111 | } |