我是一个红宝石新手,这是我的第一个(现在是命令行)程序。首先,一些来源。
文件:AccessDb.rb
require 'mongo'
require 'json'
(...)
class AccessDb
def initialize dbname, collection #, username, password
@dbname = dbname
@collection = collection
@conn = Mongo::Connection.new
@db = @conn[dbname]
@coll = @db[collection]
end
def upsert_by_meta json
# print json
@coll.update({ :hash_md5 => json[:hash_md5] }, json, :upsert => true)
end
end
使用
文件:下载器.rb
require 'curb'
require 'yaml'
require 'json'
(...)
class Downloader
def initialize(directory)
@PASS=nil
@COOKIE=nil
@filename=nil
@full_file_location = nil
@target_dir = directory
File.exists? @target_dir # File.directory? @target_dir
@c = Curl::Easy.new
curl_setup
@mongo = AccessDb.new "meta","meta"
end
def parse_link_info(url)
json = {}
json[:link_requested] = url
if @c.last_effective_url != url
then json[:link_final] = @c.last_effective_url end
json[:link_filename_requested] = @filename
@final_filename = @c.last_effective_url.split(/\?/).first.split(/\//).last
if @final_filename != @filename
then json[:link_filename_delivered] = @final_filename end
json[:link_filetime] = Time.at(@c.file_time).utc.to_s
json[:content_lenght] = @c.downloaded_content_length
json[:content_type] = @c.content_type
@hash = MovieHasher::compute_hash(@save_location)
@hash = MovieHasher::compute_hash(@save_location)
if !@hash.nil?
then json[:hash_bigfile] = @hash end
json[:hash_md5] = Digest::MD5.hexdigest(File.read(@save_location))
JSON.pretty_generate(json)
end
(json是一些生成的json文件)
在 AccessDb.rb 测试中使用来自 Downloader.rb 的代码可以完美运行,但是当在 Downloader.rb 中使用该方法时,我得到以下输出:
D:/Dropbox/#code/PracaInz-Program/AccessDb.rb:20:in `[]': can't convert Symbol into Integer (TypeError)
from D:/Dropbox/#code/PracaInz-Program/AccessDb.rb:20:in `upsert_by_meta'
from D:/Dropbox/#code/PracaInz-Program/Downloader.rbw:158:in `block in add_links'
from D:/Dropbox/#code/PracaInz-Program/Downloader.rbw:148:in `each'
from D:/Dropbox/#code/PracaInz-Program/Downloader.rbw:148:in `add_links'
from D:/Dropbox/#code/PracaInz-Program/Downloader.rbw:189:in `<main>'
[Finished in 4.9s with exit code 1]
在一个可以在一个文件中完美测试的方法代码中。我如何编写它以便它可以使用符号但在该特定文件之外工作。谢谢
def parse_link_info(url)
json = {}
json[:link_requested] = url
if @c.last_effective_url != url
then json[:link_final] = @c.last_effective_url end
json[:link_filename_requested] = @filename
@final_filename = @c.last_effective_url.split(/\?/).first.split(/\//).last
if @final_filename != @filename
then json[:link_filename_delivered] = @final_filename end
json[:link_filetime] = Time.at(@c.file_time).utc.to_s
json[:content_lenght] = @c.downloaded_content_length
json[:content_type] = @c.content_type
@hash = MovieHasher::compute_hash(@save_location)
@hash = MovieHasher::compute_hash(@save_location)
if !@hash.nil?
then json[:hash_bigfile] = @hash end
json[:hash_md5] = Digest::MD5.hexdigest(File.read(@save_location))
JSON.pretty_generate(json)
end
def add_links(url_array,cred=nil,ref=nil,cookie=nil)
link_setup(cred,ref,cookie)
url_array.each do |single_url|
@c.url=single_url
@filename = single_url.split(/\?/).first.split(/\//).last
@save_location = @target_dir + '\\' + @filename
# puts @save_location
@c.perform
File.open(@save_location,"wb").write @c.body_str
json = parse_link_info single_url
# puts json
id = @mongo.upsert_by_meta json
puts id
json["_id"] = id
File.open(@save_location + :"meta.json","w").write json
end
end
编辑:更多代码(json 定义),完整跟踪