Skip to content

Commit a4572ee

Browse files
committed
feat: add configurable timeout for API requests
1 parent 6ef92d6 commit a4572ee

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ You can get a token for free by [creating a Crawlbase account](https://crawlbase
3434
api = Crawlbase::API.new(token: 'YOUR_TOKEN')
3535
```
3636

37+
By default, the timeout for API requests is set to 90 seconds. You can configure a custom timeout by passing a `timeout` option during initialization.
38+
39+
```ruby
40+
api = Crawlbase::API.new(token: 'YOUR_TOKEN', timeout: 120)
41+
```
42+
3743
### GET requests
3844

3945
Pass the url that you want to scrape plus any options from the ones available in the [API documentation](https://crawlbase.com/dashboard/docs).
@@ -357,7 +363,7 @@ The gem is available as open source under the terms of the [MIT License](http://
357363

358364
## Code of Conduct
359365

360-
Everyone interacting in the Crawlbase projects codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/crawlbase-source/crawlbase-ruby/blob/master/CODE_OF_CONDUCT.md).
366+
Everyone interacting in the Crawlbase project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/crawlbase-source/crawlbase-ruby/blob/master/CODE_OF_CONDUCT.md).
361367

362368
---
363369

crawlbase.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
2424
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2525
spec.require_paths = ["lib"]
2626

27+
spec.add_dependency "rexml"
28+
2729
spec.add_development_dependency "rspec", "~> 3.2"
2830
spec.add_development_dependency "webmock", "~> 3.4"
2931
spec.add_development_dependency "bundler", "~> 2.0"

lib/crawlbase/api.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@
66

77
module Crawlbase
88
class API
9-
attr_reader :token, :body, :status_code, :original_status, :pc_status, :url, :storage_url
9+
attr_reader :token, :body, :status_code, :original_status, :pc_status, :url, :storage_url, :timeout
1010

1111
INVALID_TOKEN = 'Token is required'
1212
INVALID_URL = 'URL is required'
13+
DEFAULT_TIMEOUT = 90
1314

1415
def initialize(options = {})
1516
raise INVALID_TOKEN if options[:token].nil?
1617

1718
@token = options[:token]
19+
@timeout = options.fetch(:timeout, DEFAULT_TIMEOUT)
1820
end
1921

2022
def get(url, options = {})
2123
raise INVALID_URL if url.empty?
2224

2325
uri = prepare_uri(url, options)
24-
25-
response = Net::HTTP.get_response(uri)
26+
http = build_http(uri)
27+
request = Net::HTTP::Get.new(uri.request_uri)
28+
response = http.request(request)
2629

2730
prepare_response(response, options[:format])
2831

@@ -33,10 +36,7 @@ def post(url, data, options = {})
3336
raise INVALID_URL if url.empty?
3437

3538
uri = prepare_uri(url, options)
36-
37-
http = Net::HTTP.new(uri.host, uri.port)
38-
39-
http.use_ssl = true
39+
http = build_http(uri)
4040

4141
content_type = options[:post_content_type].to_s.include?('json') ? { 'Content-Type': 'text/json' } : nil
4242

@@ -57,6 +57,14 @@ def post(url, data, options = {})
5757

5858
private
5959

60+
def build_http(uri)
61+
http = Net::HTTP.new(uri.host, uri.port)
62+
http.use_ssl = true
63+
http.open_timeout = @timeout
64+
http.read_timeout = @timeout
65+
http
66+
end
67+
6068
def base_url
6169
'https://api.crawlbase.com'
6270
end

spec/api_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
expect(Crawlbase::API.new(token: 'test').token).to eql('test')
1111
end
1212

13+
it 'sets default timeout to 90 seconds' do
14+
expect(Crawlbase::API.new(token: 'test').timeout).to eql(90)
15+
end
16+
17+
it 'sets a custom timeout' do
18+
expect(Crawlbase::API.new(token: 'test', timeout: 120).timeout).to eql(120)
19+
end
20+
1321
describe '#get' do
1422
it 'sends an get request to Crawlbase API' do
1523
stub_request(:get, 'https://api.crawlbase.com/?token=test&url=http%3A%2F%2Fhttpbin.org%2Fanything%3Fparam1%3Dx%26params2%3Dy').
@@ -28,6 +36,14 @@
2836
expect(response.url).to eql('http://httpbin.org/anything?param1=x&params2=y')
2937
expect(response.body).to eql('body')
3038
end
39+
40+
it 'raises a timeout error' do
41+
stub_request(:get, 'https://api.crawlbase.com/?token=test_with_timeout&url=http%3A%2F%2Fhttpbin.org%2Fdelay%2F3').to_timeout
42+
43+
api = Crawlbase::API.new(token: 'test_with_timeout', timeout: 2)
44+
45+
expect { api.get('http://httpbin.org/delay/3') }.to raise_error(Net::OpenTimeout)
46+
end
3147
end
3248

3349
describe '#post' do
@@ -68,6 +84,14 @@
6884
expect(response.url).to eql('http://httpbin.org/anything?param1=x&params2=y')
6985
expect(response.body).to eql('body')
7086
end
87+
88+
it 'raises a timeout error' do
89+
stub_request(:post, 'https://api.crawlbase.com/?token=test_with_timeout&url=http%3A%2F%2Fhttpbin.org%2Fdelay%2F3').to_timeout
90+
91+
api = Crawlbase::API.new(token: 'test_with_timeout', timeout: 2)
92+
93+
expect { api.post('http://httpbin.org/delay/3', {}) }.to raise_error(Net::OpenTimeout)
94+
end
7195
end
7296

73-
end
97+
end

0 commit comments

Comments
 (0)