Class: DownloadSolutions::Api::Reddit::FilterByLanguage

Inherits:
Object
  • Object
show all
Defined in:
lib/download_solutions/api/reddit/filter_by_language.rb

Class Method Summary collapse

Class Method Details

.call(params:) ⇒ Array<Hash>

Filters comments by the desired programming language(s).

Parameters:

Returns:

  • (Array<Hash>)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/download_solutions/api/reddit/filter_by_language.rb', line 9

def self.call(params:)
  params.original_comments.filter { |comment|
    comment_body = comment[:body]&.downcase
    next unless comment_body

    language_specified = comment_body.match?(/\[[[:punct:]]*language:/i)

    if language_specified
      params.languages.any? { |language|
        comment_body.match?(/\[[[:punct:]]*language:\s*#{language}/i)
      }
    else
      params.languages.any? { |language|
        # "sh:" because of "<pre class=\"brush:ruby", see:
        # https://github.com/xijo/reverse_markdown/blob/14d53d5f914fd926b49e6492fd7bd95e62ef541a/lib/reverse_markdown/converters/pre.rb#L37
        comment_body.match?(/(?<!```|sh:)#{language}/i)
      }
    end
  }
end