Ruby 如何规范文件下载
我正在开发一个库,需要能够使用RestClient从远程API下载插件文件。该库首先获取插件列表,然后将每个插件作为原始文件下载,并将每个插件保存在插件目录中 以下是迄今为止我所拥有的,但它让我失望:Ruby 如何规范文件下载,ruby,rspec,rspec2,Ruby,Rspec,Rspec2,我正在开发一个库,需要能够使用RestClient从远程API下载插件文件。该库首先获取插件列表,然后将每个插件作为原始文件下载,并将每个插件保存在插件目录中 以下是迄今为止我所拥有的,但它让我失望: require 'yaml' module Monitaur class Client attr_accessor :logger, :client_key, :server_url, :config, :raw_config, :plugin_
require 'yaml'
module Monitaur
class Client
attr_accessor :logger, :client_key, :server_url, :config, :raw_config,
:plugin_manifest
def initialize
load_config
@plugin_manifest ||= []
end
def run
get_plugin_manifest
sync_plugins
end
def get_plugin_manifest
res = RestClient.get("#{server_url}/nodes/#{client_key}/plugins")
@plugin_manifest = JSON.parse(res)
end
def sync_plugins
@plugin_manifest.each do |plugin|
res = RestClient.get("#{server_url}/plugins/#{plugin['name']}")
File.open(File.join(Monitaur.plugin_dir, "#{plugin['name']}.rb"), "w+") do |file|
file.write res.body
end
end
end
def load_config
if File.exist?(Monitaur.config_file_path) && File.readable?(Monitaur.config_file_path)
@raw_config = YAML.load_file(Monitaur.config_file_path)
else
raise IOError, "Cannot open or read #{Monitaur.config_file_path}"
end
@server_url = raw_config['server_url']
@client_key = raw_config['client_key']
end
end
end
和客户_spec.rb
require 'spec_helper'
module Monitaur
describe Client do
let(:server_url) { "http://api.monitaurapp.com" }
let(:client_key) { "asdf1234" }
describe "#load_config" do
let(:client) { Monitaur::Client.new }
before do
File.open(Monitaur.config_file_path, "w") do |file|
file.puts "server_url: http://api.monitaurapp.com"
file.puts "client_key: asdf1234"
end
end
it "loads up the configuration file" do
client.load_config
client.server_url.should == "http://api.monitaurapp.com"
client.client_key.should == "asdf1234"
end
end
describe "#get_plugin_manifest" do
let(:client) { Monitaur::Client.new }
before do
stub_get_plugin_manifest
end
it "retrieves a plugins manifest from the server" do
client.get_plugin_manifest
client.plugin_manifest.should == plugin_manifest_response
end
end
describe "#sync_plugins" do
let(:client) { Monitaur::Client.new }
let(:foo_plugin) { mock('foo_plugin') }
let(:bar_plugin) { mock('bar_plugin') }
before do
FileUtils.mkdir("/tmp")
File.open("/tmp/foo_plugin.rb", "w+") do |file|
file.write %|
class FooPlugin < Monitaur::Plugin
name "foo_plugin"
desc "A test plugin to determine whether plugin sync works"
def run
{ :foo => 'foo' }
end
end
|
end
File.open("/tmp/bar_plugin.rb", "w+") do |file|
file.write %|
class BarPlugin < Monitaur::Plugin
name "bar_plugin"
desc "A test plugin to determine whether plugin sync works"
def run
{ :bar => 'bar' }
end
end
|
end
Monitaur.install
stub_get_plugin_manifest
stub_sync_plugins
client.get_plugin_manifest
end
it "downloads plugins to the cache directory" do
File.should_receive(:open).
with(File.join(Monitaur.plugin_dir, "foo_plugin.rb"), "w+")
and_yield(foo_plugin)
client.sync_plugins
File.exist?("/home/user/.monitaur/cache/plugins/foo_plugin.rb").should be_true
File.exist?("/home/user/.monitaur/cache/plugins/bar_plugin.rb").should be_true
end
end
end
end
def stub_get_plugin_manifest
stub_request(:get, "#{server_url}/nodes/#{client_key}/plugins").
to_return(
:status => 200,
:body => %Q{
[
{
"name": "foo_plugin",
"checksum": "qwer5678"
},
{
"name": "bar_plugin",
"checksum": "hjkl4321"
}
]
}
)
end
def plugin_manifest_response
[
{
"name" => "foo_plugin",
"checksum" => "qwer5678"
},
{
"name" => "bar_plugin",
"checksum" => "hjkl4321"
}
]
end
def stub_sync_plugins
stub_request(:get, "#{server_url}/plugins/foo_plugin").
to_return(:body => File.open('/tmp/foo_plugin.rb').read)
stub_request(:get, "#{server_url}/plugins/bar_plugin").
to_return(:body => File.open('/tmp/bar_plugin.rb').read)
end
require'spec\u helper'
模块Monitaur
描述客户做什么
let(:server_url){”http://api.monitaurapp.com" }
let(:client_key){“asdf1234”}
描述“#加载配置”do
let(:client){Monitaur::client.new}
在做之前
打开(Monitaur.config_File_path,“w”)do|File|
file.puts“服务器\u url:http://api.monitaurapp.com"
file.puts“客户端密钥:asdf1234”
结束
结束
它会“加载配置文件”吗
client.load\u config
client.server_url.should==”http://api.monitaurapp.com"
client.client_key.should==“asdf1234”
结束
结束
描述“#获取插件(清单)”的操作
let(:client){Monitaur::client.new}
在做之前
存根\u获取\u插件\u清单
结束
它“从服务器检索插件清单”做什么
client.get_插件_清单
client.plugin\u manifest.should==plugin\u manifest\u响应
结束
结束
描述“#同步#u插件”做什么
let(:client){Monitaur::client.new}
let(:foo_plugin){mock('foo_plugin')}
let(:bar_plugin){mock('bar_plugin')}
在做之前
FileUtils.mkdir(“/tmp”)
打开(“/tmp/foo_plugin.rb”,“w+”)do|文件|
file.write%|
类FooPluginfoo'}
结束
结束
|
结束
打开(“/tmp/bar_plugin.rb”,“w+”)do|文件|
file.write%|
类BarPluginbar'}
结束
结束
|
结束
Monitaur.install
存根\u获取\u插件\u清单
存根同步插件
client.get_插件_清单
结束
它“下载插件到缓存目录”吗
文件。应接收(:打开)。
使用(File.join(Monitaur.plugin\u dir,“foo\u plugin.rb”),“w+”)
和_产量(foo_插件)
client.sync_插件
File.exist?(“/home/user/.monitaur/cache/plugins/foo_plugin.rb”)。应为true
File.exist?(“/home/user/.monitaur/cache/plugins/bar_plugin.rb”)。应为真
结束
结束
结束
结束
def存根获取插件清单
存根请求(:get,“#{server_url}/nodes/#{client_key}/plugins”)。
返回(
:状态=>200,
:body=>%Q{
[
{
“名称”:“foo_插件”,
“校验和”:“qwer5678”
},
{
“名称”:“bar_插件”,
“校验和”:“hjkl4321”
}
]
}
)
结束
def插件清单响应
[
{
“name”=>“foo_插件”,
“校验和”=>“qwer5678”
},
{
“name”=>“bar_插件”,
“校验和”=>“hjkl4321”
}
]
结束
def存根同步插件
存根请求(:get,“{server\u url}/plugins/foo\u plugin”)。
to_return(:body=>File.open('/tmp/foo_plugin.rb').read)
存根请求(:get,“{server\u url}/plugins/bar\u plugin”)。
to_return(:body=>File.open('/tmp/bar_plugin.rb').read)
结束
如何测试下载过程 我使用它是为了达到这个目的,因为如果另一个站点出现故障或其他情况,您的规范就没有必要失败。请参阅文档中的“回放记录的响应”。我们要做的是curl
将页面保存在某个地方作为一个固定装置,并在规范中重播它。这可能有帮助吗?