Class: Blekko
- Inherits:
-
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
login if @api_key && @username && @password
end
|
Class Attribute Details
.last_request_at ⇒ Object
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_key ⇒ Object
Returns the value of attribute api_key.
14
15
16
|
# File 'lib/blekko-search/blekko.rb', line 14
def api_key
@api_key
end
|
Returns the value of attribute headers.
14
15
16
|
# File 'lib/blekko-search/blekko.rb', line 14
def
@headers
end
|
#login_cookie ⇒ Object
Returns the value of attribute login_cookie.
14
15
16
|
# File 'lib/blekko-search/blekko.rb', line 14
def login_cookie
@login_cookie
end
|
#max_frequency_per_second ⇒ Object
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
|
#password ⇒ Object
Returns the value of attribute password.
14
15
16
|
# File 'lib/blekko-search/blekko.rb', line 14
def password
@password
end
|
#protocol ⇒ Object
Returns the value of attribute protocol.
14
15
16
|
# File 'lib/blekko-search/blekko.rb', line 14
def protocol
@protocol
end
|
#username ⇒ Object
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_requests ⇒ Object
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_request ⇒ Object
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
|
#host ⇒ Object
25
26
27
|
# File 'lib/blekko-search/blekko.rb', line 25
def host
HOST
end
|
#http_class ⇒ Object
41
42
43
|
# File 'lib/blekko-search/blekko.rb', line 41
def http_class
BlekkoSearch.http_class || Net::HTTP
end
|
#last_request_at ⇒ Object
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
|
#login ⇒ Object
63
64
65
66
67
68
69
70
71
|
# File 'lib/blekko-search/blekko.rb', line 63
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=/ }
["Cookie"] = login_cookie
["User-Agent"] = "blekko-search-#{BlekkoSearch::VERSION}"
end
end
|
#login_uri ⇒ Object
55
56
57
|
# File 'lib/blekko-search/blekko.rb', line 55
def login_uri
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
.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_request ⇒ Object
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
|