Class: MetaInspector::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/meta_inspector/request.rb

Overview

Makes the request to the server

Instance Method Summary collapse

Constructor Details

#initialize(initial_url, options = {}) ⇒ Request

Returns a new instance of Request.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/meta_inspector/request.rb', line 13

def initialize(initial_url, options = {})
  @url                = initial_url

  fail MetaInspector::RequestError.new('URL must be HTTP') unless @url.url =~ /http[s]?:\/\//i

  @allow_redirections = options[:allow_redirections]
  @connection_timeout = options[:connection_timeout]
  @read_timeout       = options[:read_timeout]
  @retries            = options[:retries]
  @encoding           = options[:encoding]
  @headers            = options[:headers]
  @faraday_options    = options[:faraday_options] || {}
  @faraday_http_cache = options[:faraday_http_cache]

  response            # request early so we can fail early
end

Instance Method Details

#content_typeObject


42
43
44
45
# File 'lib/meta_inspector/request.rb', line 42

def content_type
  return nil if response.headers['content-type'].nil?
  response.headers['content-type'].split(';')[0] if response
end

#readObject


33
34
35
36
37
38
39
40
# File 'lib/meta_inspector/request.rb', line 33

def read
  return unless response
  body = response.body
  body = body.encode!(@encoding, @encoding, :invalid => :replace) if @encoding
  body.tr("\000", '')
rescue ArgumentError => e
  raise MetaInspector::RequestError.new(e)
end

#responseObject


47
48
49
50
51
52
53
# File 'lib/meta_inspector/request.rb', line 47

def response
  @response ||= fetch
rescue Faraday::TimeoutError => e
  raise MetaInspector::TimeoutError.new(e)
rescue Faraday::ConnectionFailed, Faraday::SSLError, URI::InvalidURIError, FaradayMiddleware::RedirectLimitReached => e
  raise MetaInspector::RequestError.new(e)
end