desearch 0.1.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 +7 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE +21 -0
- data/README.md +49 -0
- data/lib/desearch/version.rb +5 -0
- data/lib/desearch.rb +102 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5ce4971d99b0416a23493fe18b132329f3fd3f3be6cd236673f6d6fa83822b41
|
|
4
|
+
data.tar.gz: 14031e333f24c74c4791c2be522d2dd79f53757d7c24419961dd2898989d6e4d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: cc6efaeca78e620700cf624fecd87a9fc2be1cb33853bd37fd2fd0561f4c24373bad9a9f4e0413c86f8bdaf038767b3a3f61311489fa80fd095a412a69e38543
|
|
7
|
+
data.tar.gz: a31c000e243d44a6abc9e19f3fe396cfafbec5d127be88303fbb425744404b500d403f25408dc4e022a55377229d4bbf8783df0c225359673c50ec0579050441
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Desearch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Desearch Ruby SDK
|
|
2
|
+
|
|
3
|
+
Ruby client for the [Desearch Search API](https://www.desearch.ai/docs/guide/apis/search-api). The gem requires Ruby 3.4 or later.
|
|
4
|
+
|
|
5
|
+
## Build and install
|
|
6
|
+
|
|
7
|
+
Run from the repository root.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
gem build desearch.gemspec
|
|
11
|
+
gem install --local --no-document ./desearch-0.1.0.gem
|
|
12
|
+
ruby -rdesearch -e 'puts Desearch::VERSION'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Search
|
|
16
|
+
|
|
17
|
+
Set `DESEARCH_API_KEY` in your environment using your [Desearch API key](https://www.desearch.ai/docs/guide/introduction/api-keys). Keep the key on your server. Search requests are billed according to [Desearch pricing](https://www.desearch.ai/pricing).
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
ruby -rdesearch <<'RUBY'
|
|
21
|
+
client = Desearch::Client.new
|
|
22
|
+
response = client.search(
|
|
23
|
+
"RubyGems guide to building a Ruby gem",
|
|
24
|
+
count: 10,
|
|
25
|
+
include_domains: ["guides.rubygems.org"]
|
|
26
|
+
)
|
|
27
|
+
response.fetch("results").each do |result|
|
|
28
|
+
puts result.fetch("title"), result.fetch("url")
|
|
29
|
+
end
|
|
30
|
+
RUBY
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`search` returns the response as a hash with string keys, including `results` and the billing fields. See the [endpoint reference](https://www.desearch.ai/docs/api-reference/post-search) for all search options and response fields.
|
|
34
|
+
|
|
35
|
+
`Desearch::HTTPError#status` reports the HTTP status when the API rejects a request. The client raises `Desearch::NetworkError` for transport failures and `Desearch::ResponseError` for unreadable or invalid search results.
|
|
36
|
+
|
|
37
|
+
## Tests
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
ruby -Ilib test/desearch_test.rb
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Support
|
|
44
|
+
|
|
45
|
+
Use [Desearch support](https://www.desearch.ai/contact) to report a problem. Include the gem and Ruby versions and the HTTP status if available.
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT. See [LICENSE](LICENSE).
|
data/lib/desearch.rb
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "openssl"
|
|
6
|
+
require_relative "desearch/version"
|
|
7
|
+
|
|
8
|
+
module Desearch
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
class NetworkError < Error; end
|
|
11
|
+
class ResponseError < Error; end
|
|
12
|
+
|
|
13
|
+
class HTTPError < Error
|
|
14
|
+
attr_reader :status
|
|
15
|
+
|
|
16
|
+
def initialize(status)
|
|
17
|
+
@status = status
|
|
18
|
+
super("Desearch Search returned HTTP #{status}.")
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class Client
|
|
23
|
+
ENDPOINT = URI("https://api.desearch.ai/search").freeze
|
|
24
|
+
private_constant :ENDPOINT
|
|
25
|
+
|
|
26
|
+
def initialize(api_key: ENV["DESEARCH_API_KEY"])
|
|
27
|
+
unless api_key.is_a?(String) && api_key.ascii_only? && /\A[!-~]+\z/.match?(api_key)
|
|
28
|
+
raise ArgumentError, "Set DESEARCH_API_KEY or pass a valid API key."
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
@api_key = api_key.dup.freeze
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def inspect
|
|
35
|
+
"#<#{self.class}>"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def pretty_print(printer)
|
|
39
|
+
printer.text(inspect)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def search(query, mode: nil, count: nil, start_date: nil, end_date: nil, highlights: nil,
|
|
43
|
+
page_text: nil, summary: nil, include_domains: nil, exclude_domains: nil)
|
|
44
|
+
unless query.is_a?(String) && query.valid_encoding? && query.length.between?(1, 2000)
|
|
45
|
+
raise ArgumentError, "Search query must be a string between 1 and 2,000 characters."
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
request = Net::HTTP::Post.new(ENDPOINT)
|
|
49
|
+
request["Authorization"] = @api_key
|
|
50
|
+
request["Content-Type"] = "application/json"
|
|
51
|
+
request["Accept"] = "application/json"
|
|
52
|
+
request.body = encode(query: query, mode: mode, count: count, start_date: start_date,
|
|
53
|
+
end_date: end_date, highlights: highlights, page_text: page_text,
|
|
54
|
+
summary: summary, include_domains: include_domains,
|
|
55
|
+
exclude_domains: exclude_domains)
|
|
56
|
+
|
|
57
|
+
response = send_request(request)
|
|
58
|
+
status = response.code.to_i
|
|
59
|
+
raise HTTPError.new(status), cause: nil unless status == 200
|
|
60
|
+
|
|
61
|
+
parse(response.body)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def encode(options)
|
|
67
|
+
JSON.generate(options.compact)
|
|
68
|
+
rescue JSON::GeneratorError
|
|
69
|
+
raise ArgumentError, "Search parameters must be valid JSON values.", cause: nil
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def send_request(request)
|
|
73
|
+
http = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port, nil)
|
|
74
|
+
http.use_ssl = true
|
|
75
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
|
76
|
+
http.open_timeout = 5
|
|
77
|
+
http.read_timeout = 30
|
|
78
|
+
http.write_timeout = 10
|
|
79
|
+
http.max_retries = 0
|
|
80
|
+
http.start { |connection| connection.request(request) }
|
|
81
|
+
rescue SocketError, SystemCallError, IOError, Timeout::Error, OpenSSL::SSL::SSLError,
|
|
82
|
+
Net::ProtocolError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError
|
|
83
|
+
raise NetworkError, "Could not complete the Desearch Search request.", cause: nil
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def parse(body)
|
|
87
|
+
payload = JSON.parse(body.to_s)
|
|
88
|
+
results = payload["results"] if payload.is_a?(Hash)
|
|
89
|
+
unless results.is_a?(Array) && results.all? { |result| valid_result?(result) }
|
|
90
|
+
raise ResponseError, "Desearch Search returned an invalid result list."
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
payload
|
|
94
|
+
rescue JSON::ParserError
|
|
95
|
+
raise ResponseError, "Desearch Search returned invalid JSON.", cause: nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def valid_result?(result)
|
|
99
|
+
result.is_a?(Hash) && result["url"].is_a?(String) && result["title"].is_a?(String)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: desearch
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Desearch
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2026-12-03 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Run web searches and read structured results from the Desearch Search
|
|
13
|
+
API.
|
|
14
|
+
email:
|
|
15
|
+
- support@desearch.ai
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- CHANGELOG.md
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/desearch.rb
|
|
24
|
+
- lib/desearch/version.rb
|
|
25
|
+
homepage: https://www.desearch.ai/
|
|
26
|
+
licenses:
|
|
27
|
+
- MIT
|
|
28
|
+
metadata:
|
|
29
|
+
source_code_uri: https://github.com/Desearch-ai/desearch-ruby
|
|
30
|
+
documentation_uri: https://www.desearch.ai/docs/guide/apis/search-api
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '3.4'
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
requirements: []
|
|
45
|
+
rubygems_version: 3.6.9
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: Ruby client for the Desearch Search API
|
|
48
|
+
test_files: []
|