Coverage Report - com.jcabi.github.RtSearch
 
Classes in this File Line Coverage Branch Coverage Complexity
RtSearch
83%
15/18
8%
1/12
1.9
RtSearch$1
100%
2/2
N/A
1.9
RtSearch$2
60%
3/5
N/A
1.9
RtSearch$3
100%
2/2
N/A
1.9
RtSearch$4
55%
5/9
N/A
1.9
RtSearch$AjcClosure1
100%
1/1
N/A
1.9
RtSearch$AjcClosure3
100%
1/1
N/A
1.9
RtSearch$AjcClosure5
100%
1/1
N/A
1.9
RtSearch$AjcClosure7
100%
1/1
N/A
1.9
RtSearch$AjcClosure9
100%
1/1
N/A
1.9
 
 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 java.io.IOException;
 36  
 import java.net.URI;
 37  
 import java.net.URISyntaxException;
 38  
 import java.util.EnumMap;
 39  
 import java.util.regex.Pattern;
 40  
 import javax.json.JsonObject;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import lombok.EqualsAndHashCode;
 43  
 
 44  
 /**
 45  
  * Github Search.
 46  
  *
 47  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 48  
  * @version $Id$
 49  
  * @since 0.8
 50  
  * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = "ghub")
 55  
 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
 56  9
 final class RtSearch implements Search {
 57  
 
 58  
     /**
 59  
      * Slash pattern for url splitting.
 60  
      */
 61  1
     private static final Pattern SLASH = Pattern.compile("/");
 62  
 
 63  
     /**
 64  
      * Equals pattern for query splitting.
 65  
      */
 66  1
     private static final Pattern QUERY = Pattern.compile("=");
 67  
 
 68  
     /**
 69  
      * Github.
 70  
      */
 71  
     private final transient Github ghub;
 72  
 
 73  
     /**
 74  
      * RESTful Request to search.
 75  
      */
 76  
     private final transient Request request;
 77  
 
 78  
     /**
 79  
      * Public ctor.
 80  
      * @param github Github
 81  
      * @param req RESTful API entry point
 82  
      */
 83  4
     RtSearch(final Github github, final Request req) {
 84  4
         this.ghub = github;
 85  4
         this.request = req.uri().path("/search").back();
 86  4
     }
 87  
 
 88  
     @Override
 89  
     @NotNull(message = "Github is never NULL")
 90  
     public Github github() {
 91  2
         return this.ghub;
 92  
     }
 93  
 
 94  
     @Override
 95  
     @NotNull(message = "Iterable of repos is never NULL")
 96  
     public Iterable<Repo> repos(
 97  
         @NotNull(message = "Search keywords can't be NULL")
 98  
         final String keywords,
 99  
         @NotNull(message = "Sort field can't be NULL") final String sort,
 100  
         @NotNull(message = "Sort order can't be NULL") final Order order)
 101  
         throws IOException {
 102  2
         return new RtSearchPagination<Repo>(
 103  
             this.request, "repositories", keywords, sort, order.identifier(),
 104  2
             new RtValuePagination.Mapping<Repo, JsonObject>() {
 105  
                 @Override
 106  
                 public Repo map(final JsonObject object) {
 107  1
                     return RtSearch.this.github().repos().get(
 108  
                         new Coordinates.Simple(object.getString("full_name"))
 109  
                     );
 110  
                 }
 111  
             }
 112  
         );
 113  
     }
 114  
 
 115  
     //@checkstyle ParameterNumberCheck (5 lines)
 116  
     @Override
 117  
     @NotNull(message = "Iterable of issues is never NULL")
 118  
     public Iterable<Issue> issues(final String keywords, final String sort,
 119  
         final Order order, final EnumMap<Qualifier, String> qualifiers)
 120  
         throws IOException {
 121  2
         final StringBuilder keyword = new StringBuilder(keywords);
 122  1
         for (final EnumMap.Entry<Qualifier, String> entry : qualifiers
 123  
             .entrySet()) {
 124  0
             keyword.append('+').append(entry.getKey().identifier())
 125  
                 .append(':').append(entry.getValue());
 126  0
         }
 127  1
         return new RtSearchPagination<Issue>(
 128  
             this.request,
 129  
             "issues",
 130  
             keyword.toString(),
 131  
             sort,
 132  
             order.identifier(),
 133  2
             new RtValuePagination.Mapping<Issue, JsonObject>() {
 134  
                 @Override
 135  
                 public Issue map(final JsonObject object) {
 136  
                     try {
 137  1
                         final String[] parts = RtSearch.SLASH.split(
 138  
                             // @checkstyle MultipleStringLiteralsCheck (1 line)
 139  
                             new URI(object.getString("url")).getPath()
 140  
                         );
 141  1
                         return RtSearch.this.ghub.repos().get(
 142  
                             // @checkstyle MagicNumber (1 line)
 143  
                             new Coordinates.Simple(parts[2], parts[3])
 144  
                         ).issues().get(object.getInt("number"));
 145  0
                     } catch (final URISyntaxException ex) {
 146  0
                         throw new IllegalStateException(ex);
 147  
                     }
 148  
                 }
 149  
             }
 150  
         );
 151  
     }
 152  
 
 153  
     @Override
 154  
     @NotNull(message = "Iterable of users is never NULL")
 155  
     public Iterable<User> users(
 156  
         @NotNull(message = "Search keywords can't be NULL")
 157  
         final String keywords,
 158  
         @NotNull(message = "Sort field can't be NULL") final String sort,
 159  
         @NotNull(message = "Sort order can't be NULL") final Order order)
 160  
         throws IOException {
 161  2
         return new RtSearchPagination<User>(
 162  
             this.request, "users", keywords, sort, order.identifier(),
 163  2
             new RtValuePagination.Mapping<User, JsonObject>() {
 164  
                 @Override
 165  
                 public User map(final JsonObject object) {
 166  1
                     return RtSearch.this.ghub.users().get(
 167  
                         object.getString("login")
 168  
                     );
 169  
                 }
 170  
             }
 171  
         );
 172  
     }
 173  
 
 174  
     @Override
 175  
     @NotNull(message = "Iterable of contents is never NULL")
 176  
     public Iterable<Content> codes(
 177  
         @NotNull(message = "Search keywords can't be NULL")
 178  
         final String keywords,
 179  
         @NotNull(message = "Sort field can't be NULL") final String sort,
 180  
         @NotNull(message = "Sort order can't be NULL") final Order order)
 181  
         throws IOException {
 182  2
         return new RtSearchPagination<Content>(
 183  
             this.request, "code", keywords, sort, order.identifier(),
 184  
             // @checkstyle AnonInnerLengthCheck (25 lines)
 185  3
             new RtValuePagination.Mapping<Content, JsonObject>() {
 186  
                 @Override
 187  
                 public Content map(final JsonObject object) {
 188  
                     try {
 189  
                         // @checkstyle MultipleStringLiteralsCheck (1 line)
 190  2
                         final URI uri = new URI(object.getString("url"));
 191  2
                         final String[] parts = RtSearch.SLASH.split(
 192  
                             uri.getPath()
 193  
                         );
 194  2
                         final String ref = RtSearch.QUERY.split(
 195  
                             uri.getQuery()
 196  
                         )[1];
 197  2
                         return RtSearch.this.ghub.repos().get(
 198  
                             // @checkstyle MagicNumber (1 line)
 199  
                             new Coordinates.Simple(parts[2], parts[3])
 200  
                         ).contents().get(object.getString("path"), ref);
 201  0
                     } catch (final URISyntaxException ex) {
 202  0
                         throw new IllegalStateException(ex);
 203  0
                     } catch (final IOException ex) {
 204  0
                         throw new IllegalStateException(ex);
 205  
                     }
 206  
                 }
 207  
             }
 208  
         );
 209  
     }
 210  
 
 211  
 }