| 1 | 2 | |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 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 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 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 | |
|
| 59 | |
|
| 60 | 1 | private static final Pattern SLASH = Pattern.compile("/"); |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | 1 | private static final Pattern QUERY = Pattern.compile("="); |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
private final transient Github ghub; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
private final transient Request request; |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 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 | |
|
| 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 | |
|
| 132 | 1 | new URI(object.getString("url")).getPath() |
| 133 | |
); |
| 134 | 2 | return RtSearch.this.ghub.repos().get( |
| 135 | |
|
| 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 | |
|
| 172 | 3 | new RtValuePagination.Mapping<Content, JsonObject>() { |
| 173 | |
@Override |
| 174 | |
public Content map(final JsonObject object) { |
| 175 | |
try { |
| 176 | |
|
| 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 | |
|
| 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 | |
} |