Skip to content

Commit

Permalink
allow user to override timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
one-m1nd committed Mar 7, 2024
1 parent b6adea6 commit 76b6a9c
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [Unreleased]

## [0.3.0] - 2024-03-07
- allow user to override http timeout

## [0.2.0] - 2024-02-29
- allow user to override logger

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Plants::Log.instance = logger # Your logger here
### Endpoints
All methods return an instance of [`HTTP::Response`](https://github.com/httprb/http/wiki/Response-Handling)

The HTTP timeout is by default 10 seconds, you can override this by:
```ruby
Plants.timeout = 5 # new value here
```

#### Corrections
```ruby
# List
Expand Down
6 changes: 6 additions & 0 deletions lib/plants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def token=(new_value)
client.config.token = new_value
end

# @param new_value [Integer]
# @return [void]
def timeout=(new_value)
client.config.timeout = new_value
end

# @return [Plants::Config]
def config
client.config
Expand Down
2 changes: 1 addition & 1 deletion lib/plants/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get(resource, params: {})
# @return [::HTTP::Client]
def http
HTTP
.timeout(10)
.timeout(config.timeout)
.use(logging: { logger: Log.instance })
.headers({ 'User-Agent' => "Plants #{Plants::VERSION} ruby-#{RUBY_VERSION}" })
end
Expand Down
5 changes: 4 additions & 1 deletion lib/plants/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ module Plants
#
# @!attribute token
# @return [String]
# @!attribute timeout
# @return [Integer]
#
class Config
def initialize
@token = nil
@timeout = 10
end
attr_accessor :token
attr_accessor :token, :timeout
end
end
2 changes: 1 addition & 1 deletion lib/plants/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module Plants
# @return [String]
VERSION = "0.2.0"
VERSION = "0.3.0"
end
1 change: 1 addition & 0 deletions spec/plants/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

it do
expect(config).to receive(:token).and_return(nil)
expect(config).to receive(:timeout).and_return(10)
expect(subject).to be_instance_of(HTTP::Response)
expect(a_request(:get, "#{Plants::Client::URL}/plants")).to have_been_made
end
Expand Down
17 changes: 17 additions & 0 deletions spec/plants/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,21 @@
expect(config.token).to eql('secret')
end
end

describe '#timeout' do
subject { config.timeout }

it do
expect(subject).to be_instance_of(Integer)
end
end

describe '#timeout=' do
subject { config.timeout = 5 }

it do
subject
expect(config.timeout).to eql(5)
end
end
end
11 changes: 11 additions & 0 deletions spec/plants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
end
end

describe '.timeout=' do
after(:each) { Plants.timeout = 10 }

subject { Plants.timeout = 5 }

it do
subject
expect(Plants.config.timeout).to eql 5
end
end

describe '.config' do
subject { Plants.config }

Expand Down

0 comments on commit 76b6a9c

Please sign in to comment.