ruby - Ruby API response - how to action
问题描述
Learning Ruby & APIs. Practicing with the Uber API. Wrote a script to estimate the price of a ride.
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
the output of estimate has the format #<Uber::Price:0x00007fc663821b90>
. I run estimate.class
and it's an array. I run estimate[0].class
and I get Uber::Price
. How can I extract the values that I should be getting from Uber's API response? [0]
[0] https://developer.uber.com/docs/riders/references/api/v1.2/estimates-price-get#response
解决方案
您正在通过库与 API 交谈,通常您会遵循该库uber-ruby的文档。
不幸的是,该库没有记录 an 的Uber::Price
作用。可以肯定的是,Uber::Price 具有与 API 文档中相同的字段。在 Uber::Price 的代码中达到顶峰,我们看到这基本上是正确的。
attr_accessor :product_id, :currency_code, :display_name,
:estimate, :low_estimate, :high_estimate,
:surge_multiplier, :duration, :distance
您可以使用 访问 API 字段estimate.field
。例如,要查看所有估计和持续时间...
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
推荐阅读
- asp.net-mvc - Ninject 正在重用明确定义的实例 InTransientScope()
- java - java arraylist中的调用方法,其中包含由具有通用参数的类创建的对象
- java - Java:将类名传递给函数
- node.js - 使用 Express JS 实现搜索和表单提交的最有效方法
- c# - 如何在实例化(克隆)屏幕上统一使用视频播放器播放视频?
- meteor - 从包 akyrum:vue-component 加载插件 vue-componen 时,找不到模块
- pandas - 使用 dask 从 Hive 读取数据
- html - 当另一个 div 悬停在上面时使 div 向上滑动
- sql - DB2 查询中的 DATEDIFF
- equalsverifier - EqualsVerifier 断言错误:重要字段:equals 不使用
,或者它是无状态的