blekko-search 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,16 +3,16 @@ class Blekko
3
3
  DEFAULT_MAX_FREQUENCY_PER_SECOND = 1
4
4
  SECURE_PROTOCOL = "https://"
5
5
  NON_SECURE_PROTOCOL = "http://"
6
-
6
+
7
7
  class << self
8
8
  attr_accessor :last_request_at
9
9
  def last_request_at
10
10
  @last_request_at ||= {}
11
11
  end
12
12
  end
13
-
13
+
14
14
  attr_accessor :protocol, :api_key, :max_frequency_per_second, :username, :password, :login_cookie, :headers
15
-
15
+
16
16
  def initialize(args={})
17
17
  @api_key = args[:api_key]
18
18
  @protocol = args[:secure] ? SECURE_PROTOCOL : NON_SECURE_PROTOCOL
@@ -21,33 +21,33 @@ class Blekko
21
21
  @max_frequency_per_second = args[:max_frequency_per_second] || DEFAULT_MAX_FREQUENCY_PER_SECOND
22
22
  login if @api_key && @username && @password
23
23
  end
24
-
24
+
25
25
  def host
26
26
  HOST
27
27
  end
28
-
28
+
29
29
  def search(query, args={})
30
30
  Blekko::Search.new(self, query, args).search
31
31
  end
32
-
32
+
33
33
  def slashtag(name, args={})
34
34
  Blekko::Slashtag.new(self, name, args)
35
35
  end
36
-
36
+
37
37
  def request(url)
38
38
  sleep(seconds_until_next_request)
39
39
  self.last_request_at = Time.now
40
40
  open(url, headers)
41
41
  end
42
-
42
+
43
43
  def login_uri
44
44
  URI("#{SECURE_PROTOCOL}#{HOST}/login?u=#{CGI.escape(username)}&p=#{CGI.escape(password)}&auth=#{api_key}")
45
45
  end
46
-
46
+
47
47
  def headers
48
- @headers ||= {}
48
+ @headers ||= { "User-Agent" => "blekko-search-#{BlekkoSearch::VERSION}" }
49
49
  end
50
-
50
+
51
51
  def login
52
52
  raise ArgumentError, "Username and password are required" unless username && password
53
53
  Net::HTTP.start(login_uri.host, login_uri.port, use_ssl: true) do |http|
@@ -57,25 +57,25 @@ class Blekko
57
57
  headers["User-Agent"] = "blekko-search-#{BlekkoSearch::VERSION}"
58
58
  end
59
59
  end
60
-
60
+
61
61
  def delay_between_requests
62
62
  1 / max_frequency_per_second.to_f
63
63
  end
64
-
64
+
65
65
  def last_request_at
66
66
  self.class.last_request_at[api_key]
67
67
  end
68
-
68
+
69
69
  def last_request_at=(value)
70
70
  self.class.last_request_at[api_key] = value
71
71
  end
72
-
72
+
73
73
  def earliest_next_request
74
74
  last_request_at ? last_request_at + delay_between_requests : Time.now
75
75
  end
76
-
76
+
77
77
  def seconds_until_next_request
78
78
  [earliest_next_request - Time.now, 0].max
79
79
  end
80
-
80
+
81
81
  end
@@ -1,3 +1,3 @@
1
1
  module BlekkoSearch
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blekko-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-01 00:00:00.000000000 Z
12
+ date: 2013-01-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Search and manage slashtags for blekko.com
15
15
  email: