Coverage Report - com.jcabi.github.RtUser
 
Classes in this File Line Coverage Branch Coverage Complexity
RtUser
87%
29/33
27%
6/22
1.231
RtUser$AjcClosure1
100%
1/1
N/A
1.231
RtUser$AjcClosure11
100%
1/1
N/A
1.231
RtUser$AjcClosure13
0%
0/1
N/A
1.231
RtUser$AjcClosure15
100%
1/1
N/A
1.231
RtUser$AjcClosure17
100%
1/1
N/A
1.231
RtUser$AjcClosure3
100%
1/1
N/A
1.231
RtUser$AjcClosure5
100%
1/1
N/A
1.231
RtUser$AjcClosure7
0%
0/1
N/A
1.231
RtUser$AjcClosure9
100%
1/1
N/A
1.231
 
 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.aspects.Loggable;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.response.JsonResponse;
 36  
 import java.io.IOException;
 37  
 import java.util.Date;
 38  
 import java.util.LinkedList;
 39  
 import java.util.List;
 40  
 import javax.json.JsonArray;
 41  
 import javax.json.JsonObject;
 42  
 import javax.json.JsonValue;
 43  
 import javax.validation.constraints.NotNull;
 44  
 import lombok.EqualsAndHashCode;
 45  
 
 46  
 /**
 47  
  * Github user.
 48  
  *
 49  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 50  
  * @version $Id$
 51  
  * @since 0.1
 52  
  * @todo #913:30min Implement operations RtUser.markAsRead().
 53  
  */
 54  
 @Immutable
 55  
 @Loggable(Loggable.DEBUG)
 56  0
 @EqualsAndHashCode(of = { "ghub", "request" })
 57  
 @SuppressWarnings("PMD.TooManyMethods")
 58  
 final class RtUser implements User {
 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  
         @NotNull(message = "github can't be NULL") final Github github,
 82  
         @NotNull(message = "req can't be NULL") final Request req
 83  
     ) {
 84  5
         this(github, req, "");
 85  5
     }
 86  
 
 87  
     /**
 88  
      * Public ctor.
 89  
      * @param github Github
 90  
      * @param req Request
 91  
      * @param login User identity/identity
 92  
      */
 93  
     RtUser(
 94  
         @NotNull(message = "github can't be NULL") final Github github,
 95  
         @NotNull(message = "req can't be NULL") final Request req,
 96  
         @NotNull(message = "login can't be NULL") final String login
 97  37
     ) {
 98  37
         this.ghub = github;
 99  37
         if (login.isEmpty()) {
 100  6
             this.request = req.uri().path("/user").back();
 101  
         } else {
 102  31
             this.request = req.uri().path("/users").path(login).back();
 103  
         }
 104  37
         this.self = login;
 105  37
     }
 106  
 
 107  
     @Override
 108  
     @NotNull(message = "toString is never NULL")
 109  
     public String toString() {
 110  0
         return this.request.uri().get().toString();
 111  
     }
 112  
 
 113  
     @Override
 114  
     @NotNull(message = "github is never NULL")
 115  
     public Github github() {
 116  2
         return this.ghub;
 117  
     }
 118  
 
 119  
     @Override
 120  
     @NotNull(message = "login is never NULL")
 121  
     public String login() throws IOException {
 122  
         final String login;
 123  8
         if (this.self.isEmpty()) {
 124  2
             login = this.json().getString("login");
 125  
         } else {
 126  2
             login = this.self;
 127  
         }
 128  4
         return login;
 129  
     }
 130  
 
 131  
     @Override
 132  
     @NotNull(message = "organizations is never NULL")
 133  
     public Organizations organizations() {
 134  2
         return new RtOrganizations(this.ghub, this.ghub.entry(), this);
 135  
     }
 136  
 
 137  
     @Override
 138  
     @NotNull(message = "PublicKeys is never NULL")
 139  
     public PublicKeys keys() {
 140  0
         return new RtPublicKeys(this.ghub.entry(), this);
 141  
     }
 142  
 
 143  
     @Override
 144  
     @NotNull(message = "user emails is never NULL")
 145  
     public UserEmails emails() {
 146  2
         return new RtUserEmails(this.ghub.entry());
 147  
     }
 148  
 
 149  
     @Override
 150  
     public List<Notification> notifications() throws IOException {
 151  2
         final List<Notification> list =
 152  
             new LinkedList<Notification>();
 153  1
         final JsonResponse resp = this.github().entry().uri()
 154  
             .path("notifications")
 155  
             .back()
 156  
             .fetch()
 157  
             .as(JsonResponse.class);
 158  1
         final JsonArray array = resp.json().readArray();
 159  1
         for (final JsonValue value : array) {
 160  1
             final JsonObject notif = (JsonObject) value;
 161  1
             list.add(this.createNotification(notif));
 162  1
         }
 163  1
         return list;
 164  
     }
 165  
 
 166  
     @Override
 167  
     public void markAsRead(final Date lastread) {
 168  
         // Will be implemented later
 169  0
     }
 170  
 
 171  
     @Override
 172  
     @NotNull(message = "JSON is never NULL")
 173  
     public JsonObject json() throws IOException {
 174  54
         return new RtJson(this.request).fetch();
 175  
     }
 176  
 
 177  
     @Override
 178  
     public void patch(
 179  
         @NotNull(message = "JSON is never NULL") final JsonObject json)
 180  
         throws IOException {
 181  2
         new RtJson(this.request).patch(json);
 182  1
     }
 183  
 
 184  
     /**
 185  
      * Creates RtNotification object with the id from notifobj.
 186  
      * @param notifobj JSON object with notification data.
 187  
      * @return RtNotification object with the id from notifobj.
 188  
      */
 189  
     private Notification createNotification(final JsonObject notifobj) {
 190  1
         return new RtNotification(
 191  
             Long.parseLong(notifobj.getString("id"))
 192  
         );
 193  
     }
 194  
 }