blekko-search 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,15 @@
1
1
  class Blekko
2
- HOST = "www.blekko.com"
2
+ HOST = "blekko.com"
3
3
  DEFAULT_MAX_FREQUENCY_PER_SECOND = 1
4
+ SECURE_PROTOCOL = "https://"
5
+ NON_SECURE_PROTOCOL = "http://"
4
6
 
5
- attr_accessor :protocol, :api_key, :max_frequency_per_second, :username, :password, :login_cookie
7
+ attr_accessor :protocol, :api_key, :max_frequency_per_second, :username, :password, :login_cookie,
8
+ :last_request_at
6
9
 
7
10
  def initialize(args={})
8
11
  @api_key = args[:api_key]
9
- @protocol = args[:secure] ? "https://" : "http://"
12
+ @protocol = args[:secure] ? SECURE_PROTOCOL : NON_SECURE_PROTOCOL
10
13
  @username = args[:username]
11
14
  @password = args[:password]
12
15
  @max_frequency_per_second = args[:max_frequency_per_second] || DEFAULT_MAX_FREQUENCY_PER_SECOND
@@ -25,14 +28,20 @@ class Blekko
25
28
  Blekko::Slashtag.new(self, name, args)
26
29
  end
27
30
 
31
+ def request(url)
32
+ sleep(seconds_until_next_request)
33
+ self.last_request_at = Time.now
34
+ open(url, headers)
35
+ end
36
+
28
37
  def login_uri
29
- URI("https://blekko.com/login?u=#{CGI.escape(username)}&p=#{CGI.escape(password)}&auth=#{api_key}")
38
+ URI("#{SECURE_PROTOCOL}#{HOST}/login?u=#{CGI.escape(username)}&p=#{CGI.escape(password)}&auth=#{api_key}")
30
39
  end
31
40
 
32
41
  def headers
33
42
  {
34
- "Cookie" => "A=AfGTtGoSio7Hnc1xiaSrwkJX4ggMfcFiBXufPUPQXZvB5TRe36Q6tPI4woK6SKO8%2Bh8qeD7z0qEk%2B4Ceg5N9HA95UTpznKUvuuEfb04GiwhAlKARpLnp18%2BI6EYQfes1PB0QNnhHwEAC3kLjyJqCZbsxVw8ud4Z6F%2Fbg6BvJj28L;",
35
- "User-Agent" => "Ruby"
43
+ "Cookie" => login_cookie,
44
+ "User-Agent" => "blekko-search-#{BlekkoSearch::VERSION}"
36
45
  }
37
46
  end
38
47
 
@@ -44,5 +53,17 @@ class Blekko
44
53
  end
45
54
  end
46
55
 
56
+ def delay_between_requests
57
+ 1 / max_frequency_per_second.to_f
58
+ end
59
+
60
+ def earliest_next_request
61
+ last_request_at ? last_request_at + delay_between_requests : Time.now
62
+ end
63
+
64
+ def seconds_until_next_request
65
+ [earliest_next_request - Time.now, 0].max
66
+ end
67
+
47
68
 
48
69
  end
@@ -6,7 +6,7 @@ class Blekko
6
6
  PREFIX = "/ws/?q="
7
7
  RESPONSE_FORMAT = "/json+/"
8
8
 
9
- attr_accessor :query, :slashtags, :results
9
+ attr_accessor :query, :slashtags, :results, :blekko
10
10
 
11
11
  def initialize(blekko, query, args={})
12
12
  args = {page_size: DEFAULT_PAGE_SIZE }.merge(args)
@@ -24,7 +24,7 @@ class Blekko
24
24
  def search
25
25
  page_number = 0
26
26
  number_of_searches.times do
27
- response = JSON.load(open(url(page_number)))
27
+ response = JSON.load(blekko.request(url(page_number)))
28
28
  if response['RESULT']
29
29
  self.results += response['RESULT'].collect { |r| Blekko::SearchResult.new(r) }
30
30
  else
@@ -40,7 +40,7 @@ class Blekko
40
40
  end
41
41
 
42
42
  def escaped_query
