lib/blekko-search/blekko.rb in blekko-search-0.0.3 vs lib/blekko-search/blekko.rb in blekko-search-0.0.4

- old
+ new

@@ -9,11 +9,11 @@ def last_request_at @last_request_at ||= {} end end - attr_accessor :protocol, :api_key, :max_frequency_per_second, :username, :password, :login_cookie + attr_accessor :protocol, :api_key, :max_frequency_per_second, :username, :password, :login_cookie, :headers def initialize(args={}) @api_key = args[:api_key] @protocol = args[:secure] ? SECURE_PROTOCOL : NON_SECURE_PROTOCOL @username = args[:username] @@ -43,20 +43,19 @@ 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}" - } + @headers ||= {} 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=/ } + headers["Cookie"] = login_cookie + headers["User-Agent"] = "blekko-search-#{BlekkoSearch::VERSION}" end end def delay_between_requests 1 / max_frequency_per_second.to_f