class Threatinator::Fetchers::Http

Attributes

url[R]

Public Class Methods

new(opts = {}) click to toggle source

@param [Hash] opts An options hash. @option opts [Addressable::URI] :url The URL that is to be fetched

(required)
Calls superclass method Threatinator::Fetcher::new
# File lib/threatinator/fetchers/http.rb, line 14
def initialize(opts = {})
  @url = opts.delete(:url) or raise ArgumentError.new("Missing :url")
  super(opts)
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method Threatinator::Fetcher#==
# File lib/threatinator/fetchers/http.rb, line 19
def ==(other)
  @url == other.url && super(other)
end
fetch() click to toggle source

@return [IO] an IO-style object. @raise [Threatinator::Exceptions::FetchFailed] if the fetch fails

# File lib/threatinator/fetchers/http.rb, line 25
def fetch
  tempfile = Tempfile.new("threatinator_http")
  request = Typhoeus::Request.new(@url,
                                  ssl_verifypeer: false,
                                  followlocation: true,
                                  forbid_reuse: true
                                 )
  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