Class: Cinch::Plugins::Reddit
- Inherits:
-
Object
- Object
- Cinch::Plugins::Reddit
- Includes:
- ActionView::Helpers::DateHelper, Cinch::Plugin
- Defined in:
- lib/cinch/plugins/reddit.rb
Constant Summary collapse
- RedditBaseUrl =
"http://www.reddit.com"
Instance Method Summary collapse
-
#commify(n) ⇒ Object
Takes an ingeger, puts commas in appropriate places, and returns a string.
- #karma(m, user) ⇒ Object
- #linkLookup(m, query) ⇒ Object
- #lookup(m, query) ⇒ Object
- #mods(m, subreddit) ⇒ Object
-
#pluralize(n, singular, plural = nil) ⇒ Object
Pluralizes a string Simple and stupid, but it works.
-
#pluralJoin(array) ⇒ Object
Utility to join an array into a gramarically correct sentence.
- #readers(m, subreddit) ⇒ Object
-
#urlload(url) ⇒ Object
Loads a URL from the reddit API This saves time, as well as providing our own unique user-agent.
Instance Method Details
#commify(n) ⇒ Object
Takes an ingeger, puts commas in appropriate places, and returns a string
23 24 25 26 27 28 29 |
# File 'lib/cinch/plugins/reddit.rb', line 23 def commify(n) n.to_s =~ /([^\.]*)(\..*)?/ int, dec = $1.reverse, $2 ? $2 : "" while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2,\3') end int.reverse + dec end |
#karma(m, user) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/cinch/plugins/reddit.rb', line 59 def karma(m, user) url = "%s/user/%s/about.json" % [RedditBaseUrl, user] data = JSON.parse(urlload url)["data"] rescue nil if data m.reply("%s has a link karma of %s and comment karma of %s" % [ Format(:bold, data["name"]), commify(data["link_karma"]), commify(data["comment_karma"]) ]) else m.reply("%s doesn't appear to exist and therefore can't have karma" % user) end end |
#linkLookup(m, query) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/cinch/plugins/reddit.rb', line 116 def linkLookup(m, query) return nil unless URI.parse(query).host.end_with?('reddit.com') thing_id = URI.parse(query).path.split('/')[4] url = "%s/api/info.json?id=t3_%s" % [RedditBaseUrl, thing_id] data = JSON.parse(urlload url)["data"]["children"][0]["data"] rescue nil if data m.reply("\"%s\" %s(%s|%s) by %s, %s ago, to /r/%s" % [ Format(:bold, data['title']), Format(:grey, commify(data['score'])), Format(:orange, "+" + commify(data['ups'])), Format(:blue, "-" + commify(data['downs'])), data['author'], time_ago_in_words(DateTime.strptime(data['created'].to_s, '%s')), data['subreddit'] ]) else m.reply("I couldn't find that for some reason. It might be my fault, or it might be reddit's") end end |
#lookup(m, query) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cinch/plugins/reddit.rb', line 96 def lookup(m, query) url = "%s/api/info.json?url=%s" % [RedditBaseUrl, query] data = JSON.parse(urlload url)["data"]["children"][0]["data"] rescue nil if data m.reply("%s - \"%.100s\" %s(%s|%s) by %s, %s ago, to /r/%s" % [ "http://redd.it/#{data['id']}", Format(:bold, data['title']), Format(:grey, commify(data['score'])), Format(:orange, "+" + commify(data['ups'])), Format(:blue, "-" + commify(data['downs'])), data['author'], time_ago_in_words(DateTime.strptime(data['created'].to_s,'%s')), data['subreddit'] ]) else m.reply("I couldn't find that for some reason. It might be my fault, or it might be reddit's") end end |
#mods(m, subreddit) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/cinch/plugins/reddit.rb', line 71 def mods(m, subreddit) url = "%s/r/%s/about/moderators.json" % [RedditBaseUrl, subreddit] data = JSON.parse(urlload url)["data"]["children"] rescue nil if data subMods = [] data.each { |mod| subMods << mod["name"] } m.reply("/r/%s has %s: %s" % [Format(:bold, subreddit), pluralize( subMods.length, "moderator"), pluralJoin(subMods) ]) else m.reply("/r/%s doesn't appear to exist and therefore can't have moderators" % subreddit) end end |
#pluralize(n, singular, plural = nil) ⇒ Object
Pluralizes a string Simple and stupid, but it works
39 40 41 42 43 44 45 46 47 |
# File 'lib/cinch/plugins/reddit.rb', line 39 def pluralize(n, singular, plural=nil) if n == 1 "1 #{singular}" elsif plural "%s %s" % [commify(n), plural] else "%s %ss" % [commify(n), singular] end end |
#pluralJoin(array) ⇒ Object
Utility to join an array into a gramarically correct sentence.
50 51 52 53 |
# File 'lib/cinch/plugins/reddit.rb', line 50 def pluralJoin(array) return array[0] unless array.length > 1 array[0..-2].join(", ") + ", and " + array[-1] end |
#readers(m, subreddit) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/cinch/plugins/reddit.rb', line 85 def readers(m, subreddit) url = "%s/r/%s/about.json" % [RedditBaseUrl, subreddit] data = JSON.parse(urlload url)["data"]["subscribers"] rescue nil if data m.reply("/r/%s has %s" % [Format(:bold, subreddit), pluralize(data, "reader")]) else m.reply("/r/%s doesn't appear to exist and therefore can't have subscribers" % subreddit) end end |
#urlload(url) ⇒ Object
Loads a URL from the reddit API This saves time, as well as providing our own unique user-agent
33 34 35 |
# File 'lib/cinch/plugins/reddit.rb', line 33 def urlload(url) open(url, "User-Agent" => "reddit-irc-bot").read end |