43
- CGI.escape(query + " ") + @slashtags.join("+") + "+"
43
+ CGI.escape(query + " ") + @slashtags.collect { |s| CGI.escape(s) }.join("+") + "+"
44
44
  end
45
45
 
46
46
  def page_size_param
@@ -48,11 +48,11 @@ class Blekko
48
48
  end
49
49
 
50
50
  def page_number_param(page_number)
51
- "p=#{page_number}"
51
+ "p=#{page_number}" if page_number > 0
52
52
  end
53
53
 
54
54
  def auth_param
55
- @blekko.api_key ? "auth=#{@blekko.api_key}" : nil
55
+ blekko.api_key ? "auth=#{blekko.api_key}" : nil
56
56
  end
57
57
 
58
58
  def params(page_number)
@@ -60,7 +60,7 @@ class Blekko
60
60
  end
61
61
 
62
62
  def url(page_number)
63
- @blekko.protocol + @blekko.host + PREFIX + escaped_query + RESPONSE_FORMAT + params(page_number)
63
+ blekko.protocol + blekko.host + PREFIX + escaped_query + RESPONSE_FORMAT + params(page_number)
64
64
  end
65
65
 
66
66
  end
@@ -8,7 +8,7 @@ class Blekko
8
8
  @blekko = blekko
9
9
  @name = name
10
10
  @urls = *args[:urls]
11
- if args[:eager_load] && !@urls
11
+ if args[:eager_load] && urls.empty?
12
12
  self.urls = saved_urls
13
13
  end
14
14
  end
@@ -19,7 +19,7 @@ class Blekko
19
19
 
20
20
  def saved_urls
21
21
  url = blekko.protocol + blekko.host + "/tag/view?name=" + CGI.escape(name) + "&format=text&auth=#{blekko.api_key}"
22
- lines = open(url).collect { |line| line.strip }
22
+ lines = open(url, blekko.headers).collect { |line| line.strip }
23
23
  unless lines.first.scan(" ").any?
24
24
  lines.collect { |line| line }
25
25
  end
@@ -37,35 +37,34 @@ class Blekko
37
37
  end
38
38
 
39
39
  def remove_urls!(target_urls)
40
- open(remove_url(target_urls), blekko.headers)
40
+ blekko.request(remove_url(target_urls))
41
41
  true
42
42
  end
43
43
 
44
44
 
45
45
  def create!
46
- open(save_url("create"), blekko.headers)
46
+ blekko.request(save_url("create"))
47
47
  end
48
48
 
49
49
  def update!
50
- open(save_url("update"), blekko.headers)
50
+ blekko.request(save_url("update"))
51
51
  end
52
52
 
53
-
54
53
  def save_url(method, target_urls=urls)
55
- "https://" + blekko.host + "/tag/add?name=#{name}&submit=#{method}&urls=#{urls.join("%0A")}&auth=#{blekko.api_key}"
54
+ blekko.protocol + blekko.host + "/tag/add?name=#{name}&submit=#{method}&urls=#{urls.join("%0A")}&auth=#{blekko.api_key}"
56
55
  end
57
56
 
58
57
  def remove_url(target_urls)
59
- "https://" + blekko.host + "/tag/edit?submit=1&type=del&name=#{name}&urls=#{target_urls.join("%0A")}&auth=#{blekko.api_key}"
58
+ blekko.protocol + blekko.host + "/tag/edit?submit=1&type=del&name=#{name}&urls=#{target_urls.join("%0A")}&auth=#{blekko.api_key}"
60
59
  end
61
60
 
62
61
  def delete_url
63
- "https://blekko.com/tag/delete?submit=1&name=#{name}&auth=#{blekko.api_key}"
62
+ blekko.protocol + blekko.host + "/tag/delete?submit=1&name=#{name}&auth=#{blekko.api_key}"
64
63
  end
65
64
 
66
65
  def delete!
67
66
  return ArgumentError, "This is not implemented by blekko yet"
68
- open(delete_url, blekko.headers)
67
+ blekko.reqeust(delete_url)
69
68
  end
70
69
 
71
70
  end
@@ -1,3 +1,3 @@
1
1
  module BlekkoSearch
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: