Skip to content

Commit

Permalink
TFJ-721 add TrendsResources#getPlaceTrends()
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuke committed Nov 28, 2012
1 parent 3c030f6 commit e53303f
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 4 deletions.
31 changes: 29 additions & 2 deletions twitter4j-async/src/main/java/twitter4j/AsyncTwitterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
* Currently this class is not carefully designed to be extended. It is suggested to extend this class only for mock testing purporse.<br>
*
* @author Yusuke Yamamoto - yusuke at mac.com
* @see twitter4j.AsyncTwitter
* @see twitter4j.TwitterListener
* @see {@link twitter4j.AsyncTwitter}
* @see {@link twitter4j.TwitterListener}
*/
class AsyncTwitterImpl extends TwitterBaseImpl implements AsyncTwitter {
private static final long serialVersionUID = -2008667933225051907L;
Expand Down Expand Up @@ -2611,6 +2611,33 @@ public void invoke(List<TwitterListener> listeners) throws TwitterException {
}

/* Trends Resources */

/**
* {@inheritDoc}
*/
@Override
public void getLocationTrends(int woeid) {
getPlaceTrends(woeid);
}

/**
* {@inheritDoc}
*/
@Override
public void getPlaceTrends(final int woeid) {
getDispatcher().invokeLater(new AsyncTask(PLACE_TRENDS, listeners) {
public void invoke(List<TwitterListener> listeners) throws TwitterException {
Trends trends = twitter.getPlaceTrends(woeid);
for (TwitterListener listener : listeners) {
try {
listener.gotPlaceTrends(trends);
} catch (Exception ignore) {
}
}
}
});
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void gotSimilarPlaces(SimilarPlaces places){}
public void createdPlace(Place place){}

/* Trends Resources */
public void gotPlaceTrends(Trends trends) {}
public void gotAvailableTrends(ResponseList<Location> locations){}

/* Spam Reporting Resources */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public interface TwitterListener {
void createdPlace(Place place);

/* Trends Resources */
void gotPlaceTrends(Trends trends);
void gotAvailableTrends(ResponseList<Location> locations);

/* Spam Reporting Resources */
Expand Down
1 change: 1 addition & 0 deletions twitter4j-async/src/main/java/twitter4j/TwitterMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public enum TwitterMethod {
CREATE_PLACE,

/* Trends Resources */
PLACE_TRENDS,
AVAILABLE_TRENDS,

/* Spam Reporting Resources */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,36 @@

package twitter4j.api;

import twitter4j.GeoLocation;

/**
* @author Yusuke Yamamoto - yusuke at mac.com
* @since Twitter4J 2.1.3
*/
public interface TrendsResourcesAsync {
/**
* Returns the top 10 trending topics for a specific WOEID, if trending information is available for it.<br>
* The response is an array of "trend" objects that encode the name of the trending topic, the query parameter that can be used to search for the topic on <a href="http://search.twitter.com/">Twitter Search</a>, and the Twitter Search URL.<br>
* This information is cached for 5 minutes. Requesting more frequently than that will not return any more data, and will count against your rate limit usage.<br>
* <br>This method calls http://api.twitter.com/1.1/trends/place.json
*
* @param woeid <a href="http://developer.yahoo.com/geo/geoplanet/">The Yahoo! Where On Earth ID</a> of the location to return trending information for. Global information is available by using 1 as the WOEID.
* @see <a href="https://dev.twitter.com/docs/api/1.1/get/trends/place">GET trends/place | Twitter Developers</a>
* @since Twitter4J 2.1.1
* @deprecated use {@link #getPlaceTrends(int)} instead
*/
void getLocationTrends(int woeid);

/**
* Returns the top 10 trending topics for a specific WOEID, if trending information is available for it.<br>
* The response is an array of "trend" objects that encode the name of the trending topic, the query parameter that can be used to search for the topic on <a href="http://search.twitter.com/">Twitter Search</a>, and the Twitter Search URL.<br>
* This information is cached for 5 minutes. Requesting more frequently than that will not return any more data, and will count against your rate limit usage.<br>
* <br>This method calls http://api.twitter.com/1.1/trends/place.json
*
* @param woeid <a href="http://developer.yahoo.com/geo/geoplanet/">The Yahoo! Where On Earth ID</a> of the location to return trending information for. Global information is available by using 1 as the WOEID.
* @see <a href="https://dev.twitter.com/docs/api/1.1/get/trends/place">GET trends/place | Twitter Developers</a>
* @since Twitter4J 3.0.2
*/
void getPlaceTrends(int woeid);

/**
* Retrieves the locations that Twitter has trending topic information for. The response is an array of &quot;locations&quot; that encode the location's WOEID (a <a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo! Where On Earth ID</a>) and some other human-readable information such as a canonical name and country the location belongs in.
* <br>This method calls http://api.twitter.com/1.1/trends/available.json
Expand Down
6 changes: 6 additions & 0 deletions twitter4j-async/src/test/java/twitter4j/AsyncTwitterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,12 @@ public void createdPlace(Place place) {
notifyResponse();
}

@Override
public void gotPlaceTrends(Trends trends) {
this.trends = trends;
notifyResponse();
}

/* Legal Resources */

/**
Expand Down
17 changes: 17 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/TwitterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,23 @@ public Place createPlace(String name, String containedWithin, String token, GeoL

/* Trends Resources */

/**
* {@inheritDoc}
*/
@Override
public Trends getLocationTrends(int woeid) throws TwitterException {
return getPlaceTrends(woeid);
}

/**
* {@inheritDoc}
*/
@Override
public Trends getPlaceTrends(int woeid) throws TwitterException {
return factory.createTrends(get(conf.getRestBaseURL()
+ "trends/place.json?id=" + woeid));
}

/**
* {@inheritDoc}
*/
Expand Down
29 changes: 29 additions & 0 deletions twitter4j-core/src/main/java/twitter4j/api/TrendsResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@
* @since Twitter4J 2.1.3
*/
public interface TrendsResources {
/**
* Returns the top 10 trending topics for a specific WOEID, if trending information is available for it.<br>
* The response is an array of "trend" objects that encode the name of the trending topic, the query parameter that can be used to search for the topic on <a href="http://search.twitter.com/">Twitter Search</a>, and the Twitter Search URL.<br>
* This information is cached for 5 minutes. Requesting more frequently than that will not return any more data, and will count against your rate limit usage.<br>
* <br>This method calls http://api.twitter.com/1.1/trends/place.json
*
* @param woeid <a href="http://developer.yahoo.com/geo/geoplanet/">The Yahoo! Where On Earth ID</a> of the location to return trending information for. Global information is available by using 1 as the WOEID.
* @return trends
* @throws twitter4j.TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1.1/get/trends/place">GET trends/place | Twitter Developers</a>
* @since Twitter4J 2.1.1
* @deprecated use {@link #getPlaceTrends(int)} instead
*/
Trends getLocationTrends(int woeid) throws TwitterException;

/**
* Returns the top 10 trending topics for a specific WOEID, if trending information is available for it.<br>
* The response is an array of "trend" objects that encode the name of the trending topic, the query parameter that can be used to search for the topic on <a href="http://search.twitter.com/">Twitter Search</a>, and the Twitter Search URL.<br>
* This information is cached for 5 minutes. Requesting more frequently than that will not return any more data, and will count against your rate limit usage.<br>
* <br>This method calls http://api.twitter.com/1.1/trends/place.json
*
* @param woeid <a href="http://developer.yahoo.com/geo/geoplanet/">The Yahoo! Where On Earth ID</a> of the location to return trending information for. Global information is available by using 1 as the WOEID.
* @return trends
* @throws twitter4j.TwitterException when Twitter service or network is unavailable
* @see <a href="https://dev.twitter.com/docs/api/1.1/get/trends/place">GET trends/place | Twitter Developers</a>
* @since Twitter4J 3.0.2
*/
Trends getPlaceTrends(int woeid) throws TwitterException;

/**
* Returns the locations that Twitter has trending topic information for. The response is an array of &quot;locations&quot; that encode the location's WOEID (a <a href="http://developer.yahoo.com/geo/geoplanet/">Yahoo! Where On Earth ID</a>) and some other human-readable information such as a canonical name and country the location belongs in.
* <br>This method calls http://api.twitter.com/1.1/trends/available.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ protected void tearDown() throws Exception {
}

public void testLocalTrendsMethods() throws Exception {
Trends trends = twitter2.getPlaceTrends(1);
assertEquals("Worldwide",trends.getLocation().getName());
ResponseList<Location> locations;
locations = twitter1.getAvailableTrends();
assertNotNull(DataObjectFactory.getRawJSON(locations));
Expand Down

0 comments on commit e53303f

Please sign in to comment.