Sha256: 86362cff1884428af666d2a9c8c8f4d33d6bf9c73536eefbaf28156e0eb636ed

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

class Blekko
  HOST = "blekko.com"
  DEFAULT_MAX_FREQUENCY_PER_SECOND = 1
  SECURE_PROTOCOL = "https://"
  NON_SECURE_PROTOCOL = "http://"
      
  attr_accessor :protocol, :api_key, :max_frequency_per_second, :username, :password, :login_cookie,
  :last_request_at
      
  def initialize(args={})
    @api_key = args[:api_key]
    @protocol = args[:secure] ? SECURE_PROTOCOL : NON_SECURE_PROTOCOL
    @username = args[:username]
    @password = args[:password]
    @max_frequency_per_second = args[:max_frequency_per_second] || DEFAULT_MAX_FREQUENCY_PER_SECOND
    login if @api_key && @username && @password
  end
  
  def host
    HOST
  end
  
  def search(query, args={})
    Blekko::Search.new(self, query, args).search
  end
  
  def slashtag(name, args={})
    Blekko::Slashtag.new(self, name, args)
  end
  
  def request(url)
    sleep(seconds_until_next_request)
    self.last_request_at = Time.now
    open(url, headers)
  end
  
  def login_uri
    URI("#{SECURE_PROTOCOL}#{HOST}/login?u=#{CGI.escape(username)}&p=#{CGI.escape(password)}&auth=#{api_key}")
  end
  
  def headers
    { 
      "Cookie" => login_cookie,
      "User-Agent" => "blekko-search-#{BlekkoSearch::VERSION}"
    }
  end
  
  def login
    raise ArgumentError, "Username and password are required" unless username && password
    Net::HTTP.start(login_uri.host, login_uri.port, use_ssl: true) do |http|
      response = http.request Net::HTTP::Get.new login_uri.request_uri
      self.login_cookie = response.get_fields('Set-Cookie').find { |c| c =~ /\AA=/ }
    end
  end
  
  def delay_between_requests
    1 / max_frequency_per_second.to_f
  end
  
  def earliest_next_request
    last_request_at ? last_request_at + delay_between_requests : Time.now
  end
  
  def seconds_until_next_request
    [earliest_next_request - Time.now, 0].max
  end
  
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

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