Class: Scriptorium::Reddit
- Inherits:
-
Object
- Object
- Scriptorium::Reddit
- Includes:
- Exceptions, Helpers
- Defined in:
- lib/scriptorium/reddit.rb
Instance Method Summary collapse
-
#autopost(post_data, subreddit = nil) ⇒ Object
Autopost a post to Reddit.
-
#config ⇒ Object
Get Reddit configuration.
-
#configured? ⇒ Boolean
Check if Reddit autoposting is configured.
-
#initialize(repo) ⇒ Reddit
constructor
A new instance of Reddit.
Methods included from Helpers
#cf_time, #change_config, #clean_slugify, #copy_gem_asset_to_user, #copy_to_clipboard, #d4, #escape_html, #generate_missing_asset_svg, #get_asset_path, #get_from_clipboard, #getvars, #list_gem_assets, #make_dir, #make_tree, #need, #read_commented_file, #read_file, #see, #see_file, #slugify, #substitute, #system!, #view_dir, #write_file, #write_file!, #ymdhms
Methods included from Exceptions
Constructor Details
Instance Method Details
#autopost(post_data, subreddit = nil) ⇒ Object
Autopost a post to Reddit
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/scriptorium/reddit.rb', line 17 def autopost(post_data, subreddit = nil) need(:file, @credentials_file, "Reddit credentials file not found") config = self.config return false unless config begin # Initialize Reddit session using redd gem session = create_reddit_session(config) # Determine target subreddit target_subreddit = subreddit || post_data[:subreddit] || config['default_subreddit'] raise "No subreddit specified" unless target_subreddit # Get the subreddit and submit the post subreddit_instance = session.subreddit(target_subreddit) submission = subreddit_instance.submit( post_data[:title], url: post_data[:url], resubmit: false ) ("Successfully autoposted to Reddit: #{post_data[:title]} -> r/#{target_subreddit}") return true rescue => e ("Failed to autopost to Reddit: #{e.}") return false end end |
#config ⇒ Object
Get Reddit configuration
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/scriptorium/reddit.rb', line 54 def config return nil unless configured? begin JSON.parse(read_file(@credentials_file)) rescue => e ("Failed to parse Reddit credentials: #{e.}") nil end end |
#configured? ⇒ Boolean
Check if Reddit autoposting is configured
49 50 51 |
# File 'lib/scriptorium/reddit.rb', line 49 def configured? File.exist?(@credentials_file) end |