降低RABL模板的复杂性

时间:2014-07-24 08:50:52

标签: ruby-on-rails ruby json rabl

我的RABL模板似乎非常干燥且过于复杂。因此,我认为我可能错误地使用它,或者有更好的方法来产生我想要的输出。

从show.rabl代码中可以看出,我必须将plugins_vulnerability.vulnerability关联转换为JSON哈希,明确选择我需要的密钥,然后将plugins_vulnerability.fixed_in值合并到哈希中,最后将新哈希(现在包含fixed_in值)添加到vulnerabilities_array数组。

我这样做是因为我希望fixed_in值在vulnerability节点内。

plugins_controller.rb

class Api::V1::PluginsController < Api::V1::BaseController
  def show
    @plugin = Plugin.friendly.includes(:plugins_vulnerability, :vulnerabilities).find(params[:id])
  end
end

show.rabl:

object @plugin
cache @plugin if Rails.env == 'production'

attributes :name

# Add the 'vulnerabilities' node.
node :vulnerabilities do |vulnerabilities|
  vulnerabilities_array = []

  # turn the plugins_vulnerability association into an array
  vulnerabilities.plugins_vulnerability.to_a.each do |plugins_vulnerability|
    vulnerability = plugins_vulnerability.vulnerability.as_json # turn the plugins_vulnerability.vulnerability association into json
    vulnerability = vulnerability.select {|k,v| %w(id title references osvdb cve secunia exploitdb created_at updated_at metasploit fixed_in).include?(k) } # only select needed keys

    vulnerabilities_array << { 
      :vulnerability => vulnerability.merge(:fixed_in => plugins_vulnerability.fixed_in)
    } # merge the fixed_in attribute into the vulnerability hash and add them to an array (fixed_in is from plugins_vulnerabilities)
  end

  vulnerabilities_array
end

output.json

{
  "plugin": {
    "name": "simple-share-buttons-adder",
    "vulnerabilities": [
      {
        "vulnerability": {
          "id": 88157,
          "title": "Simple Share Buttons Adder 4.4 - options-general.php Multiple Admin Actions CSRF",
          "references": "https:\/\/security.dxw.com\/advisories\/csrf-and-stored-xss-in-simple-share-buttons-adder\/,http:\/\/packetstormsecurity.com\/files\/127238\/",
          "osvdb": "108444",
          "cve": "2014-4717",
          "secunia": "",
          "exploitdb": "33896",
          "created_at": "2014-07-15T17:16:51.227Z",
          "updated_at": "2014-07-15T17:16:51.227Z",
          "metasploit": "",
          "fixed_in": "4.5"
        }
      },
      {
        "vulnerability": {
          "id": 88158,
          "title": "Simple Share Buttons Adder 4.4 - options-general.php ssba_share_text Parameter Stored XSS Weakness",
          "references": "https:\/\/security.dxw.com\/advisories\/csrf-and-stored-xss-in-simple-share-buttons-adder\/,http:\/\/packetstormsecurity.com\/files\/127238\/",
          "osvdb": "108445",
          "cve": "",
          "secunia": "",
          "exploitdb": "33896",
          "created_at": "2014-07-15T17:16:51.341Z",
          "updated_at": "2014-07-15T17:16:51.341Z",
          "metasploit": "",
          "fixed_in": "4.5"
        }
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

我想你可以这样做:

object @plugin
cache @plugin if Rails.env == 'production'

attributes :name

child(@plugin.vulnerabilities => :vulnerabilities) {
  attributes :id, :title, :references, :osvdb, :cve, :secunia, :exploitdb, :created_at, :updated_at, :metasploit

  # Add the 'fixed_in' node.
  node :fixed_in do |vulnerability|
    @plugin.plugins_vulnerability.fixed_in
  end

}

这应该创建您需要的相同输出。它对我来说看起来并不复杂。