Coverage Report - com.jcabi.github.RtUser
 
Classes in this File Line Coverage Branch Coverage Complexity
RtUser
89%
34/38
25%
7/28
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-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 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 lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github user.
 47  
  *
 48  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 49  
  * @version $Id: 12758e29705e9092652280cf3fd8ffcf2ae852db $
 50  
  * @since 0.1
 51  
  * @todo #913:30min Implement operations RtUser.markAsRead().
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @EqualsAndHashCode(of = { "ghub", "request" })
 56  
 @SuppressWarnings("PMD.TooManyMethods")
 57  
 final class RtUser implements User {
 58  
 
 59  
     /**
 60  
      * Github.
 61  
      */
 62  
     private final transient Github ghub;
 63  
 
 64  
     /**
 65  
      * RESTful request.
 66  
      */
 67  
     private final transient Request request;
 68  
 
 69  
     /**
 70  
      * Login of the user.
 71  
      */
 72  
     private final transient String self;
 73  
 
 74  
     /**
 75  
      * Public ctor.
 76  
      * @param github Github
 77  
      * @param req Request
 78  
      */
 79  
     RtUser(
 80  
         final Github github,
 81  
         final Request req
 82  
     ) {
 83  5
         this(github, req, "");
 84  5
     }
 85  
 
 86  
     /**
 87  
      * Public ctor.
 88  
      * @param github Github
 89  
      * @param req Request
 90  
      * @param login User identity/identity
 91  
      */
 92  
     RtUser(
 93  
         final Github github,
 94  
         final Request req,
 95  
         final String login
 96  38
     ) {
 97  38
         this.ghub = github;
 98  38
         if (login.isEmpty()) {
 99  6
             this.request = req.uri().path("/user").back();
 100  
         } else {
 101  32
             this.request = req.uri().path("/users").path(login).back();
 102  
         }
 103  38
         this.self = login;
 104  38
     }
 105  
 
 106  
     @Override
 107  
     public String toString() {
 108  0
         return this.request.uri().get().toString();
 109  
     }
 110  
 
 111  
     @Override
 112  
     public Github github() {
 113  2
         return this.ghub;
 114  
     }
 115  
 
 116  
     @Override
 117  
     public String login() throws IOException {
 118  
         final String login;
 119  8
         if (this.self.isEmpty()) {
 120  2
             login = this.json().getString("login");
 121  
         } else {
 122  2
             login = this.self;
 123  
         }
 124  4
         return login;
 125  
     }
 126  
 
 127  
     @Override
 128  
     public UserOrganizations organizations() {
 129  2
         return new RtUserOrganizations(this.ghub, this.ghub.entry(), this);
 130  
     }
 131  
 
 132  
     @Override
 133  
     public PublicKeys keys() {
 134  0
         return new RtPublicKeys(this.ghub.entry(), this);
 135  
     }
 136  
 
 137  
     @Override
 138  
     public UserEmails emails() {
 139  2
         return new RtUserEmails(this.ghub.entry());
 140  
     }
 141  
 
 142  
     @Override
 143  
     public List<Notification> notifications() throws IOException {
 144  2
         final List<Notification> list =
 145  
             new LinkedList<Notification>();
 146  1
         final JsonResponse resp = this.github().entry().uri()
 147  1
             .path("notifications")
 148  1
             .back()
 149  1
             .fetch()
 150  1
             .as(JsonResponse.class);
 151  1
         final JsonArray array = resp.json().readArray();
 152  1
         for (final JsonValue value : array) {
 153  1
             final JsonObject notif = (JsonObject) value;
 154  1
             list.add(this.createNotification(notif));
 155  1
         }
 156  1
         return list;
 157  
     }
 158  
 
 159  
     @Override
 160  
     public void markAsRead(final Date lastread) {
 161  
         // Will be implemented later
 162  0
     }
 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  
     /**
 177  
      * Creates RtNotification object with the id from notifobj.
 178  
      * @param notifobj JSON object with notification data.
 179  
      * @return RtNotification object with the id from notifobj.
 180  
      */
 181  
     private Notification createNotification(final JsonObject notifobj) {
 182  2
         return new RtNotification(
 183  1
             Long.parseLong(notifobj.getString("id"))
 184  
         );
 185  
     }
 186  
 }