gpt4 book ai didi

Ruby API 响应 - 如何操作

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:50 25 4
gpt4 key购买 nike

学习 Ruby 和 API。使用 Uber API 练习。编写了一个脚本来估算乘车的价格。

require 'uber'
require 'geocoder'

def ride()

# print "start? "
# location_start = gets.chomp
# print "finish? "
# location_end = gets.chomp

coordinates_start = Geocoder.coordinates("dublin") # gets a location for start and transforms into lat long
coordinates_end = Geocoder.coordinates("dalkey") # gets a location for start and transforms into lat long

client = Uber::Client.new do |config|
config.server_token = "{SERVER_TOKEN}"
config.sandbox = true
end
estimate = client.price_estimations(start_latitude: coordinates_start[0], start_longitude: coordinates_start[1],
end_latitude: coordinates_end[0], end_longitude: coordinates_end[1])
estimate

end

puts ride

估算的输出格式为#<Uber::Price:0x00007fc663821b90> .我跑 estimate.class它是一个数组。我跑 estimate[0].class我得到 Uber::Price .我如何提取应该从 Uber 的 API 响应中获取的值? [0]

[0] https://developer.uber.com/docs/riders/references/api/v1.2/estimates-price-get#response

最佳答案

您通过库与 API 对话,通常您会遵循该库的文档 uber-ruby .

不幸的是,该库没有记录 Uber::Price 的作用。可以肯定的是,Uber::Price 具有与 API 文档中相同的字段。 Peaking at the code for Uber::Price我们看到这基本上是正确的。

attr_accessor :product_id, :currency_code, :display_name,
:estimate, :low_estimate, :high_estimate,
:surge_multiplier, :duration, :distance

您可以使用 estimate.field 访问 API 字段。例如,要查看所有估算值和持续时间...

estimates = ride()

estimates.each do |estimate|
puts "Taking a #{estimate.display_name} will cost #{estimate.estimate} #{estimate.currency_code} and take #{estimate.duration / 60} minutes"
end

关于Ruby API 响应 - 如何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50594916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com