Class: LinkPreview::HTTPClient

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

Instance Method Summary collapse

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

#connectionObject



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.options[:timeout] = @config.timeout
    builder.options[: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, options = {})
  with_extra_env(options) 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