Skip to content

Instantly share code, notes, and snippets.

@naturalwarren
Last active June 29, 2018 05:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naturalwarren/fc12ebcb86eb060789a56a709ae5d65f to your computer and use it in GitHub Desktop.
Save naturalwarren/fc12ebcb86eb060789a56a709ae5d65f to your computer and use it in GitHub Desktop.
Model of Rider
/**
* A converter factory that uses Rave to validate responses from the network. In the event that a response doesn't
* validate an exception is thrown to consumers.
*
* NOTE: This is a forwarding converter that delegates to another converter that should convert Java Objects
* into JSON and back. This converter must registered to your Retrofit instance before any other converter it might
* delegate to.
*/
final class RaveConverterFactory extends Converter.Factory {
static RaveConverterFactory create() {
return new RaveConverterFactory();
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(
Type type, Annotation[] annotations, Retrofit retrofit) {
Converter<ResponseBody, ?> delegateConverter = retrofit.nextResponseBodyConverter(this, type, annotations);
return new RaveResponseConverter(delegateConverter);
}
/**
* A converter that validates responses received from the network using rave.
*/
private static final class RaveResponseConverter implements Converter<ResponseBody, Object> {
private final Converter<ResponseBody, ?> delegateConverter;
RaveResponseConverter(Converter<ResponseBody, ?> delegateConverter) {
this.delegateConverter = delegateConverter;
}
@Override
public Object convert(ResponseBody value) throws IOException {
Object convert = delegateConverter.convert(value);
try {
Rave.getInstance().validate(convert);
} catch (RaveException e) {
// This response didn't pass RAVE validation, throw an exception.
throw new RuntimeException(e);
}
return convert;
}
}
}
public class Ride {
Url getUrl() {
return this.url;
}
@MustBeTrue
boolean hasValidUrl() {
Uri url = getUrl();
if (url != null) {
return URLUtil.isValidUrl(url.toString());
} else {
return true;
}
}
}
public class Rider {
private String firstName;
private String lastName;
private String phoneNumber;
public Rider(String firstName, String lastName, String phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
public String firstName() {
return firstName;
}
public String lastName() {
return lastName;
}
public String phoneNumber() {
return phoneNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment