我正在尝试通过来自 rest-api的padrino框架中的 activeresource 解析以下 jsons 响应:
{"result":"Ok","resource":"ProjectRazor::Slice::Model","errcode":0,"command":"models_query_all","response":[{"@uri":"http://192.168.0.50:8026/razor/api/model/5nBqgTngqvxPIxVJJpdeQm","@callback":{},"@version":2,"@uuid":"5nBqgTngqvxPIxVJJpdeQm","@description":"Ubuntu Precise Model","@domainname":"andreasheumaier.de","@counter":0,"@broker_plugin":"agent","@root_password":"/7bbNwa6","@log":[],"@policy_bound":null,"@hostname_prefix":"firebird","@classname":"ProjectRazor::ModelTemplate::UbuntuPrecise","@osversion":"precise","@is_template":false,"@final_state":"os_complete","@node":null,"@req_metadata_hash":{"@domainname":{"validation":"^[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9](\\.[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])*$","example":"example.com","required":true,"description":"local domain name (will be used in /etc/hosts file)","default":"localdomain"},"@root_password":{"validation":"^[\\S]{8,}","example":"P@ssword!","required":true,"description":"root password (> 8 characters)","default":"test1234"},"@hostname_prefix":{"validation":"^[a-zA-Z0-9][a-zA-Z0-9\\-]*$","example":"node","required":true,"description":"node hostname prefix (will append node number)","default":"node"}},"@hidden":false,"@name":"ubuntu_precise","@label":"install_precise","@current_state":"init","@result":null,"@template":"linux_deploy","@noun":"model","@image_prefix":"os","@image_uuid":"4VuDj4mJDHP6bvWeztfXOO"}],"http_err_code":200}
我定义了这个模型
class RazorJsonFormatter
include ActiveResource::Formats::JsonFormat
def decode(json)
ActiveResource::Formats::JsonFormat.decode(json)['response']
end
end
class Razor < ActiveResource::Base
include ActiveResource::Formats::JsonFormat
include ActiveResource::Extend::WithoutExtension
self.format = RazorJsonFormatter.new
self.site = "http://yellowbird:8026/razor/api/"
end
class RazorModel < Razor
set_collection_name 'model'
end
这会产生以下错误:
RazorModel.all
NameError: wrong constant name @callback
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1428:in `const_defined?'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1428:in `find_or_create_resource_for'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1278:in `block in load'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1264:in `each'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1264:in `load'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:1006:in `initialize'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:933:in `new'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:933:in `instantiate_record'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `block in instantiate_collection'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `collect!'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:929:in `instantiate_collection'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:901:in `find_every'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:814:in `find'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/base.rb:826:in `first'
from (irb):27
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/lib/padrino-core/cli/base.rb:68:in `console'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/task.rb:27:in `run'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:120:in `invoke_task'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor.rb:275:in `dispatch'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:109:in `invoke'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/lib/padrino-core/cli/base.rb:73:in `c'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/task.rb:27:in `run'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/invocation.rb:120:in `invoke_task'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor.rb:275:in `dispatch'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/thor-0.15.4/lib/thor/base.rb:425:in `start'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/gems/padrino-core-0.10.7/bin/padrino:9:in `<top (required)>'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/bin/padrino:19:in `load'
from /home/aheumaier/.rvm/gems/ruby-1.9.3-p194/bin/padrino:19:in `<main>'1.9.3p194
(...)
这可以与预定义的 ActiveResource 模式一起使用。我目前的解决方法是使用rest-client从头开始编写所有类
如何使用 activeresource 解析 - 或忽略 - 嵌套的 json 属性?