Sha256: c574e74f8e2d55d966de6fd0122c81c50ea4cc01aa482189c4b2dbdb1a3000ec

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module Redd
  module Models
    # Applied to {Session} for site-wide and {Subreddit} for subreddit-specific search.
    module Searchable
      # Search reddit.
      # @see https://www.reddit.com/wiki/search
      #
      # @param query [String] the search query
      # @param params [Hash] the search params
      # @option params [:cloudsearch, :lucene, :plain] :syntax the query's syntax
      # @option params [String] :after return results after the given fullname
      # @option params [String] :before return results before the given fullname
      # @option params [Integer] :count the number of items already seen in the listing
      # @option params [1..100] :limit the maximum number of things to return
      # @option params [:hour, :day, :week, :month, :year, :all] :time the time period to restrict
      #   search results by
      # @option params [:relevance, :hot, :top, :new, :comments] :sort the sort order of results
      # @option params [String] :restrict_to restrict by subreddit (prefer {Subreddit#search})
      # @return [Listing<Comment, Submission>] the search results
      def search(query, **params)
        params[:q] = query if query
        params[:t] = params.delete(:time) if params.key?(:time)
        if params[:restrict_to]
          subreddit = params.delete(:restrict_to)
          params[:restrict_sr] = true
          @client.model(:get, "/r/#{subreddit}/search", params)
        else
          @client.model(:get, '/search', params)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
redd-0.8.1 lib/redd/models/searchable.rb
redd-0.8.0 lib/redd/models/searchable.rb
redd-0.8.0.pre.2 lib/redd/models/searchable.rb