Coverage Report - com.jcabi.github.RtUser
 
Classes in this File Line Coverage Branch Coverage Complexity
RtUser
91%
32/35
19%
5/26
1.167
RtUser$AjcClosure1
100%
1/1
N/A
1.167
RtUser$AjcClosure11
100%
1/1
N/A
1.167
RtUser$AjcClosure13
100%
1/1
N/A
1.167
RtUser$AjcClosure15
100%
1/1
N/A
1.167
RtUser$AjcClosure17
100%
1/1
N/A
1.167
RtUser$AjcClosure3
100%
1/1
N/A
1.167
RtUser$AjcClosure5
100%
1/1
N/A
1.167
RtUser$AjcClosure7
0%
0/1
N/A
1.167
RtUser$AjcClosure9
100%
1/1
N/A
1.167
 
 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.RestResponse;
 36  
 import java.io.IOException;
 37  
 import java.net.HttpURLConnection;
 38  
 import java.time.format.DateTimeFormatter;
 39  
 import java.util.Date;
 40  
 import javax.json.JsonObject;
 41  
 import lombok.EqualsAndHashCode;
 42  
 
 43  
 /**
 44  
  * Github user.
 45  
  *
 46  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 47  
  * @version $Id: 77b905e58aefe474bc50591c3b0bedf769a92207 $
 48  
  * @since 0.1
 49  
  */
 50  
 @Immutable
 51  
 @Loggable(Loggable.DEBUG)
 52  0
 @EqualsAndHashCode(of = { "ghub", "request" })
 53  
 @SuppressWarnings("PMD.TooManyMethods")
 54  
 final class RtUser implements User {
 55  
     /**
 56  
      * Path for the notifications resource.
 57  
      */
 58  
     private static final String NOTIF_PATH = "notifications";
 59  
 
 60  
     /**
 61  
      * Github.
 62  
      */
 63  
     private final transient Github ghub;
 64  
 
 65  
     /**
 66  
      * RESTful request.
 67  
      */
 68  
     private final transient Request request;
 69  
 
 70  
     /**
 71  
      * Login of the user.
 72  
      */
 73  
     private final transient String self;
 74  
 
 75  
     /**
 76  
      * Public ctor.
 77  
      * @param github Github
 78  
      * @param req Request
 79  
      */
 80  
     RtUser(
 81  
         final Github github,
 82  
         final Request req
 83  
     ) {
 84  7
         this(github, req, "");
 85  7
     }
 86  
 
 87  
     /**
 88  
      * Public ctor.
 89  
      * @param github Github
 90  
      * @param req Request
 91  
      * @param login User identity/identity
 92  
      */
 93  
     RtUser(
 94  
         final Github github,
 95  
         final Request req,
 96  
         final String login
 97  38
     ) {
 98  40
         this.ghub = github;
 99  40
         if (login.isEmpty()) {
 100  8
             this.request = req.uri().path("/user").back();
 101  
         } else {
 102  32
             this.request = req.uri().path("/users").path(login).back();
 103  
         }
 104  40
         this.self = login;
 105  40
     }
 106  
 
 107  
     @Override
 108  
     public String toString() {
 109  0
         return this.request.uri().get().toString();
 110  
     }
 111  
 
 112  
     @Override
 113  
     public Github github() {
 114  6
         return this.ghub;
 115  
     }
 116  
 
 117  
     @Override
 118  
     public String login() throws IOException {
 119  
         final String login;
 120  8
         if (this.self.isEmpty()) {
 121  2
             login = this.json().getString("login");
 122  
         } else {
 123  2
             login = this.self;
 124  
         }
 125  4
         return login;
 126  
     }
 127  
 
 128  
     @Override
 129  
     public UserOrganizations organizations() {
 130  2
         return new RtUserOrganizations(this.ghub, this.ghub.entry(), this);
 131  
     }
 132  
 
 133  
     @Override
 134  
     public PublicKeys keys() {
 135  0
         return new RtPublicKeys(this.ghub.entry(), this);
 136  
     }
 137  
 
 138  
     @Override
 139  
     public UserEmails emails() {
 140  2
         return new RtUserEmails(this.ghub.entry());
 141  
     }
 142  
 
 143  
     @Override
 144  
     public Notifications notifications() throws IOException {
 145  3
         return new RtNotifications(
 146  1
             this.github().entry().uri().path(RtUser.NOTIF_PATH).back()
 147  
         );
 148  
     }
 149  
 
 150  
     @Override
 151  
     public void markAsRead(final Date lastread) throws IOException {
 152  4
         this.github().entry().uri()
 153  2
             .path(RtUser.NOTIF_PATH)
 154  2
             .queryParam(
 155  
                 "last_read_at",
 156  2
                 DateTimeFormatter.ISO_INSTANT.format(lastread.toInstant())
 157  2
             ).back()
 158  2
             .method(Request.PUT)
 159  2
             .fetch()
 160  2
             .as(RestResponse.class)
 161  2
             .assertStatus(HttpURLConnection.HTTP_RESET);
 162  1
     }
 163  
 
 164  
     @Override
 165  
     public JsonObject json() throws IOException {
 166  54
         return new RtJson(this.request).fetch();
 167  
     }
 168  
 
 169  
     @Override
 170  
     public void patch(
 171  
         final JsonObject json)
 172  
         throws IOException {
 173  2
         new RtJson(this.request).patch(json);
 174  1
     }
 175  
 }
 176