Module: RedditPostToMarkdown
- Defined in:
- lib/reddit_post_to_markdown.rb,
lib/reddit_post_to_markdown/errors.rb,
lib/reddit_post_to_markdown/version.rb,
lib/reddit_post_to_markdown/post_renderer.rb,
lib/reddit_post_to_markdown/reddit_client.rb,
lib/reddit_post_to_markdown/url_validator.rb
Overview
Top-level namespace for the reddit_post_to_markdown gem.
Defined Under Namespace
Classes: FetchError, InvalidResponseError, NotAPostError, PostRenderer, RedditClient, UrlValidator
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.convert(url, filters: {}, include_comments: true) ⇒ String
Downloads a public Reddit post and returns it as a Markdown string.
Class Method Details
.convert(url, filters: {}, include_comments: true) ⇒ String
Downloads a public Reddit post and returns it as a Markdown string.
The URL must point directly to a single post. Subreddit listings, user profiles, search pages, and similar URLs will raise NotAPostError. Posts that require authentication (private subreddits, age-gated content) are not accessible.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/reddit_post_to_markdown.rb', line 57 def self.convert(url, filters: {}, include_comments: true) clean = UrlValidator.clean_url(url) unless UrlValidator.valid_post_url?(clean) raise NotAPostError, "Not a Reddit post URL: #{url}" end data = RedditClient.new.fetch_post(clean) post_info = data.dig(0, "data", "children") raise InvalidResponseError, "No post data found in response" if post_info.nil? || post_info.empty? post_data = post_info[0].fetch("data", {}) replies_data = include_comments ? (data.dig(1, "data", "children") || []) : [] PostRenderer.render(post_data, replies_data, filters: filters) end |