class Borrower::Content::Item

Public Class Methods

new(path) click to toggle source
# File lib/borrower/content.rb, line 27
def initialize path
  @path = path
end

Public Instance Methods

content() click to toggle source
# File lib/borrower/content.rb, line 43
def content
  if remote? && exists?
    @_response.body
  else
    IO.read( @path )
  end
end
exists?() click to toggle source
# File lib/borrower/content.rb, line 35
def exists?
  if remote?
    return ( fetch_from_remote(@path).msg.include? "OK" )
  else
    return File.exists? @path
  end
end
remote?() click to toggle source
# File lib/borrower/content.rb, line 31
def remote?
  @_remote ||= is_remote?
end

Private Instance Methods

fetch_from_remote(path, limit=10) click to toggle source
# File lib/borrower/content.rb, line 60
def fetch_from_remote path, limit=10
  return @_response unless @_response.nil?

  raise Error, 'HTTP redirect too deep' if limit == 0
  response = get_response(path)

  case response
  when Net::HTTPSuccess then @_response = response
  when Net::HTTPRedirection then fetch_from_remote(response['location'], limit-1 )
  else
    @_response = response
  end
end
get_response(path) click to toggle source
# File lib/borrower/content.rb, line 74
def get_response path
  uri = URI.parse(path)
  request = Net::HTTP.new(uri.host, uri.port)
  request.use_ssl = ( uri.scheme == "https" )

  return request.get(uri.request_uri)
end
is_remote?() click to toggle source
# File lib/borrower/content.rb, line 53
def is_remote?
  if @path.match /http[s]?:\/\//
    return true
  end
  return false
end