正在乱搞Kickbox的api进行电子邮件验证。我试图让程序只在返回的JSON中显示结果对象。
以下是代码:
require "kickbox"
require 'httparty'
require 'json'
client = Kickbox::Client.new('ac748asdfwef2fbf0e8177786233a6906cd3dcaa')
kickbox = client.kickbox()
response = kickbox.verify("test@easdfwf.com")
file = File.read(response)
json = JSON.parse(file)
json['result']
我收到错误verify.rb:10:read': no implicit conversion of Kickbox::HttpClient::Response into String (TypeError)
from verify.rb:10:in
'
以下是一个示例回复:
{
"result":"undeliverable",
"reason":"rejected_email",
"role":false,
"free":false,
"disposable":false,
"accept_all":false,
"did_you_mean":"bill.lumbergh@gmail.com",
"sendex":0,
"email":"bill.lumbergh@gamil.com",
"user":"bill.lumbergh",
"domain":"gamil.com",
"success":true,
"message":null
}
答案 0 :(得分:1)
您收到此错误:
read': no implicit conversion of Kickbox::HttpClient::Response into String (TypeError)
因为,在这一行:
file = File.read(response)
您的response
是Kickbox::HttpClient::Response
类型对象,但File.read
期待String
对象(可能是带路径的文件名)。
我不确定你要做什么,但是这个:file = File.read(response)
是错误的。你无法做到这一点,这就是你得到上述错误的原因。
如果您真的想使用文件,那么您可以将response
写入文件,然后从文件中读取response
并使用该文件:
f = File.new('response.txt', 'w+') # creating a file in read/write mode
f.write(response) # writing the response into that file
file_content = File.read('response.txt') # reading the response back from the file
所以,问题不在于在ruby中访问第三方API JSON对象,而是在尝试以错误的方式使用File.read
。
您可以通过以下方式从API获取response
:
client = Kickbox::Client.new('YOUR_API_KEY')
kickbox = client.kickbox()
response = kickbox.verify("test@easdfwf.com")
然后,您可以使用response
进行游戏,例如可以执行puts response.inspect
或puts response.body.inspect
并查看该对象内的内容。
而且,从那里你只能提取你所需的输出。