Bitwise
Bitwise

Reputation: 8461

HTTP request Ruby

I'm doing a tutorial online and I have a challenge to read through a text file and file the one and only name that is a Palindrome. However the file containing the names is in this url http://www.codequizzes.com/challenges/names.txt. I'm pretty new to this http request stuff. How can I require the contents of this text file with a basic ruby program?

Upvotes: 0

Views: 54

Answers (2)

SteveTurczyn
SteveTurczyn

Reputation: 36880

This will copy a remote file to a local file...

require "open-uri"

remote_resource = "http://www.codequizzes.com/challenges/names.txt"

remote_data = open(remote_resource).read

local_file = open("local_names.txt", "w") 

local_file.write(remote_data)
local_file.close

Upvotes: 1

user94559
user94559

Reputation: 60153

How about using Net::HTTP? (There are other libraries, but this one is built in and simple enough.)

require 'net/http'

text = Net::HTTP.get(URI('http://www.codequizzes.com/challenges/names.txt'))

Upvotes: 3

Related Questions