Class: Vangrail::HTTP
- Inherits:
-
Object
- Object
- Vangrail::HTTP
- Defined in:
- lib/vangrail/http.rb
Overview
JSON over Net::HTTP with stdlib only: a guardrail that pulls in a transport stack is a guardrail nobody installs. Every call is bounded by open and read timeouts, because a rail that hangs is worse than a rail that is absent.
Constant Summary collapse
- DEFAULT_OPEN_TIMEOUT =
5- DEFAULT_READ_TIMEOUT =
30
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#read_timeout ⇒ Object
readonly
Returns the value of attribute read_timeout.
-
#retries ⇒ Object
readonly
Returns the value of attribute retries.
Instance Method Summary collapse
- #get_json(path) ⇒ Object
-
#initialize(base_url:, api_key: nil, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, retries: 1, headers: {}) ⇒ HTTP
constructor
A new instance of HTTP.
- #post_json(path, payload) ⇒ Object
-
#reachable?(path) ⇒ Boolean
True when the endpoint answers at all.
Constructor Details
#initialize(base_url:, api_key: nil, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, retries: 1, headers: {}) ⇒ HTTP
Returns a new instance of HTTP.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/vangrail/http.rb', line 17 def initialize(base_url:, api_key: nil, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT, retries: 1, headers: {}) @base_url = base_url.to_s.sub(/\/+\z/, '') raise ArgumentError, 'base_url is required' if @base_url.empty? @api_key = api_key @open_timeout = open_timeout @read_timeout = read_timeout @retries = retries @headers = headers end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
15 16 17 |
# File 'lib/vangrail/http.rb', line 15 def base_url @base_url end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
15 16 17 |
# File 'lib/vangrail/http.rb', line 15 def open_timeout @open_timeout end |
#read_timeout ⇒ Object (readonly)
Returns the value of attribute read_timeout.
15 16 17 |
# File 'lib/vangrail/http.rb', line 15 def read_timeout @read_timeout end |
#retries ⇒ Object (readonly)
Returns the value of attribute retries.
15 16 17 |
# File 'lib/vangrail/http.rb', line 15 def retries @retries end |
Instance Method Details
#get_json(path) ⇒ Object
29 30 31 |
# File 'lib/vangrail/http.rb', line 29 def get_json(path) request(Net::HTTP::Get, path, nil) end |
#post_json(path, payload) ⇒ Object
33 34 35 |
# File 'lib/vangrail/http.rb', line 33 def post_json(path, payload) request(Net::HTTP::Post, path, payload) end |
#reachable?(path) ⇒ Boolean
True when the endpoint answers at all. Used to pick a rail mode without making the caller handle an exception for the ordinary "not running" case.
39 40 41 42 43 44 45 46 |
# File 'lib/vangrail/http.rb', line 39 def reachable?(path) get_json(path) true rescue HTTPError true rescue Error false end |