Module: Eat::ObjectExtensions

Defined in:
lib/eat.rb

Instance Method Summary collapse

Instance Method Details

#eat(url, options = {}) ⇒ Object

url can be filesystem or http/https

Options:

  • :timeout in seconds

  • :limit is characters (bytes in Ruby 1.8)

  • :openssl_verify_mode set to ‘none’ if you don’t want to verify SSL certificates

Example:

eat('http://brighterplanet.com')                 #=> '...'
eat('http://brighterplanet.com', :timeout => 10) #=> '...'
eat('http://brighterplanet.com', :limit => 1)    #=> '.'


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/eat.rb', line 21

def eat(url, options = {})
  limit = options.fetch(:limit, ::Infinity)
  
  uri = ::URI.parse url.to_s

  body = []
  read_so_far = 0

  case uri.scheme

  when 'file', nil
    chunk_size = limit < 1_048_576 ? limit : 1_048_576
    ::File.open(uri.path, 'r') do |f|
      while chunk = f.read(chunk_size)
        break if read_so_far > limit
        read_so_far += chunk_size
        body << chunk
      end
    end

  when 'http', 'https'
    require 'httpclient'
    timeout = options.fetch(:timeout, 2)
    openssl_verify_mode = options.fetch(:openssl_verify_mode, ::OpenSSL::SSL::VERIFY_PEER)
    if openssl_verify_mode == 'none'
      openssl_verify_mode = ::OpenSSL::SSL::VERIFY_NONE
    end
    http = ::HTTPClient.new
    http.redirect_uri_callback = ::Proc.new { |uri, res| ::URI.parse(res.header['location'][0]) }
    http.transparent_gzip_decompression = true
    http.receive_timeout = timeout
    if uri.scheme == 'https'
      http.ssl_config.verify_mode = openssl_verify_mode
    end
    catch :stop do
      http.get_content(uri.to_s) do |chunk|
        body << chunk
        read_so_far += chunk.length
        throw :stop if read_so_far > limit
      end
    end
  end

  limit == ::Infinity ? body.join : body.join[0...limit]
end