Class: Blekko

Inherits:
Object
  • Object
show all
Defined in:
lib/blekko-search/blekko.rb,
lib/blekko-search/search.rb,
lib/blekko-search/slashtag.rb,
lib/blekko-search/search_result.rb

Defined Under Namespace

Classes: Search, SearchResult, Slashtag

Constant Summary collapse

HOST =
"blekko.com"
DEFAULT_MAX_FREQUENCY_PER_SECOND =
1
SECURE_PROTOCOL =
"https://"
NON_SECURE_PROTOCOL =
"http://"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Blekko

Returns a new instance of Blekko.



16
17
18
19
20
21
22
23
# File 'lib/blekko-search/blekko.rb', line 16

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
   if @api_key && @username && @password
end

Class Attribute Details

.last_request_atObject

Returns the value of attribute last_request_at.



8
9
10
# File 'lib/blekko-search/blekko.rb', line 8

def last_request_at
  @last_request_at
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



14
15
16
# File 'lib/blekko-search/blekko.rb', line 14

def api_key
  @api_key
end

#headersObject

Returns the value of attribute headers.



14
15
16
# File 'lib/blekko-search/blekko.rb', line 14

def headers
  @headers
end

Returns the value of attribute login_cookie.



14
15
16
# File 'lib/blekko-search/blekko.rb', line 14

def 
  @login_cookie
end

#max_frequency_per_secondObject

Returns the value of attribute max_frequency_per_second.



14
15
16
# File 'lib/blekko-search/blekko.rb', line 14

def max_frequency_per_second
  @max_frequency_per_second
end

#passwordObject

Returns the value of attribute password.



14
15
16
# File 'lib/blekko-search/blekko.rb', line 14

def password
  @password
end

#protocolObject

Returns the value of attribute protocol.



14
15
16
# File 'lib/blekko-search/blekko.rb', line 14

def protocol
  @protocol
end

#usernameObject

Returns the value of attribute username.



14
15
16
# File 'lib/blekko-search/blekko.rb', line 14

def username
  @username
end

Instance Method Details

#delay_between_requestsObject



73
74
75
# File 'lib/blekko-search/blekko.rb', line 73

def delay_between_requests
  1 / max_frequency_per_second.to_f
end

#earliest_next_requestObject



85
86
87
# File 'lib/blekko-search/blekko.rb', line 85

def earliest_next_request
  last_request_at ? last_request_at + delay_between_requests : Time.now
end

#hostObject



25
26
27
# File 'lib/blekko-search/blekko.rb', line 25

def host
  HOST
end

#http_classObject



41
42
43
# File 'lib/blekko-search/blekko.rb', line 41

def http_class
  BlekkoSearch.http_class || Net::HTTP
end

#last_request_atObject



77
78
79
# File 'lib/blekko-search/blekko.rb', line 77

def last_request_at
  self.class.last_request_at[api_key]
end

#last_request_at=(value) ⇒ Object



81
82
83
# File 'lib/blekko-search/blekko.rb', line 81

def last_request_at=(value)
  self.class.last_request_at[api_key] = value
end

#loginObject

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
# File 'lib/blekko-search/blekko.rb', line 63

def 
  raise ArgumentError, "Username and password are required" unless username && password
  Net::HTTP.start(.host, .port, use_ssl: true) do |http|
    response = http.request Net::HTTP::Get.new .request_uri
    self. = response.get_fields('Set-Cookie').find { |c| c =~ /\AA=/ }
    headers["Cookie"] = 
    headers["User-Agent"] = "blekko-search-#{BlekkoSearch::VERSION}"
  end
end

#login_uriObject



55
56
57
# File 'lib/blekko-search/blekko.rb', line 55

def 
  URI("#{SECURE_PROTOCOL}#{HOST}/login?u=#{CGI.escape(username)}&p=#{CGI.escape(password)}&auth=#{api_key}")
end

#request(path) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/blekko-search/blekko.rb', line 45

def request(path)
  sleep(seconds_until_next_request)
  self.last_request_at = Time.now
  http_class.start("blekko.com", 80) do |http|
    request = Net::HTTP::Get.new path
    headers.each { |key, value| request[key] = value }
    http.request(request).body
  end
end

#search(query, args = {}) ⇒ Object



29
30
31
# File 'lib/blekko-search/blekko.rb', line 29

def search(query, args={})
  Blekko::Search.new(self, query, args).search
end

#seconds_until_next_requestObject



89
90
91
# File 'lib/blekko-search/blekko.rb', line 89

def seconds_until_next_request
  [earliest_next_request - Time.now, 0].max
end

#slashtag(name, args = {}) ⇒ Object



37
38
39
# File 'lib/blekko-search/blekko.rb', line 37

def slashtag(name, args={})
  Blekko::Slashtag.new(self, name, args)
end

#web_url(query) ⇒ Object



33
34
35
# File 'lib/blekko-search/blekko.rb', line 33

def web_url(query)
  Blekko::Search.new(self, query).web_url
end