Class: Scriptorium::Reddit

Inherits:
Object
  • Object
show all
Includes:
Exceptions, Helpers
Defined in:
lib/scriptorium/reddit.rb

Instance Method Summary collapse

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

#make_exception

Constructor Details

#initialize(repo) ⇒ Reddit

Returns a new instance of Reddit.



11
12
13
14
# File 'lib/scriptorium/reddit.rb', line 11

def initialize(repo)
  @repo = repo
  @credentials_file = @repo.root/:config/"reddit_credentials.json"
end

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
    )
    
    log_message("Successfully autoposted to Reddit: #{post_data[:title]} -> r/#{target_subreddit}")
    return true
    
  rescue => e
    log_message("Failed to autopost to Reddit: #{e.message}")
    return false
  end
end

#configObject

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
    log_message("Failed to parse Reddit credentials: #{e.message}")
    nil
  end
end

#configured?Boolean

Check if Reddit autoposting is configured

Returns:

  • (Boolean)


49
50
51
# File 'lib/scriptorium/reddit.rb', line 49

def configured?
  File.exist?(@credentials_file)
end