Using Reddit API for Ruby

$ gem install redd
require 'redd'session = Redd.it(
user_agent: 'personal use script',
client_id: CLIENT_ID,
secret: SECRET,
username: USERNAME,
password: PASSWORD
)
  • user_agent is a way to tell the server who you are. There are standards to follow depending on the app you’re creating, but for my purposes I just gave it a generic title.
  • client_id and secret are given to you when you register your app on Reddit.
  • username and password is the account credentials of the Reddit account you’re using the API with. I used my own personal account login to test out the API, but if I were to use in a real app I would want to create a separate account specifically for API usage.
r_ruby = session.subreddit('ruby')
post = r_ruby.hot.first
puts post.title
r_ruby = session.subreddit('ruby')
subs = r_ruby.subscribers
puts subs

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store