Class: SimpleRSS::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-rss/http_client.rb

Constant Summary collapse

ACCEPT =
"application/feed+json, application/rss+xml, application/atom+xml, application/json, application/xml, text/xml, */*".freeze
REDIRECT_CODES =
%w[301 302 303 307 308].freeze
CROSS_ORIGIN_HEADERS =
%w[accept accept-language user-agent].freeze
PROTECTED_HEADERS =
%w[host connection proxy-authorization proxy-connection accept-encoding range transfer-encoding content-length te trailer upgrade
expect].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ HTTPClient

Returns a new instance of HTTPClient.



25
26
27
28
29
30
31
32
33
34
# File 'lib/simple-rss/http_client.rb', line 25

def initialize(options)
  @options = options.dup
  @headers = (@options[:headers] || {}).dup
  @policy = @options.key?(:network_policy) ? SimpleRSS::RequestPolicy.new(@options[:network_policy]) : nil
  @remaining_bytes = @options.fetch(:max_bytes, 2 * 1024 * 1024)
  @remaining_wire_bytes = @remaining_bytes
  @redirect_limit = @options.fetch(:max_redirects, 5)
  @timeout = @options.fetch(:timeout, @policy ? 10 : nil)
  validate_options
end

Instance Method Details

#get(url) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/simple-rss/http_client.rb', line 37

def get(url)
  return perform(URI.parse(url)) unless @policy

  Timeout.timeout(@timeout, SimpleRSS::RequestTimeout, "HTTP operation exceeded its timeout") do
    perform(SimpleRSS::RequestPolicy.parse_url(url))
  end
rescue Timeout::Error
  raise unless @policy

  raise SimpleRSS::RequestTimeout, "HTTP operation exceeded its timeout"
rescue IOError, SystemCallError, SocketError, OpenSSL::SSL::SSLError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Resolv::ResolvError, Zlib::Error => e
  raise unless @policy

  raise SimpleRSS::RequestError, "HTTP transport failed: #{e.message}"
end