Class: Threatinator::Fetchers::Http

Inherits:
Threatinator::Fetcher show all
Defined in:
lib/threatinator/fetchers/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Threatinator::Fetcher

#eql?

Constructor Details

#initialize(opts = {}) ⇒ Http

Returns a new instance of Http.

Parameters:

  • opts (Hash) (defaults to: {})

    An options hash.

Options Hash (opts):

  • :url (Addressable::URI)

    The URL that is to be fetched (required)



14
15
16
17
# File 'lib/threatinator/fetchers/http.rb', line 14

def initialize(opts = {})
  @url = opts.delete(:url) or raise ArgumentError.new("Missing :url")
  super(opts)
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/threatinator/fetchers/http.rb', line 9

def url
  @url
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
# File 'lib/threatinator/fetchers/http.rb', line 19

def ==(other)
  @url == other.url && super(other)
end

#fetchIO

Returns an IO-style object.

Returns:

  • (IO)

    an IO-style object.

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/threatinator/fetchers/http.rb', line 25

def fetch
  tempfile = Tempfile.new("threatinator_http")
  request = Typhoeus::Request.new(@url, ssl_verifypeer: false)
  request.on_headers do |response|
    if response.response_code != 200

      raise Threatinator::Exceptions::FetchFailed.new("Request failed!")
    end
  end
  request.on_body do |chunk|
    tempfile.write(chunk)
  end
  # Run it
  request.run
  # Reset the IO to the beginning of the file
  tempfile.rewind
  tempfile
end