Skip to content

Commit 4b88119

Browse files
committed
Funciones de transformacion y ejecucion final
1 parent 916c1e1 commit 4b88119

6 files changed

Lines changed: 285 additions & 0 deletions

File tree

jobs-search-reporter/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sourceCompatibility = 1.8
1414
//Nuestra clase principal.
1515
application {
1616
mainClassName = "com.platzi.jobsearch.JobSearch"
17+
applicationName = "job-search"
1718
}
1819

1920
repositories {

jobs-search-reporter/src/main/java/com/platzi/jobsearch/CommanderFunctions.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.platzi.jobsearch;
22

33
import com.beust.jcommander.JCommander;
4+
import com.beust.jcommander.ParameterException;
45

6+
import java.util.List;
7+
import java.util.Optional;
58
import java.util.function.Supplier;
69

710
public interface CommanderFunctions {
@@ -34,4 +37,30 @@ static <T> JCommander buildCommanderWithName(String name, Supplier<T> argumentsS
3437
jCommander.setProgramName(name);
3538
return jCommander;
3639
}
40+
41+
/**
42+
* Funcion utilizada para tomar los datos de JCommander, los argumentos esperados y en caso de que algo falle,
43+
* una funcion con el JCommander que genero el error.
44+
*/
45+
static Optional<List<Object>> parseArguments(
46+
JCommander jCommander,
47+
String[] arguments,
48+
OnCommandError onCommandError
49+
) {
50+
List<Object> result;
51+
try {
52+
jCommander.parse(arguments);
53+
54+
return Optional.of(jCommander.getObjects());
55+
} catch (ParameterException exception) {
56+
onCommandError.onError(jCommander);
57+
}
58+
59+
return Optional.empty();
60+
}
61+
62+
@FunctionalInterface
63+
interface OnCommandError {
64+
void onError(JCommander jCommander);
65+
}
3766
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package com.platzi.jobsearch;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
5+
import java.util.Objects;
6+
7+
/**
8+
* Clase que representa los resultados de una busqueda
9+
*/
10+
public final class JobPosition {
11+
private String id;
12+
13+
private String type;
14+
15+
private String ulr;
16+
17+
@SerializedName("created_at")
18+
private String createdAt;
19+
20+
private String company;
21+
22+
@SerializedName("company_url")
23+
private String companyUrl;
24+
25+
private String location;
26+
27+
private String title;
28+
29+
private String description;
30+
31+
@SerializedName("how_to_apply")
32+
private String howToApply;
33+
34+
@SerializedName("company_logo")
35+
private String companyLogo;
36+
37+
public String getId() {
38+
return id;
39+
}
40+
41+
public void setId(String id) {
42+
this.id = id;
43+
}
44+
45+
public String getType() {
46+
return type;
47+
}
48+
49+
public void setType(String type) {
50+
this.type = type;
51+
}
52+
53+
public String getUlr() {
54+
return ulr;
55+
}
56+
57+
public void setUlr(String ulr) {
58+
this.ulr = ulr;
59+
}
60+
61+
public String getCreatedAt() {
62+
return createdAt;
63+
}
64+
65+
public void setCreatedAt(String createdAt) {
66+
this.createdAt = createdAt;
67+
}
68+
69+
public String getCompany() {
70+
return company;
71+
}
72+
73+
public void setCompany(String company) {
74+
this.company = company;
75+
}
76+
77+
public String getCompanyUrl() {
78+
return companyUrl;
79+
}
80+
81+
public void setCompanyUrl(String companyUrl) {
82+
this.companyUrl = companyUrl;
83+
}
84+
85+
public String getLocation() {
86+
return location;
87+
}
88+
89+
public void setLocation(String location) {
90+
this.location = location;
91+
}
92+
93+
public String getTitle() {
94+
return title;
95+
}
96+
97+
public void setTitle(String title) {
98+
this.title = title;
99+
}
100+
101+
public String getDescription() {
102+
return description;
103+
}
104+
105+
public void setDescription(String description) {
106+
this.description = description;
107+
}
108+
109+
public String getHowToApply() {
110+
return howToApply;
111+
}
112+
113+
public void setHowToApply(String howToApply) {
114+
this.howToApply = howToApply;
115+
}
116+
117+
public String getCompanyLogo() {
118+
return companyLogo;
119+
}
120+
121+
public void setCompanyLogo(String companyLogo) {
122+
this.companyLogo = companyLogo;
123+
}
124+
125+
@Override
126+
public boolean equals(Object o) {
127+
if (this == o) return true;
128+
if (o == null || getClass() != o.getClass()) return false;
129+
JobPosition that = (JobPosition) o;
130+
return getId().equals(that.getId()) &&
131+
getType().equals(that.getType()) &&
132+
getUlr().equals(that.getUlr()) &&
133+
getCreatedAt().equals(that.getCreatedAt()) &&
134+
getCompany().equals(that.getCompany()) &&
135+
Objects.equals(getCompanyUrl(), that.getCompanyUrl()) &&
136+
Objects.equals(getLocation(), that.getLocation()) &&
137+
getTitle().equals(that.getTitle()) &&
138+
Objects.equals(getDescription(), that.getDescription()) &&
139+
Objects.equals(getHowToApply(), that.getHowToApply()) &&
140+
Objects.equals(getCompanyLogo(), that.getCompanyLogo());
141+
}
142+
143+
@Override
144+
public int hashCode() {
145+
return Objects.hash(getId(), getType(), getUlr(), getCreatedAt(), getCompany(), getCompanyUrl(), getLocation(), getTitle(), getDescription(), getHowToApply(), getCompanyLogo());
146+
}
147+
148+
@Override
149+
public String toString() {
150+
return "JobPosition{" +
151+
"id='" + id + '\'' +
152+
", type='" + type + '\'' +
153+
", ulr='" + ulr + '\'' +
154+
", createdAt='" + createdAt + '\'' +
155+
", company='" + company + '\'' +
156+
", companyUrl='" + companyUrl + '\'' +
157+
", location='" + location + '\'' +
158+
", title='" + title + '\'' +
159+
", description='" + description + '\'' +
160+
", howToApply='" + howToApply + '\'' +
161+
", companyLogo='" + companyLogo + '\'' +
162+
'}';
163+
}
164+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,58 @@
11
package com.platzi.jobsearch;
22

33
import com.beust.jcommander.JCommander;
4+
import com.platzi.jobsearch.api.JobsAPI;
45
import com.platzi.jobsearch.cli.CLIArguments;
6+
import com.platzi.jobsearch.cli.CLIFunctions;
7+
8+
import java.util.Collection;
9+
import java.util.Collections;
10+
import java.util.Map;
11+
import java.util.Optional;
12+
import java.util.stream.Stream;
513

614
import static com.platzi.jobsearch.CommanderFunctions.buildCommanderWithName;
15+
import static com.platzi.jobsearch.CommanderFunctions.parseArguments;
16+
import static com.platzi.jobsearch.api.APIFunctions.buildAPI;
717

818
public class JobSearch {
919
public static void main(String[] args) {
1020
//Creacion de nuestro CLI con JCommander
1121
JCommander jCommander = buildCommanderWithName("job-search", CLIArguments::newInstance);
22+
23+
//Obtenemos las opciones que se le dieron a JCommander
24+
Stream<CLIArguments> streamOfCLI =
25+
//Nos retorna un Optional<List<Object>>
26+
parseArguments(jCommander, args, JCommander::usage)
27+
//En caso de un Optional.empty()
28+
.orElse(Collections.emptyList())
29+
.stream()
30+
.map(obj -> (CLIArguments) obj);
31+
32+
//Tomamos nuestro Stream y obtenemos las opciones que se dieron en el CLI
33+
Optional<CLIArguments> cliOptional = streamOfCLI
34+
//Solo nos interesan los casos donde no sea la solicitud de ayuda
35+
.filter(cli -> !cli.isHelp())
36+
//Y que contengan un keyword, en otros caso no tenemos que buscar
37+
.filter(cli -> cli.getKeyword() != null)
38+
.findFirst();
39+
40+
//Si el Optional tiene un dato, lo convertimos a Map<String,Object>
41+
cliOptional.map(CLIFunctions::toMap)
42+
//Convertimos el Map en un request
43+
.map(JobSearch::executeRequest)
44+
//Aun seguimos operando sobre un Optional… en caso de que no hubiera datos
45+
//Generamos un stream vacio
46+
.orElse(Stream.empty())
47+
//Imprimos los datos por pantalla.
48+
.forEach(System.out::println);
49+
}
50+
51+
private static Stream<JobPosition> executeRequest(Map<String, Object> options) {
52+
JobsAPI api = buildAPI(JobsAPI.class, "https://jobs.github.com");
53+
54+
return Stream.of(options)
55+
.map(api::jobs)
56+
.flatMap(Collection::stream);
1257
}
1358
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.platzi.jobsearch.api;
2+
3+
import com.platzi.jobsearch.JobPosition;
4+
import feign.Headers;
5+
import feign.Param;
6+
import feign.QueryMap;
7+
import feign.RequestLine;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
/**
13+
* Esta interfaz sera usada por Feign para hacer las peticiones a la API de github.
14+
*/
15+
@Headers("Accept: application/json")
16+
public interface JobsAPI {
17+
@RequestLine("GET /positions.json")
18+
List<JobPosition> jobs(@QueryMap Map<String, Object> queryMap);
19+
20+
@RequestLine("GET /positions/{id}.json")
21+
JobPosition job(@Param("id") String id);
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.platzi.jobsearch.cli;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public interface CLIFunctions {
7+
/**
8+
* Funcion que tomara los argumentos del CLI y los convertira en algo que se pueda
9+
* usar en la API de github.
10+
*/
11+
static Map<String, Object> toMap(CLIArguments cliArguments) {
12+
Map<String, Object> params = new HashMap<>();
13+
params.put("description", cliArguments.getKeyword());
14+
params.put("location", cliArguments.getLocation());
15+
params.put("full_time", cliArguments.isFullTime());
16+
params.put("page", cliArguments.getPage());
17+
18+
if (cliArguments.isMarkdown()) {
19+
params.put("markdown", true);
20+
}
21+
22+
return params;
23+
}
24+
}

0 commit comments

Comments
 (0)