Class: Edgar::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/edgar/http_client.rb

Overview

Faraday-based HTTP client with rate limiting, retries, and gzip handling.

Defined Under Namespace

Classes: RateLimitError, SslVerificationError

Constant Summary collapse

GZIP_MAGIC =
[0x1f, 0x8b].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate_limit: Config::EDGAR_RATE_LIMIT_PER_SEC, timeout: Config::DEFAULT_TIMEOUT, connect_timeout: Config::DEFAULT_CONNECT_TIMEOUT, retries: 5) ⇒ HTTPClient

Returns a new instance of HTTPClient.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/edgar/http_client.rb', line 17

def initialize(
  rate_limit: Config::EDGAR_RATE_LIMIT_PER_SEC,
  timeout: Config::DEFAULT_TIMEOUT,
  connect_timeout: Config::DEFAULT_CONNECT_TIMEOUT,
  retries: 5
)
  @rate_limit = rate_limit
  @min_interval = 1.0 / rate_limit
  @last_request_time = 0.0
  @mutex = Mutex.new
  @retries = retries

  @connection = build_connection(timeout, connect_timeout)
end

Instance Attribute Details

#last_request_timeObject (readonly)

Returns the value of attribute last_request_time.



15
16
17
# File 'lib/edgar/http_client.rb', line 15

def last_request_time
  @last_request_time
end

Instance Method Details

#download_json(url) ⇒ Object



45
46
47
48
49
# File 'lib/edgar/http_client.rb', line 45

def download_json(url)
  response = get(url)
  body = decompress_body(url, response.body, response.headers)
  JSON.parse(body)
end

#download_text(url) ⇒ Object



40
41
42
43
# File 'lib/edgar/http_client.rb', line 40

def download_text(url)
  response = get(url)
  decompress_body(url, response.body, response.headers)
end

#get(url, **options) ⇒ Object



32
33
34
# File 'lib/edgar/http_client.rb', line 32

def get(url, **options)
  request(:get, url, **options)
end

#post(url, body: nil, **options) ⇒ Object



36
37
38
# File 'lib/edgar/http_client.rb', line 36

def post(url, body: nil, **options)
  request(:post, url, body: body, **options)
end