Sha256: c22d90841656d16f688ef86b654cd38ee817ae5d63509a40d852692200eb58f2

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

class Blekko
  class Search

    DEFAULT_PAGE_SIZE = 100
    DEFAULT_PAGE_NUMBER = 0
    PREFIX = "/ws/?q="
    RESPONSE_FORMAT = "/json+/"

    attr_accessor :query, :slashtags, :results, :blekko

    def initialize(blekko, query, args={})
      args = {page_size: DEFAULT_PAGE_SIZE }.merge(args)
      @blekko = blekko
      @query = query
      @slashtags = *args[:slashtags] || []
      @page_size = args[:page_size]
      @total_size = args[:total_size] || @page_size
    end

    def results
      @results ||= []
    end

    def search
      page_number = 0
      number_of_searches.times do
        response = JSON.load(blekko.request(url(page_number)))
        if response['RESULT']
          self.results += response['RESULT'].collect { |r| Blekko::SearchResult.new(r) }
        else
          return results
        end
        page_number += 1
      end
      results[0,@total_size]
    end

    def number_of_searches
      @number_of_searches ||= (@total_size.to_f / @page_size).ceil
    end

    def escaped_query
      CGI.escape(query + " ") + @slashtags.collect { |s| CGI.escape(s) }.join("+") + "+"
    end

    def page_size_param
      "ps=#{@page_size}"
    end

    def page_number_param(page_number)
      "p=#{page_number}" if page_number > 0
    end

    def auth_param
      blekko.api_key ? "auth=#{blekko.api_key}" : nil
    end

    def params(page_number)
      [page_size_param, auth_param, page_number_param(page_number)].compact.join("&")
    end

    def url(page_number)
      blekko.protocol + blekko.host + PREFIX + escaped_query + RESPONSE_FORMAT + params(page_number)
    end

    def web_url
      blekko.protocol + blekko.host + PREFIX + escaped_query
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blekko-search-0.0.6 lib/blekko-search/search.rb