Coverage Report - com.jcabi.github.RtUserEmails
 
Classes in this File Line Coverage Branch Coverage Complexity
RtUserEmails
92%
39/42
40%
8/20
1.667
RtUserEmails$AjcClosure1
100%
1/1
N/A
1.667
RtUserEmails$AjcClosure3
100%
1/1
N/A
1.667
RtUserEmails$AjcClosure5
100%
1/1
N/A
1.667
RtUserEmails$AjcClosure7
0%
0/1
N/A
1.667
 
 1  0
 /**
 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 java.util.ArrayList;
 40  
 import java.util.Collection;
 41  
 import java.util.List;
 42  
 import javax.json.Json;
 43  
 import javax.json.JsonArrayBuilder;
 44  
 import javax.json.JsonObject;
 45  
 import lombok.EqualsAndHashCode;
 46  
 
 47  
 /**
 48  
  * Github user's emails.
 49  
  * @author Alexander Sinyagin (sinyagin.alexander@gmail.com)
 50  
  * @version $Id: c01ee0044df6c0230202b8ffb31cff55ff471ea2 $
 51  
  * @since 0.8
 52  
  */
 53  
 @Immutable
 54  
 @Loggable(Loggable.DEBUG)
 55  0
 @EqualsAndHashCode(of = "request")
 56  
 final class RtUserEmails implements UserEmails {
 57  
 
 58  
     /**
 59  
      * RESTful API request for the emails.
 60  
      */
 61  
     private final transient Request request;
 62  
 
 63  
     /**
 64  
      * Ctor.
 65  
      * @param req RESTful API entry point
 66  
      */
 67  4
     RtUserEmails(final Request req) {
 68  4
         this.request = req.header("Accept", "application/vnd.github.v3")
 69  4
             .uri().path("/user/emails").back();
 70  4
     }
 71  
 
 72  
     @Override
 73  
     public Iterable<String> iterate() throws IOException {
 74  2
         final List<JsonObject> array = this.request.method(Request.GET)
 75  1
             .fetch().as(RestResponse.class)
 76  1
             .assertStatus(HttpURLConnection.HTTP_OK)
 77  1
             .as(JsonResponse.class)
 78  1
             .json().readArray().getValuesAs(JsonObject.class);
 79  1
         final Collection<String> emails = new ArrayList<String>(array.size());
 80  1
         for (final JsonObject obj : array) {
 81  
             // @checkstyle MultipleStringLiterals (1 line)
 82  1
             emails.add(obj.getString("email"));
 83  1
         }
 84  1
         return emails;
 85  
     }
 86  
 
 87  
     @Override
 88  
     public Iterable<String> add(
 89  
         final Iterable<String> emails
 90  
     ) throws IOException {
 91  2
         final JsonArrayBuilder json = Json.createArrayBuilder();
 92  1
         for (final String email : emails) {
 93  1
             json.add(email);
 94  1
         }
 95  1
         final List<JsonObject> array = this.request.method(Request.POST)
 96  1
             .body().set(json.build()).back()
 97  1
             .fetch().as(RestResponse.class)
 98  1
             .assertStatus(HttpURLConnection.HTTP_CREATED)
 99  1
             .as(JsonResponse.class)
 100  1
             .json().readArray().getValuesAs(JsonObject.class);
 101  1
         final Collection<String> result = new ArrayList<String>(array.size());
 102  1
         for (final JsonObject obj : array) {
 103  1
             result.add(obj.getString("email"));
 104  1
         }
 105  1
         return result;
 106  
     }
 107  
 
 108  
     @Override
 109  
     public void remove(final Iterable<String> emails) throws IOException {
 110  2
         final JsonArrayBuilder json = Json.createArrayBuilder();
 111  1
         for (final String email : emails) {
 112  1
             json.add(email);
 113  1
         }
 114  1
         this.request.method(Request.DELETE)
 115  1
             .body().set(json.build()).back()
 116  1
             .fetch().as(RestResponse.class)
 117  1
             .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
 118  1
     }
 119  
 
 120  
     @Override
 121  
     public String toString() {
 122  0
         return this.request.uri().get().toString();
 123  
     }
 124  
 
 125  
     @Override
 126  
     public JsonObject json() throws IOException {
 127  0
         return new RtJson(this.request).fetch();
 128  
     }
 129  
 
 130  
 }