Search code examples
rubyhttptruetypeopen-uri

Downloading a ttf file in Ruby


link = 'http://protext.hackerrank.com/static/gen/font_a_21fb3d1d1a91a7e80dff456205f3380b.ttf'

If you visit this URL in your browser it should download a .ttf file. However, I haven't been able to find a way to download said file using Ruby, or even access the information within.

Things I've tried:

require 'nokogiri'
require 'open-uri'
require 'net/http'

-

File.open('font_0.ttf', 'wb') do |fo|
  fo.write open(url).read
end

-

download = open(url)
IO.copy_stream(download, '~/font_0.ttf')

-

Net::HTTP.start("http://protext.hackerrank.com") { |http|
  resp = http.get("/static/gen/font_a_21fb3d1d1a91a7e80dff456205f3380b.ttf")

  p resp.body
}

I've also tried most of the above with Nokogiri's parse.

Any help is appreciated, thank you!


Solution

  • According to some local test, you need to specify a header field Cookie: X-VALID=TRUE to pass the server check.

    require 'open-uri'
    data = open("http://protext.hackerrank.com/static/gen/font_a_21fb3d1d1a91a7e80dff456205f3380b.ttf", 
                "Cookie" => "X-VALID=TRUE").read