Skip to content

Instantly share code, notes, and snippets.

@mishako
Created July 26, 2017 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishako/e24f399325c27d407f3bef88afe03117 to your computer and use it in GitHub Desktop.
Save mishako/e24f399325c27d407f3bef88afe03117 to your computer and use it in GitHub Desktop.
Rome 2 API Ideas
String data = "<rss><channel><title>test</title>"
+ "<pubDate>Mon, 24 Jul 2017 00:00:00 +0000</pubDate></channel></rss>"
String title = Rome.minimal().read(data).title().toString();
// Minimal parser
RomeEngine rome = Rome.minimal();
Feed feed = rome.read(data);
StringValue title = feed.title();
String value = title.toString();
boolean isPresent = title.isPresent();
Optional<String> maybeTitle = title.asOptional();
// Custom parser
RomeEngine rome = Rome.custom()
.enable(Features.SAVE_RAW)
.enable(Features.SAVE_XML)
.build();
DateTimeValue published = rome.read(data).published();
DateTime value = published.get();
String raw = published.raw(); // "Mon, 24 Jul 2017 00:00:00 +0000" (would throw if the SAVE_RAW feature wasn't enabled)
Node node = published.container(); // returns <pubDate> node (org.w3c.dom.Node)
// Writing
Feed feed = Feed.builder().title("test").build();
String xml = Rome.minimal().writeString(feed);
Rome.minimal().write(feed, outputStream); // write to an OutputStream
// Write as json feed
Rome.custom()
.set(WriterConfig.FORMAT, Format.JSON_FEED)
.build()
.writeString(feed);
// Convert to Rome v1
SyndFeed feed = Rome.minimal().read(data).toLegacy(); // @Deprecated from day one
// Specific models
Feed feed = Rome.minimal().read(data);
Itunes.Feed itunes = feed.as(Models.ITUNES);
Optional<Itunes.Type> type = itunes.type().asOptional();
// Fetching
Feed feed = Rome.minimal().readUrl("https://..."); // works only if either apache or jaxrs client is on classpath
// Errors
RomeEngine rome = Rome.custom().enable(Features.SAVE_ERRORS).build();
Feed feed = rome.read("<rss><channel><pubDate>not-a-date</pubDate></channel></rss>");
List<Error> errors = feed.errors();
String message = errors.get(0).message(); // "Invalid date"
String value = errors.get(0).value(); // "not-a-date"
feed.published().errors().get(0).message(); // "Invalid date"
// Plugins
RomeEngine rome = Rome.custom()
.register(YahooWeather.class)
.build();
YahooWeather.Feed feed = rome.read(data).as(YahooWeather.MODEL);
Wind wind = feed.wind();
IntValue direction = wind.direction();
int directionInt = direction.value();
String directionString = direction.toString();
direction.asOptional().ifPresent(System.out::println);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment