Class: LinkPreview::HTTPClient
- Inherits:
-
Object
- Object
- LinkPreview::HTTPClient
- Defined in:
- lib/link_preview/http_client.rb
Instance Method Summary collapse
- #connection ⇒ Object
- #get(uri, options = {}) ⇒ Object
-
#initialize(config) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
Constructor Details
#initialize(config) ⇒ HTTPClient
Returns a new instance of HTTPClient.
65 66 67 |
# File 'lib/link_preview/http_client.rb', line 65 def initialize(config) @config = config end |
Instance Method Details
#connection ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/link_preview/http_client.rb', line 81 def connection @connection ||= Faraday.new do |builder| builder.[:timeout] = @config.timeout builder.[:open_timeout] = @config.open_timeout builder.use ExtraEnv builder.use Faraday::FollowRedirects, limit: @config.max_redirects if @config.follow_redirects builder.use NormalizeURI builder.use ForceUTF8Body @config.middleware.each { |middleware| builder.use middleware } builder.use @config.http_adapter end end |
#get(uri, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/link_preview/http_client.rb', line 69 def get(uri, = {}) with_extra_env() do connection.get(uri).tap do |response| response.extend ResponseWithURL response.url = uri end end rescue => e @config.error_handler.call(e) Faraday::Response.new(status: 500) end |