ask-web-search 0.3.0 → 0.4.0
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.
- checksums.yaml +4 -4
- data/lib/ask/web_search/version.rb +1 -1
- data/lib/ask/web_search.rb +35 -9
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 56161d32f9534d056405cc0fd382fd6c07be300ae35084e0bea4a5017b1fe353
|
|
4
|
+
data.tar.gz: cefb6a830bff2b3998174d2b9f9d75d4bcf42f00958766b55bc9d4679c2d8c17
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1d0b196f7b4be2b6f0da74c271aa2c2446c5d5d6ec45b16a0b4b9eac2a46abf37e8461895c1f679b3b78041f5d472c6ed6590a6a6347da1e2db636e422485a29
|
|
7
|
+
data.tar.gz: fe6766f5f91bd11b82ea6d555c01248bbdbc93c152b657ce042c1011967d75994bfff13dce23730360bdde5eed3c14e28859749764f34c12fee81afaebf04fb2
|
data/lib/ask/web_search.rb
CHANGED
|
@@ -22,6 +22,20 @@ module Ask
|
|
|
22
22
|
@searxng_url = url
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
+
# Retry configuration. Set max_retries to 0 to disable retries.
|
|
26
|
+
DEFAULT_MAX_RETRIES = 3
|
|
27
|
+
RETRY_BACKOFF = [0.5, 1.0, 2.0].freeze
|
|
28
|
+
|
|
29
|
+
def self.max_retries
|
|
30
|
+
return @max_retries if defined?(@max_retries)
|
|
31
|
+
|
|
32
|
+
@max_retries = DEFAULT_MAX_RETRIES
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.max_retries=(val)
|
|
36
|
+
@max_retries = val
|
|
37
|
+
end
|
|
38
|
+
|
|
25
39
|
# Searches +query+ and returns the results as numbered markdown
|
|
26
40
|
# ("No results found." when SearXNG found nothing). Raises on
|
|
27
41
|
# connection/HTTP failures — the caller decides how to surface them.
|
|
@@ -30,18 +44,30 @@ module Ask
|
|
|
30
44
|
end
|
|
31
45
|
|
|
32
46
|
# The raw result list: { url:, title:, content: } entries from the
|
|
33
|
-
# results and infoboxes, deduplicated by url.
|
|
47
|
+
# results and infoboxes, deduplicated by url. Retries up to
|
|
48
|
+
# max_retries times on connection/HTTP failures with exponential
|
|
49
|
+
# backoff. Set max_retries to 0 to disable.
|
|
34
50
|
def self.search_results(query)
|
|
35
51
|
uri = URI("#{searxng_url}/search?q=#{URI.encode_www_form_component(query)}&format=json")
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
retries = max_retries || 0
|
|
53
|
+
attempt = 0
|
|
54
|
+
begin
|
|
55
|
+
attempt += 1
|
|
56
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
57
|
+
http.open_timeout = 5
|
|
58
|
+
http.read_timeout = 10
|
|
59
|
+
req = Net::HTTP::Get.new(uri)
|
|
60
|
+
req["User-Agent"] = "ask-web-search/#{Ask::WebSearch::VERSION}"
|
|
61
|
+
res = http.request(req)
|
|
62
|
+
raise "SearXNG returned #{res.code}: #{res.body}" unless res.code.start_with?("2")
|
|
43
63
|
|
|
44
|
-
|
|
64
|
+
parse_results(JSON.parse(res.body))
|
|
65
|
+
rescue StandardError
|
|
66
|
+
raise if attempt > retries
|
|
67
|
+
|
|
68
|
+
sleep RETRY_BACKOFF[[attempt - 1, RETRY_BACKOFF.size - 1].min]
|
|
69
|
+
retry
|
|
70
|
+
end
|
|
45
71
|
end
|
|
46
72
|
|
|
47
73
|
def self.parse_results(data)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ask-web-search
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kaka Ruto
|
|
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
116
116
|
- !ruby/object:Gem::Version
|
|
117
117
|
version: '0'
|
|
118
118
|
requirements: []
|
|
119
|
-
rubygems_version: 4.0.
|
|
119
|
+
rubygems_version: 4.0.18
|
|
120
120
|
specification_version: 4
|
|
121
121
|
summary: Web search library for the ask-rb ecosystem
|
|
122
122
|
test_files: []
|