从 URI::Generic 对象获取路径和查询
Get Path and Query from URI::Generic object
我正在尝试了解 URI::Generic
对象。但是我找不到如何将路径和查询提取到 API.
中的一个联合字符串中
如果我有这个对象:
#<URI::Generic /api/trading/model/123456789?api_key=12345>
我怎样才能return这个:
"/api/trading/model/123456789?api_key=12345"
似乎我应该能够在 URI
api 上调用一些实例方法,但我似乎找不到任何东西。有人可以帮忙吗?
用法:
def get(path, params)
uri = URI(path)
if params.any?
uri.query = URI.encode_www_form(params)
end
# PASS SRTRING INTO REQUEST
request = Net::HTTP::Get.new(uri)
execute_request(request)
end
您正在寻找 #to_s
方法:
(irb)> uri
#<URI::Generic /api/trading/model/123456789?api_key=12345>
(irb)> uri.to_s
"/api/trading/model/123456789?api_key=12345"
记住,在 ruby 中一切都是对象,您可以随时调用 #methods
来获取该对象所有导出函数的列表。
我正在尝试了解 URI::Generic
对象。但是我找不到如何将路径和查询提取到 API.
如果我有这个对象:
#<URI::Generic /api/trading/model/123456789?api_key=12345>
我怎样才能return这个:
"/api/trading/model/123456789?api_key=12345"
似乎我应该能够在 URI
api 上调用一些实例方法,但我似乎找不到任何东西。有人可以帮忙吗?
用法:
def get(path, params)
uri = URI(path)
if params.any?
uri.query = URI.encode_www_form(params)
end
# PASS SRTRING INTO REQUEST
request = Net::HTTP::Get.new(uri)
execute_request(request)
end
您正在寻找 #to_s
方法:
(irb)> uri
#<URI::Generic /api/trading/model/123456789?api_key=12345>
(irb)> uri.to_s
"/api/trading/model/123456789?api_key=12345"
记住,在 ruby 中一切都是对象,您可以随时调用 #methods
来获取该对象所有导出函数的列表。