Skip to content

Instantly share code, notes, and snippets.

@oleg
Created February 11, 2012 14:36
Show Gist options
  • Select an option

  • Save oleg/1800015 to your computer and use it in GitHub Desktop.

Select an option

Save oleg/1800015 to your computer and use it in GitHub Desktop.
example of refactoring
package logic;
import org.junit.Test;
import play.test.UnitTest;
public class RssToPostTestV1 extends UnitTest {
public static final String USER_ID = "[email protected]";
public static final String URL =
"http://shpilenok.livejournal.com/data/rss";
@Test
public void testName() throws Exception {
RssToPostV1 poster = new RssToPostV1(URL, USER_ID);
int result = poster.make(2);
assertEquals(2, result);
}
}
package logic;
import models.Twit;
import models.User;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import play.libs.WS;
import play.libs.XPath;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
public class RssToPostV1 {
private final String url;
private String userId;
public RssToPostV1(String url, String userId) {
this.url = url;
this.userId = userId;
}
public int make(int limit) throws IOException, ParserConfigurationException, SAXException {
List<Node> nodes = XPath.selectNodes("//item", WS.url(url).get().getXml());
int i;
for (i = 0; i < limit && i < nodes.size(); i++) {
Node node = nodes.get(i);
String title = XPath.selectNode("title", node).getTextContent();
Twit first = Twit.find("message like ? and user.id = ?", title + "%", userId).first();
if (first == null) {
String message;
if (StringUtils.isNotBlank(title)) {
message = "Новая запись в журнале: " + title;
} else {
String description = XPath.selectNode("description", node).getTextContent();
message = "Новая запись в журнале: " + description;
}
Twit twit = new Twit();
twit.message = message;
twit.user = User.findById(userId);
twit.save();
}
}
return i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment