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