class Funky::HTML::Page

Public Instance Methods

get(video_id:) click to toggle source
# File lib/funky/html/page.rb, line 5
def get(video_id:)
  body = response_for(video_id).body

  if body.include? '<title id="pageTitle">Facebook</title>'
    raise ContentNotFound, 'Please double check the ID and try again.'
  else
    body
  end
end

Private Instance Methods

response_for(video_id) click to toggle source
# File lib/funky/html/page.rb, line 17
def response_for(video_id)
  uri = uri_for video_id
  request = Net::HTTP::Get.new(uri.request_uri)
  response = Net::HTTP.start(uri.host, 443, use_ssl: true) do |http|
    http.request request
  end
  if response.is_a? Net::HTTPRedirection
    request = Net::HTTP::Get.new URI.parse(URI.encode(response.header['location']))
    response = Net::HTTP.start(uri.host, 443, use_ssl: true) do |http|
      http.request request
    end
  end
  response
rescue *server_errors => e
  raise ConnectionError, e.message
end
server_errors() click to toggle source
# File lib/funky/html/page.rb, line 40
def server_errors
  [
    OpenSSL::SSL::SSLError,
    Errno::ETIMEDOUT,
    Errno::EHOSTUNREACH,
    Errno::ENETUNREACH,
    Errno::ECONNRESET,
    Net::OpenTimeout,
    SocketError
  ]
end
uri_for(video_id) click to toggle source
# File lib/funky/html/page.rb, line 34
def uri_for(video_id)
  URI::HTTPS.build host:  'www.facebook.com',
                   path:  '/video.php',
                   query: "v=#{video_id}&locale=en_US"
end