Module: Snooby
- Defined in:
- lib/snooby.rb,
lib/snooby/client.rb,
lib/snooby/actions.rb,
lib/snooby/objects.rb
Defined Under Namespace
Modules: Actions Classes: Client, Comment, Post, RedditError, Subreddit, User
Constant Summary collapse
- Conn =
Opens a persistent connection that provides a significant speed improvement during repeated calls; reddit’s rate limit nullifies this for the most part, but it’s still a nice library and persistent connections are a Good Thing.
Net::HTTP::Persistent.new('snooby')
- Paths =
paths.merge(paths) { |k, v| 'http://www.reddit.com/' + v }
- Fields =
Provides a mapping of things to a list of all the attributes present in the relevant JSON object. A lot of these probably won’t get used too often, but might as well expose all available data.
{ :comment => %w[author author_flair_css_class author_flair_text body body_html created created_utc downs id likes link_id link_title name parent_id replies subreddit subreddit_id ups], :post => %w[author author_flair_css_class author_flair_text clicked created created_utc domain downs hidden id is_self likes media media_embed name num_comments over_18 permalink saved score selftext selftext_html subreddit subreddit_id thumbnail title ups url] }
Class Attribute Summary collapse
-
.auth ⇒ Object
Returns the value of attribute auth.
-
.config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
-
.build(object, path, which) ⇒ Object
The crux of Snooby.
Class Attribute Details
.auth ⇒ Object
Returns the value of attribute auth.
60 61 62 |
# File 'lib/snooby.rb', line 60 def auth @auth end |
.config ⇒ Object
Returns the value of attribute config.
60 61 62 |
# File 'lib/snooby.rb', line 60 def config @config end |
Class Method Details
.build(object, path, which) ⇒ Object
The crux of Snooby. Generates an array of structs from the Paths and Fields hashes defined above. In addition to just being a very neat container, this allows accessing the returned JSON values using thing.attribute, as opposed to thing[‘attribute’]. Only used for listings of posts and comments at the moment, but I imagine it’ll be used for moderation down the road.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/snooby.rb', line 38 def self.build(object, path, which) # A bit of string manipulation to determine which fields to populate the # generated struct with. There might be a less fragile way to go about it, # but it shouldn't be a problem as long as naming remains consistent. kind = object.to_s.split('::')[1].downcase.to_sym # Having to explicitly pass the path symbol isn't exactly DRY, but deriving # it from the object parameter (say, Snooby::Comment) doesn't expose which # kind of comment it is, either User or Post. uri = URI(Paths[path] % which) # This'll likely have to be tweaked to handle other types of listings, but # it's sufficient for comments and posts. JSON.parse(Conn.request(uri).body)['data']['children'].map do |child| # Maps each of the listing's children to the relevant struct based on the # object type passed in. The symbols in a struct definition are ordered, # but Python dictionaries are not, so #values isn't sufficient. object.new(*child['data'].values_at(*Fields[kind])) end end |