Ruby
[ruby] http call 예시
'김용환'
2017. 1. 6. 16:16
Ruby로 작성한 아주 간단한 Http 호출 예시 코드이다.
require 'net/http'
require 'uri'
require 'json'
class Host
def initialize(ip)
@ip = ip
end
def http_req
uri = URI('http://host.google.io/views?ip=' + @ip)
http = Net::HTTP.new('host.google.io', 80)
req = Net::HTTP::Get.new(uri.request_uri)
http.open_timeout = 5
http.read_timeout = 20
response = http.request(req)
if response.code.to_i == 200
return response.body
else
raise Exception.new("#{response.code} to #{uri}\n#{response.body}")
end
end
def get_host
json = JSON.parse(http_req)
json["hostname"]+ ", " + json["name"]
end
end