Sha256: c8d13d000cd7383a4f0e5eea76f92fbb90a0001b03c7b037be633473d2afe954

Contents?: true

Size: 915 Bytes

Versions: 3

Compression:

Stored size: 915 Bytes

Contents

require 'httparty'

module PopcorntimeSearch
  class Search
    include HTTParty
    base_uri BASE_URL

    attr_accessor :title, :season, :episode, :kind

    def initialize(search)
      @title   = search[TITLE_REGEXP, :showname].strip

      season   = search[SEASON_EPISODE_REGEXP, :season]
      episode  = search[SEASON_EPISODE_REGEXP, :episode]
      @season  = season.to_i if season
      @episode = episode.to_i if episode

      @kind = @season && @episode ? :show : :movie
    end

    def results
      @results ||= self.class.get("/#{@kind}s/1", query: { keywords: @title }).map do |result|
        case @kind
        when :movie
          MovieResult.new(result)
        when :show
          ShowResult.new(result, @season, @episode)
        end
      end
    end

    def results_found?
      @results_found ||= results.count > 0
    rescue SocketError
      @results_found = false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
popcorntime_search-1.1.0 lib/popcorntime_search/search.rb
popcorntime_search-1.0.0 lib/popcorntime_search/search.rb
popcorntime_search-0.1.0 lib/popcorntime_search/search.rb