class Oembed::Http

Attributes

parser[W]

Public Instance Methods

get(path, options = {}) click to toggle source
# File lib/oembed/http.rb, line 14
def get(path, options = {})
  uri        = URI.parse(path)
  connection = establish_connection(uri.host, uri.port)
  response   = connection.get(uri.request_uri)

  case response
  when Net::HTTPRedirection
    follow_redirection(response['location'], options.fetch(:limit, 10))
  when Net::HTTPSuccess
    parser.parse(response.body, response.content_type)
  else
    raise Oembed::ResponseError.new(response), 'HTTP error during request'
  end
end
parser() click to toggle source
# File lib/oembed/http.rb, line 10
def parser
  @parser ||= Oembed::Parser.new
end

Private Instance Methods

establish_connection(host, port) click to toggle source
# File lib/oembed/http.rb, line 31
def establish_connection(host, port)
  Net::HTTP.new(host, port).tap do |instance|
    instance.use_ssl = port == URI::HTTPS::DEFAULT_PORT
  end
end
follow_redirection(location, limit) click to toggle source
# File lib/oembed/http.rb, line 37
def follow_redirection(location, limit)
  if limit > 0
    get(location, limit: limit - 1)
  else
    raise Oembed::RedirectionTooDeepError, 'HTTP redirects too deep'
  end
end