metasploit-framework/api/ResponseDataHelper.html

868 lines
41 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
Module: ResponseDataHelper
&mdash; Documentation by YARD 0.9.19
</title>
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
<script type="text/javascript" charset="utf-8">
pathId = "ResponseDataHelper";
relpath = '';
</script>
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
</head>
<body>
<div class="nav_wrap">
<iframe id="nav" src="class_list.html?1"></iframe>
<div id="resizer"></div>
</div>
<div id="main" tabindex="-1">
<div id="header">
<div id="menu">
<a href="_index.html">Index (R)</a> &raquo;
<span class="title">ResponseDataHelper</span>
</div>
<div id="search">
<a class="full_list_link" id="class_list_link"
href="class_list.html">
<svg width="24" height="24">
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
</svg>
</a>
</div>
<div class="clear"></div>
</div>
<div id="content"><h1>Module: ResponseDataHelper
</h1>
<div class="box_info">
<dl>
<dt>Included in:</dt>
<dd><span class='object_link'><a href="RemoteCredentialDataService.html" title="RemoteCredentialDataService (module)">RemoteCredentialDataService</a></span>, <span class='object_link'><a href="RemoteDbExportDataService.html" title="RemoteDbExportDataService (module)">RemoteDbExportDataService</a></span>, <span class='object_link'><a href="RemoteDbImportDataService.html" title="RemoteDbImportDataService (module)">RemoteDbImportDataService</a></span>, <span class='object_link'><a href="RemoteEventDataService.html" title="RemoteEventDataService (module)">RemoteEventDataService</a></span>, <span class='object_link'><a href="RemoteHostDataService.html" title="RemoteHostDataService (module)">RemoteHostDataService</a></span>, <span class='object_link'><a href="RemoteLoginDataService.html" title="RemoteLoginDataService (module)">RemoteLoginDataService</a></span>, <span class='object_link'><a href="RemoteLootDataService.html" title="RemoteLootDataService (module)">RemoteLootDataService</a></span>, <span class='object_link'><a href="RemoteMsfDataService.html" title="RemoteMsfDataService (module)">RemoteMsfDataService</a></span>, <span class='object_link'><a href="RemoteNmapDataService.html" title="RemoteNmapDataService (module)">RemoteNmapDataService</a></span>, <span class='object_link'><a href="RemoteNoteDataService.html" title="RemoteNoteDataService (module)">RemoteNoteDataService</a></span>, <span class='object_link'><a href="RemotePayloadDataService.html" title="RemotePayloadDataService (module)">RemotePayloadDataService</a></span>, <span class='object_link'><a href="RemoteSessionDataService.html" title="RemoteSessionDataService (module)">RemoteSessionDataService</a></span>, <span class='object_link'><a href="RemoteSessionEventDataService.html" title="RemoteSessionEventDataService (module)">RemoteSessionEventDataService</a></span>, <span class='object_link'><a href="RemoteVulnAttemptDataService.html" title="RemoteVulnAttemptDataService (module)">RemoteVulnAttemptDataService</a></span>, <span class='object_link'><a href="RemoteVulnDataService.html" title="RemoteVulnDataService (module)">RemoteVulnDataService</a></span>, <span class='object_link'><a href="RemoteWebDataService.html" title="RemoteWebDataService (module)">RemoteWebDataService</a></span>, <span class='object_link'><a href="RemoteWorkspaceDataService.html" title="RemoteWorkspaceDataService (module)">RemoteWorkspaceDataService</a></span>, <span class='object_link'><a href="ServletHelper.html" title="ServletHelper (module)">ServletHelper</a></span></dd>
</dl>
<dl>
<dt>Defined in:</dt>
<dd>lib/metasploit/framework/data_service/remote/http/response_data_helper.rb</dd>
</dl>
</div>
<h2>Overview</h2><div class="docstring">
<div class="discussion">
<p>HTTP response helper class</p>
</div>
</div>
<div class="tags">
</div>
<h2>
Instance Method Summary
<small><a href="#" class="summary_toggle">collapse</a></small>
</h2>
<ul class="summary">
<li class="public ">
<span class="summary_signature">
<a href="#json_to_hash-instance_method" title="#json_to_hash (instance method)">#<strong>json_to_hash</strong>(response_wrapper) &#x21d2; Hash </a>
</span>
<span class="summary_desc"><div class='inline'>
<p>Converts an HTTP response to a Hash.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#json_to_mdm_object-instance_method" title="#json_to_mdm_object (instance method)">#<strong>json_to_mdm_object</strong>(response_wrapper, mdm_class, returns_on_error = nil) &#x21d2; ActiveRecord::Base </a>
</span>
<span class="summary_desc"><div class='inline'>
<p>Converts an HTTP response to an Mdm Object.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#process_file-instance_method" title="#process_file (instance method)">#<strong>process_file</strong>(base64_file, save_path) &#x21d2; String </a>
</span>
<span class="summary_desc"><div class='inline'>
<p>Processes a Base64 encoded file included in a JSON request.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#process_response-instance_method" title="#process_response (instance method)">#<strong>process_response</strong>(response_wrapper) &#x21d2; Object </a>
</span>
<span class="summary_desc"><div class='inline'></div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#to_ar-instance_method" title="#to_ar (instance method)">#<strong>to_ar</strong>(klass, val, base_object = nil) &#x21d2; ActiveRecord::Base </a>
</span>
<span class="summary_desc"><div class='inline'>
<p>Converts a Hash or JSON string to an ActiveRecord object.</p>
</div></span>
</li>
</ul>
<div id="instance_method_details" class="method_details_list">
<h2>Instance Method Details</h2>
<div class="method_details first">
<h3 class="signature first" id="json_to_hash-instance_method">
#<strong>json_to_hash</strong>(response_wrapper) &#x21d2; <tt>Hash</tt>
</h3><div class="docstring">
<div class="discussion">
<p>Converts an HTTP response to a Hash</p>
</div>
</div>
<div class="tags">
<p class="tag_title">Parameters:</p>
<ul class="param">
<li>
<span class='name'>A</span>
<span class='type'>(<tt>ResponseWrapper</tt>)</span>
&mdash;
<div class='inline'>
<p>wrapped HTTP response containing a JSON body.</p>
</div>
</li>
</ul>
<p class="tag_title">Returns:</p>
<ul class="return">
<li>
<span class='type'>(<tt>Hash</tt>)</span>
&mdash;
<div class='inline'>
<p>A Hash interpretation of the JSON body.</p>
</div>
</li>
</ul>
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
26
27
28
29
30
31
32
33
34
35
36
37</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/metasploit/framework/data_service/remote/http/response_data_helper.rb', line 26</span>
<span class='kw'>def</span> <span class='id identifier rubyid_json_to_hash'>json_to_hash</span><span class='lparen'>(</span><span class='id identifier rubyid_response_wrapper'>response_wrapper</span><span class='rparen'>)</span>
<span class='kw'>begin</span>
<span class='id identifier rubyid_body'>body</span> <span class='op'>=</span> <span class='id identifier rubyid_process_response'>process_response</span><span class='lparen'>(</span><span class='id identifier rubyid_response_wrapper'>response_wrapper</span><span class='rparen'>)</span>
<span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_body'>body</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>&amp;&amp;</span> <span class='op'>!</span><span class='id identifier rubyid_body'>body</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span>
<span class='id identifier rubyid_parsed_body'>parsed_body</span> <span class='op'>=</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_body'>body</span><span class='comma'>,</span> <span class='label'>symbolize_names:</span> <span class='kw'>true</span><span class='rparen'>)</span>
<span class='kw'>return</span> <span class='id identifier rubyid_parsed_body'>parsed_body</span><span class='lbracket'>[</span><span class='symbol'>:data</span><span class='rbracket'>]</span>
<span class='kw'>end</span>
<span class='kw'>rescue</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_e'>e</span>
<span class='id identifier rubyid_elog'><span class='object_link'><a href="top-level-namespace.html#elog-instance_method" title="#elog (method)">elog</a></span></span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Error parsing response as JSON: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_message'>message</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span>
<span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_backtrace'>backtrace</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_line'>line</span><span class='op'>|</span> <span class='id identifier rubyid_elog'><span class='object_link'><a href="top-level-namespace.html#elog-instance_method" title="#elog (method)">elog</a></span></span> <span class='id identifier rubyid_line'>line</span> <span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="json_to_mdm_object-instance_method">
#<strong>json_to_mdm_object</strong>(response_wrapper, mdm_class, returns_on_error = nil) &#x21d2; <tt>ActiveRecord::Base</tt>
</h3><div class="docstring">
<div class="discussion">
<p>Converts an HTTP response to an Mdm Object</p>
</div>
</div>
<div class="tags">
<p class="tag_title">Parameters:</p>
<ul class="param">
<li>
<span class='name'>A</span>
<span class='type'>(<tt>ResponseWrapper</tt>)</span>
&mdash;
<div class='inline'>
<p>wrapped HTTP response containing a JSON body.</p>
</div>
</li>
<li>
<span class='name'>The</span>
<span class='type'>(<tt>String</tt>)</span>
&mdash;
<div class='inline'>
<p>Mdm class to convert the JSON to.</p>
</div>
</li>
<li>
<span class='name'>A</span>
<span class='type'>(<tt>Anything</tt>)</span>
&mdash;
<div class='inline'>
<p>failsafe response to return if no objects are found.</p>
</div>
</li>
</ul>
<p class="tag_title">Returns:</p>
<ul class="return">
<li>
<span class='type'>(<tt>ActiveRecord::Base</tt>)</span>
&mdash;
<div class='inline'>
<p>An object of type mdm_class, which inherits from ActiveRecord::Base</p>
</div>
</li>
</ul>
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/metasploit/framework/data_service/remote/http/response_data_helper.rb', line 47</span>
<span class='kw'>def</span> <span class='id identifier rubyid_json_to_mdm_object'>json_to_mdm_object</span><span class='lparen'>(</span><span class='id identifier rubyid_response_wrapper'>response_wrapper</span><span class='comma'>,</span> <span class='id identifier rubyid_mdm_class'>mdm_class</span><span class='comma'>,</span> <span class='id identifier rubyid_returns_on_error'>returns_on_error</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
<span class='kw'>if</span> <span class='id identifier rubyid_response_wrapper'>response_wrapper</span><span class='period'>.</span><span class='id identifier rubyid_expected'>expected</span>
<span class='kw'>begin</span>
<span class='id identifier rubyid_body'>body</span> <span class='op'>=</span> <span class='id identifier rubyid_process_response'>process_response</span><span class='lparen'>(</span><span class='id identifier rubyid_response_wrapper'>response_wrapper</span><span class='rparen'>)</span>
<span class='kw'>if</span> <span class='op'>!</span><span class='id identifier rubyid_body'>body</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span> <span class='op'>&amp;&amp;</span> <span class='op'>!</span><span class='id identifier rubyid_body'>body</span><span class='period'>.</span><span class='id identifier rubyid_empty?'>empty?</span>
<span class='id identifier rubyid_parsed_body'>parsed_body</span> <span class='op'>=</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_body'>body</span><span class='comma'>,</span> <span class='label'>symbolize_names:</span> <span class='kw'>true</span><span class='rparen'>)</span>
<span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='const'>Array</span><span class='period'>.</span><span class='id identifier rubyid_wrap'>wrap</span><span class='lparen'>(</span><span class='id identifier rubyid_parsed_body'>parsed_body</span><span class='lbracket'>[</span><span class='symbol'>:data</span><span class='rbracket'>]</span><span class='rparen'>)</span>
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
<span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_json_object'>json_object</span><span class='op'>|</span>
<span class='id identifier rubyid_rv'>rv</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_to_ar'>to_ar</span><span class='lparen'>(</span><span class='id identifier rubyid_mdm_class'>mdm_class</span><span class='period'>.</span><span class='id identifier rubyid_constantize'>constantize</span><span class='comma'>,</span> <span class='id identifier rubyid_json_object'>json_object</span><span class='rparen'>)</span>
<span class='kw'>end</span>
<span class='kw'>return</span> <span class='id identifier rubyid_rv'>rv</span>
<span class='kw'>end</span>
<span class='kw'>rescue</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_e'>e</span>
<span class='id identifier rubyid_elog'><span class='object_link'><a href="top-level-namespace.html#elog-instance_method" title="#elog (method)">elog</a></span></span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Mdm Object conversion failed </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_message'>message</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span>
<span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_backtrace'>backtrace</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_line'>line</span><span class='op'>|</span> <span class='id identifier rubyid_elog'><span class='object_link'><a href="top-level-namespace.html#elog-instance_method" title="#elog (method)">elog</a></span></span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_line'>line</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span> <span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>return</span> <span class='id identifier rubyid_returns_on_error'>returns_on_error</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="process_file-instance_method">
#<strong>process_file</strong>(base64_file, save_path) &#x21d2; <tt>String</tt>
</h3><div class="docstring">
<div class="discussion">
<p>Processes a Base64 encoded file included in a JSON request. Saves the file
in the location specified in the parameter.</p>
</div>
</div>
<div class="tags">
<p class="tag_title">Parameters:</p>
<ul class="param">
<li>
<span class='name'>base64_file</span>
<span class='type'>(<tt>String</tt>)</span>
&mdash;
<div class='inline'>
<p>The Base64 encoded file.</p>
</div>
</li>
<li>
<span class='name'>save_path</span>
<span class='type'>(<tt>String</tt>)</span>
&mdash;
<div class='inline'>
<p>The location to store the file. This should include the file&#39;s name.</p>
</div>
</li>
</ul>
<p class="tag_title">Returns:</p>
<ul class="return">
<li>
<span class='type'>(<tt>String</tt>)</span>
&mdash;
<div class='inline'>
<p>The location where the file was successfully stored.</p>
</div>
</li>
</ul>
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
75
76
77
78
79
80
81
82
83
84
85
86
87
88</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/metasploit/framework/data_service/remote/http/response_data_helper.rb', line 75</span>
<span class='kw'>def</span> <span class='id identifier rubyid_process_file'>process_file</span><span class='lparen'>(</span><span class='id identifier rubyid_base64_file'>base64_file</span><span class='comma'>,</span> <span class='id identifier rubyid_save_path'>save_path</span><span class='rparen'>)</span>
<span class='id identifier rubyid_decoded_file'>decoded_file</span> <span class='op'>=</span> <span class='const'>Base64</span><span class='period'>.</span><span class='id identifier rubyid_urlsafe_decode64'>urlsafe_decode64</span><span class='lparen'>(</span><span class='id identifier rubyid_base64_file'>base64_file</span><span class='rparen'>)</span>
<span class='kw'>begin</span>
<span class='comment'># If we are running the data service on the same box this will ensure we only write
</span> <span class='comment'># the file if it is somehow not there already.
</span> <span class='kw'>unless</span> <span class='const'>File</span><span class='period'>.</span><span class='id identifier rubyid_exists?'>exists?</span><span class='lparen'>(</span><span class='id identifier rubyid_save_path'>save_path</span><span class='rparen'>)</span> <span class='op'>&amp;&amp;</span> <span class='const'>File</span><span class='period'>.</span><span class='id identifier rubyid_read'>read</span><span class='lparen'>(</span><span class='id identifier rubyid_save_path'>save_path</span><span class='rparen'>)</span> <span class='op'>==</span> <span class='id identifier rubyid_decoded_file'>decoded_file</span>
<span class='const'>File</span><span class='period'>.</span><span class='id identifier rubyid_open'>open</span><span class='lparen'>(</span><span class='id identifier rubyid_save_path'>save_path</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>w+</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_file'>file</span><span class='op'>|</span> <span class='id identifier rubyid_file'>file</span><span class='period'>.</span><span class='id identifier rubyid_write'>write</span><span class='lparen'>(</span><span class='id identifier rubyid_decoded_file'>decoded_file</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='kw'>rescue</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_e'>e</span>
<span class='id identifier rubyid_elog'><span class='object_link'><a href="top-level-namespace.html#elog-instance_method" title="#elog (method)">elog</a></span></span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>There was an error writing the file: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_e'>e</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span>
<span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_backtrace'>backtrace</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_line'>line</span><span class='op'>|</span> <span class='id identifier rubyid_elog'><span class='object_link'><a href="top-level-namespace.html#elog-instance_method" title="#elog (method)">elog</a></span></span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_line'>line</span><span class='embexpr_end'>}</span><span class='tstring_content'>\n</span><span class='tstring_end'>&quot;</span></span><span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_save_path'>save_path</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="process_response-instance_method">
#<strong>process_response</strong>(response_wrapper) &#x21d2; <tt>Object</tt>
</h3><table class="source_code">
<tr>
<td>
<pre class="lines">
9
10
11
12
13
14
15
16
17
18</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/metasploit/framework/data_service/remote/http/response_data_helper.rb', line 9</span>
<span class='kw'>def</span> <span class='id identifier rubyid_process_response'>process_response</span><span class='lparen'>(</span><span class='id identifier rubyid_response_wrapper'>response_wrapper</span><span class='rparen'>)</span>
<span class='kw'>begin</span>
<span class='kw'>if</span> <span class='id identifier rubyid_response_wrapper'>response_wrapper</span><span class='period'>.</span><span class='id identifier rubyid_expected'>expected</span>
<span class='id identifier rubyid_response_wrapper'>response_wrapper</span><span class='period'>.</span><span class='id identifier rubyid_response'>response</span><span class='period'>.</span><span class='id identifier rubyid_body'>body</span>
<span class='kw'>end</span>
<span class='kw'>rescue</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_e'>e</span>
<span class='id identifier rubyid_elog'><span class='object_link'><a href="top-level-namespace.html#elog-instance_method" title="#elog (method)">elog</a></span></span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>Error processing response: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_message'>message</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span>
<span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_backtrace'>backtrace</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_line'>line</span><span class='op'>|</span> <span class='id identifier rubyid_elog'><span class='object_link'><a href="top-level-namespace.html#elog-instance_method" title="#elog (method)">elog</a></span></span> <span class='id identifier rubyid_line'>line</span> <span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="to_ar-instance_method">
#<strong>to_ar</strong>(klass, val, base_object = nil) &#x21d2; <tt>ActiveRecord::Base</tt>
</h3><div class="docstring">
<div class="discussion">
<p>Converts a Hash or JSON string to an ActiveRecord object. Importantly, this
retains associated objects if they are in the JSON string.</p>
<p>Modified from <a
href="https://github.com/swdyh/toar">github.com/swdyh/toar</a>/ Credit to
<a href="https://github.com/swdyh">github.com/swdyh</a></p>
</div>
</div>
<div class="tags">
<p class="tag_title">Parameters:</p>
<ul class="param">
<li>
<span class='name'>klass</span>
<span class='type'>(<tt>String</tt>)</span>
&mdash;
<div class='inline'>
<p>The ActiveRecord class to convert the JSON/Hash to.</p>
</div>
</li>
<li>
<span class='name'>val</span>
<span class='type'>(<tt>String</tt>)</span>
&mdash;
<div class='inline'>
<p>The JSON string, or Hash, to convert.</p>
</div>
</li>
<li>
<span class='name'>base_class</span>
<span class='type'>(<tt>Class</tt>)</span>
&mdash;
<div class='inline'>
<p>The base class to build back to. Used for recursion.</p>
</div>
</li>
</ul>
<p class="tag_title">Returns:</p>
<ul class="return">
<li>
<span class='type'>(<tt>ActiveRecord::Base</tt>)</span>
&mdash;
<div class='inline'>
<p>A klass object, which inherits from ActiveRecord::Base.</p>
</div>
</li>
</ul>
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/metasploit/framework/data_service/remote/http/response_data_helper.rb', line 100</span>
<span class='kw'>def</span> <span class='id identifier rubyid_to_ar'>to_ar</span><span class='lparen'>(</span><span class='id identifier rubyid_klass'>klass</span><span class='comma'>,</span> <span class='id identifier rubyid_val'>val</span><span class='comma'>,</span> <span class='id identifier rubyid_base_object'>base_object</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
<span class='kw'>return</span> <span class='kw'>nil</span> <span class='kw'>unless</span> <span class='id identifier rubyid_val'>val</span>
<span class='id identifier rubyid_data'>data</span> <span class='op'>=</span> <span class='id identifier rubyid_val'>val</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span> <span class='op'>==</span> <span class='const'>Hash</span> <span class='op'>?</span> <span class='id identifier rubyid_val'>val</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span> <span class='op'>:</span> <span class='const'>JSON</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_val'>val</span><span class='comma'>,</span> <span class='label'>symbolize_names:</span> <span class='kw'>true</span><span class='rparen'>)</span>
<span class='id identifier rubyid_obj'>obj</span> <span class='op'>=</span> <span class='id identifier rubyid_base_object'>base_object</span> <span class='op'>||</span> <span class='id identifier rubyid_klass'>klass</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
<span class='id identifier rubyid_obj_associations'>obj_associations</span> <span class='op'>=</span> <span class='id identifier rubyid_klass'>klass</span><span class='period'>.</span><span class='id identifier rubyid_reflect_on_all_associations'>reflect_on_all_associations</span><span class='lparen'>(</span><span class='symbol'>:has_many</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_reduce'>reduce</span><span class='lparen'>(</span><span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_reflection'>reflection</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
<span class='id identifier rubyid_reflection'>reflection</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='period'>.</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:through</span><span class='rbracket'>]</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_i'>i</span> <span class='kw'>if</span> <span class='id identifier rubyid_i'>i</span><span class='period'>.</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:through</span><span class='rbracket'>]</span>
<span class='id identifier rubyid_reflection'>reflection</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_obj_attribute_names'>obj_attribute_names</span> <span class='op'>=</span> <span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_attributes'>attributes</span><span class='period'>.</span><span class='id identifier rubyid_transform_keys'>transform_keys</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='symbol'>:to_sym</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_keys'>keys</span>
<span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_except'>except</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_obj_attribute_names'>obj_attribute_names</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_k'>k</span><span class='comma'>,</span> <span class='id identifier rubyid_v'>v</span><span class='op'>|</span>
<span class='id identifier rubyid_association'>association</span> <span class='op'>=</span> <span class='id identifier rubyid_klass'>klass</span><span class='period'>.</span><span class='id identifier rubyid_reflect_on_association'>reflect_on_association</span><span class='lparen'>(</span><span class='id identifier rubyid_k'>k</span><span class='rparen'>)</span>
<span class='kw'>next</span> <span class='kw'>unless</span> <span class='id identifier rubyid_association'>association</span>
<span class='kw'>case</span> <span class='id identifier rubyid_association'>association</span><span class='period'>.</span><span class='id identifier rubyid_macro'>macro</span>
<span class='kw'>when</span> <span class='symbol'>:belongs_to</span>
<span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_k'>k</span><span class='embexpr_end'>}</span><span class='tstring_content'>_id</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
<span class='comment'># Polymorphic associations do not auto-create the &#39;build_model&#39; method
</span> <span class='kw'>next</span> <span class='kw'>if</span> <span class='id identifier rubyid_association'>association</span><span class='period'>.</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:polymorphic</span><span class='rbracket'>]</span>
<span class='id identifier rubyid_to_ar'>to_ar</span><span class='lparen'>(</span><span class='id identifier rubyid_association'>association</span><span class='period'>.</span><span class='id identifier rubyid_klass'>klass</span><span class='comma'>,</span> <span class='id identifier rubyid_v'>v</span><span class='comma'>,</span> <span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>build_</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_k'>k</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_class_eval'>class_eval</span> <span class='kw'>do</span>
<span class='id identifier rubyid_define_method'>define_method</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_k'>k</span><span class='embexpr_end'>}</span><span class='tstring_content'>_id</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='id identifier rubyid_k'>k</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_id'>id</span> <span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='kw'>when</span> <span class='symbol'>:has_one</span>
<span class='id identifier rubyid_to_ar'>to_ar</span><span class='lparen'>(</span><span class='id identifier rubyid_association'>association</span><span class='period'>.</span><span class='id identifier rubyid_klass'>klass</span><span class='comma'>,</span> <span class='id identifier rubyid_v'>v</span><span class='comma'>,</span> <span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>build_</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_k'>k</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='kw'>when</span> <span class='symbol'>:has_many</span>
<span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='id identifier rubyid_k'>k</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_proxy_association'>proxy_association</span><span class='period'>.</span><span class='id identifier rubyid_target'>target</span> <span class='op'>=</span>
<span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_i'>i</span><span class='op'>|</span> <span class='id identifier rubyid_to_ar'>to_ar</span><span class='lparen'>(</span><span class='id identifier rubyid_association'>association</span><span class='period'>.</span><span class='id identifier rubyid_klass'>klass</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
<span class='id identifier rubyid_as_th'>as_th</span> <span class='op'>=</span> <span class='id identifier rubyid_obj_associations'>obj_associations</span><span class='lbracket'>[</span><span class='id identifier rubyid_k'>k</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='rbracket'>]</span>
<span class='kw'>if</span> <span class='id identifier rubyid_as_th'>as_th</span>
<span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_send'>send</span><span class='lparen'>(</span><span class='id identifier rubyid_as_th'>as_th</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_proxy_association'>proxy_association</span><span class='period'>.</span><span class='id identifier rubyid_target'>target</span> <span class='op'>=</span>
<span class='id identifier rubyid_v'>v</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_i'>i</span><span class='op'>|</span> <span class='id identifier rubyid_to_ar'>to_ar</span><span class='lparen'>(</span><span class='id identifier rubyid_as_th'>as_th</span><span class='period'>.</span><span class='id identifier rubyid_klass'>klass</span><span class='comma'>,</span> <span class='id identifier rubyid_i'>i</span><span class='lbracket'>[</span><span class='id identifier rubyid_as_th'>as_th</span><span class='period'>.</span><span class='id identifier rubyid_source_reflection_name'>source_reflection_name</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='rbrace'>}</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_assign_attributes'>assign_attributes</span><span class='lparen'>(</span><span class='id identifier rubyid_data'>data</span><span class='period'>.</span><span class='id identifier rubyid_slice'>slice</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_obj_attribute_names'>obj_attribute_names</span><span class='rparen'>)</span><span class='rparen'>)</span>
<span class='id identifier rubyid_obj'>obj</span><span class='period'>.</span><span class='id identifier rubyid_instance_eval'>instance_eval</span> <span class='kw'>do</span>
<span class='comment'># prevent save
</span> <span class='kw'>def</span> <span class='id identifier rubyid_valid?'>valid?</span><span class='lparen'>(</span><span class='id identifier rubyid__context'>_context</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
<span class='kw'>false</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_obj'>obj</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
</div>
</div>
<div id="footer">
Generated on Tue Apr 23 22:36:49 2019 by
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
0.9.19 (ruby-2.3.3).
</div>
</div>
</body>
</html>