创建 Ruby API
Creating a Ruby API
我的任务是创建一个 Ruby API 来检索 youtube URL。但是,我不确定创建 'API' 的正确方法......我在下面作为服务 JSON 的 Sinatra 服务器执行了以下代码,但 JSON 的定义到底是什么? =16=] 这算不算一个?如果这不是 API,我怎样才能输入 API?提前致谢。
require 'open-uri'
require 'json'
require 'sinatra'
# get user input
puts "Please enter a search (seperate words by commas):"
search_input = gets.chomp
puts
puts "Performing search on YOUTUBE ... go to '/videos' API endpoint to see the results and use the output"
puts
# define query parameters
api_key = 'my_key_here'
search_url = 'https://www.googleapis.com/youtube/v3/search'
params = {
part: 'snippet',
q: search_input,
type: 'video',
videoCaption: 'closedCaption',
key: api_key
}
# use search_url and query parameters to construct a url, then open and parse the result
uri = URI.parse(search_url)
uri.query = URI.encode_www_form(params)
result = JSON.parse(open(uri).read)
# class to define attributes of each video and format into eventual json
class Video
attr_accessor :title, :description, :url
def initialize
@title = nil
@description = nil
@url = nil
end
def to_hash
{
'title' => @title,
'description' => @description,
'url' => @url
}
end
def to_json
self.to_hash.to_json
end
end
# create an array with top 3 search results
results_array = []
result["items"].take(3).each do |video|
@video = Video.new
@video.title = video["snippet"]["title"]
@video.description = video["snippet"]["description"]
@video.url = video["snippet"]["thumbnails"]["default"]["url"]
results_array << @video.to_json.gsub!(/\"/, '\'')
end
# define the API endpoint
get '/videos' do
results_array.to_json
end
"API = Application Program Interface" 简单地说,某种东西,另一个程序可以可靠地使用它来完成工作,而不必忙于它的小脑袋 如何完成工作。
也许现在最简单的事情是,如果可能的话,回到"tasked"你做这个任务的人那里,问问him/her,"well, what do you have in mind?"最好API 您可以设计的,在这种情况下,将是最方便的 对于 人们(正在编写...的程序)实际上必须用它。 "Don't guess. Ask!"
API 的一个非常常见的策略,在像 Ruby 这样的语言中,是定义一个 class
代表 "this application's connection to this service." 任何人想要使用 API 是通过调用某个函数来实现的,该函数将 return 这个 class 的一个新实例。此后,程序使用这个对象来发出和处理请求。
请求,也是对象。要发出请求,您首先要求 API-connection 对象给您一个新的请求对象。然后你用任何细节填写请求,然后告诉请求对象 "go!" 在未来的某个时候,通过一些适当的方式 (例如回调 ...) 请求对象会通知您成功或失败。
"A whole lot of voodoo-magic might have taken place," 在请求对象和生成它的连接对象之间,但是 client 不必关心。最重要的是,任何 API 的 objective。 "It Just Works.™"
我认为他们希望您创建一个第三方库。想象一下你有一段时间精神分裂。
Joe 想构建一个 Sinatra 应用程序来列出一些 YouTube 视频,但他很懒惰,不想做脏活,他只是想添加一些东西,给它一些凭据,请求 url 和使用它们,finito。
Joe 要求 Bob 为他实施它,他给了他他的要求:"Bob, I need YouTube library. I need it to do:"
# Please note that I don't know how YouTube API works, just guessing.
client = YouTube.new(api_key: 'hola')
video_urls = client.videos # => ['https://...', 'https://...', ...]
Bob 说 "OK." end 在他的交互式控制台中度过了一天。
所以首先,你应该弄清楚你将如何使用你还不存在的库,如果可以的话——有时你只是还不知道。
接下来,根据要求构建该库,然后将其放入您的 Sinatra 应用程序中,您就完成了。有帮助吗?
我的任务是创建一个 Ruby API 来检索 youtube URL。但是,我不确定创建 'API' 的正确方法......我在下面作为服务 JSON 的 Sinatra 服务器执行了以下代码,但 JSON 的定义到底是什么? =16=] 这算不算一个?如果这不是 API,我怎样才能输入 API?提前致谢。
require 'open-uri'
require 'json'
require 'sinatra'
# get user input
puts "Please enter a search (seperate words by commas):"
search_input = gets.chomp
puts
puts "Performing search on YOUTUBE ... go to '/videos' API endpoint to see the results and use the output"
puts
# define query parameters
api_key = 'my_key_here'
search_url = 'https://www.googleapis.com/youtube/v3/search'
params = {
part: 'snippet',
q: search_input,
type: 'video',
videoCaption: 'closedCaption',
key: api_key
}
# use search_url and query parameters to construct a url, then open and parse the result
uri = URI.parse(search_url)
uri.query = URI.encode_www_form(params)
result = JSON.parse(open(uri).read)
# class to define attributes of each video and format into eventual json
class Video
attr_accessor :title, :description, :url
def initialize
@title = nil
@description = nil
@url = nil
end
def to_hash
{
'title' => @title,
'description' => @description,
'url' => @url
}
end
def to_json
self.to_hash.to_json
end
end
# create an array with top 3 search results
results_array = []
result["items"].take(3).each do |video|
@video = Video.new
@video.title = video["snippet"]["title"]
@video.description = video["snippet"]["description"]
@video.url = video["snippet"]["thumbnails"]["default"]["url"]
results_array << @video.to_json.gsub!(/\"/, '\'')
end
# define the API endpoint
get '/videos' do
results_array.to_json
end
"API = Application Program Interface" 简单地说,某种东西,另一个程序可以可靠地使用它来完成工作,而不必忙于它的小脑袋 如何完成工作。
也许现在最简单的事情是,如果可能的话,回到"tasked"你做这个任务的人那里,问问him/her,"well, what do you have in mind?"最好API 您可以设计的,在这种情况下,将是最方便的 对于 人们(正在编写...的程序)实际上必须用它。 "Don't guess. Ask!"
API 的一个非常常见的策略,在像 Ruby 这样的语言中,是定义一个 class
代表 "this application's connection to this service." 任何人想要使用 API 是通过调用某个函数来实现的,该函数将 return 这个 class 的一个新实例。此后,程序使用这个对象来发出和处理请求。
请求,也是对象。要发出请求,您首先要求 API-connection 对象给您一个新的请求对象。然后你用任何细节填写请求,然后告诉请求对象 "go!" 在未来的某个时候,通过一些适当的方式 (例如回调 ...) 请求对象会通知您成功或失败。
"A whole lot of voodoo-magic might have taken place," 在请求对象和生成它的连接对象之间,但是 client 不必关心。最重要的是,任何 API 的 objective。 "It Just Works.™"
我认为他们希望您创建一个第三方库。想象一下你有一段时间精神分裂。
Joe 想构建一个 Sinatra 应用程序来列出一些 YouTube 视频,但他很懒惰,不想做脏活,他只是想添加一些东西,给它一些凭据,请求 url 和使用它们,finito。
Joe 要求 Bob 为他实施它,他给了他他的要求:"Bob, I need YouTube library. I need it to do:"
# Please note that I don't know how YouTube API works, just guessing.
client = YouTube.new(api_key: 'hola')
video_urls = client.videos # => ['https://...', 'https://...', ...]
Bob 说 "OK." end 在他的交互式控制台中度过了一天。
所以首先,你应该弄清楚你将如何使用你还不存在的库,如果可以的话——有时你只是还不知道。
接下来,根据要求构建该库,然后将其放入您的 Sinatra 应用程序中,您就完成了。有帮助吗?