Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.jreddit.retrieval;
  
  
  import static com.github.jreddit.utils.restclient.JsonUtils.safeJsonToString;
  
  import java.util.List;
 
 
This class offers the following functionality: 1) Parsing the results of a request into Subreddit objects (see Subreddits.parse()). 2) The ability to get listings of subreddits (see Subreddits.get()). 3) The ability to search the subreddits (for subreddits) on Reddit (see Subreddits.search()).

Author(s):
Benjamin Jakobus
Raul Rene Lepsa
Andrei Sfat
Simon Kassing
 
 public class Subreddits implements ActorDriven {

Handle to the REST client instance.
 
     private final RestClient restClient;
     private User user;

    
Constructor.

Parameters:
restClient REST client instance
 
     public Subreddits(RestClient restClient) {
         this. = restClient;
     }
    
    
Constructor.

Parameters:
restClient REST Client instance
actor User instance
 
     public Subreddits(RestClient restClientUser actor) {
     	this. = restClient;
         this. = actor;
     }
    
    
Switch the current user for the new user who will be used when invoking retrieval requests.

Parameters:
new_actor New user
 
     public void switchActor(User new_actor) {
     	this. = new_actor;
     }
    
    
Parses a JSON feed from the Reddit (URL) into a nice list of Subreddit objects.

Parameters:
url URL
Returns:
Listing of submissions
 
     public List<Subredditparse(String urlthrows RetrievalFailedExceptionRedditError {
     	
     	// Determine cookie
     	String cookie = ( == null) ? null : .getCookie();
     	
     	// List of subreddits
         List<Subredditsubreddits = new LinkedList<Subreddit>();
         
         // Send request to reddit server via REST client
         Object response = .get(urlcookie).getResponseObject();
         
         if (response instanceof JSONObject) {
         	
 	        JSONObject object = (JSONObjectresponse;
 	        JSONArray array = (JSONArray) ((JSONObjectobject.get("data")).get("children");
 	
 	        // Iterate over the subreddit results
 	        JSONObject data;
 	        for (Object anArray : array) {
 	            data = (JSONObjectanArray;
 	            
 	            // Make sure it is of the correct kind
	            String kind = safeJsonToString(data.get("kind"));
				if (kind != null) {
					if (kind.equals(..value())) {
                        // Create and add subreddit
                        data = ((JSONObjectdata.get("data"));
                        subreddits.add(new Subreddit(data));
                    }
				}
			}
        } else {
        	..println("Cannot cast to JSON Object: '" + response.toString() + "'");
        }
        // Finally return list of subreddits 
        return subreddits;
        
    }
    
    
Searches all subreddits with the given query using the given parameters. The parameters here are in Strings instead of wrapper objects, which allows users to manually adjust the parameters (if the API changes and jReddit is not updated in time yet).

Parameters:
query Search query
count Count at which the subreddits are started being numbered
limit Maximum amount of subreddits that can be returned (0-100, 25 default (see Reddit API))
after The subreddit after which needs to be retrieved
before The subreddit after which needs to be retrieved
Returns:
List of subreddits that satisfy the given parameters.
    public List<Subredditsearch(String queryString countString limitString afterString beforethrows RetrievalFailedExceptionRedditError {
    	
    	// Format parameters
    	String params = "";
    	try {
			params = ParamFormatter.addParameter(params"q", URLEncoder.encode(query"ISO-8859-1"));
		}
    	params = ParamFormatter.addParameter(params"count"count);
    	params = ParamFormatter.addParameter(params"limit"limit);
    	params = ParamFormatter.addParameter(params"after"after);
    	params = ParamFormatter.addParameter(params"before"before);
    	
        // Retrieve submissions from the given URL
        return parse(String.format(.params));
        
    }

    
Searches all subreddits with the given query using the given parameters.

Parameters:
query Search query
count Count at which the subreddits are started being numbered
limit Maximum amount of subreddits that can be returned (0-100, 25 default (see Reddit API))
after The subreddit after which needs to be retrieved
before The subreddit after which needs to be retrieved
Returns:
List of subreddits that satisfy the given parameters.
    public List<Subredditsearch(String queryint countint limitSubreddit afterSubreddit beforethrows RetrievalFailedExceptionRedditError {
    	
    	if (query == null || query.isEmpty()) {
    		throw new IllegalArgumentException("The query must be defined.");
    	}
    	
    	return search(
    			query
    			String.valueOf(count),
    			String.valueOf(limit),
    			(after != null) ? after.getFullName() : "",
    			(before != null) ? before.getFullName() : ""	
    	);
    	
    }
    
    
Gets all subreddits using the given parameters. The parameters here are in Strings instead of wrapper objects, which allows users to manually adjust the parameters (if the API changes and jReddit is not updated in time yet).

Parameters:
type Type of subreddit, this determines the ordering (e.g. new or mine)
count Count at which the subreddits are started being numbered
limit Maximum amount of subreddits that can be returned (0-100, 25 default (see Reddit API))
after The subreddit after which needs to be retrieved
before The subreddit after which needs to be retrieved
Returns:
List of subreddits that satisfy the given parameters.
    public List<Subredditget(String typeString countString limitString afterString beforethrows RetrievalFailedExceptionRedditError {
    	
    	// Format parameters
    	String params = "";
    	params = ParamFormatter.addParameter(params"count"count);
    	params = ParamFormatter.addParameter(params"limit"limit);
    	params = ParamFormatter.addParameter(params"after"after);
    	params = ParamFormatter.addParameter(params"before"before);
    	
        // Retrieve submissions from the given URL
        return parse(String.format(.typeparams));
        
    }
    
    
Gets all subreddits using the given parameters.

Parameters:
type Type of subreddit, this determines the ordering (e.g. new or mine)
count Count at which the subreddits are started being numbered
limit Maximum amount of subreddits that can be returned (0-100, 25 default (see Reddit API))
after The subreddit after which needs to be retrieved
before The subreddit after which needs to be retrieved
Returns:
List of subreddits that satisfy the given parameters.
    public List<Subredditget(SubredditsView typeint countint limitSubreddit afterSubreddit beforethrows RetrievalFailedExceptionRedditError {
        	return get(
        			(type != null) ? type.value() : "",
        			String.valueOf(count),
        			String.valueOf(limit),
        			(after != null) ? after.getFullName() : "",
        			(before != null) ? before.getFullName() : ""
        	);
    }
New to GrepCode? Check out our FAQ X