-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.POST.html
139 lines (97 loc) · 6.85 KB
/
file.POST.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
File: POST
— Documentation by YARD 0.9.25
</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/common.css" type="text/css" />
<script type="text/javascript">
pathId = "POST";
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="file_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</a> »
<span class="title">File: POST</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"><div id='filecontents'><h1 id="implementing-a-post-endpoint">Implementing a POST endpoint</h1>
<h1 id="setup">Setup</h1>
<p>This assumes you have a Rails app with a model <code>Cheese</code> defined, mapping to a database (e.g. SQLite).
It should have one required column, <code>name</code>.</p>
<p>You should also have <a href="https://curl.se/">curl</a> installed.</p>
<h1 id="implementation">Implementation</h1>
<p>Add a route to your <strong>'config/routes.rb'</strong> :</p>
<pre class="code ruby"><code class="ruby"> <span class='id identifier rubyid_resources'>resources</span> <span class='symbol'>:cheeses</span>
</code></pre>
<p>Create a controller in <strong>'controllers/cheeses_controller'</strong>:</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>CheesesController</span> <span class='op'><</span> <span class='const'>ApplicationController</span>
<span class='id identifier rubyid_skip_before_action'>skip_before_action</span> <span class='symbol'>:verify_authenticity_token</span>
<span class='kw'>def</span> <span class='id identifier rubyid_create'>create</span>
<span class='const'><span class='object_link'><a href="Flappi.html" title="Flappi (module)">Flappi</a></span></span><span class='period'>.</span><span class='id identifier rubyid_build_and_respond'>build_and_respond</span><span class='lparen'>(</span><span class='kw'>self</span><span class='comma'>,</span> <span class='symbol'>:create</span><span class='rparen'>)</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
</code></pre>
<p>Note that we don't need an authenticity token as this is an API (you may do this in a superclass, something like: <code>ApiController</code>)</p>
<p>The second parameter to <code>Flappi.build_and_respond</code> sets the action - this causes a definition <code>Cheeses</code> + <code>Create</code> => <code>CheesesCreate</code> to be used.</p>
<p>Now, create your <code>CheesesCreate</code> definition class in <strong>'app/controllers/api_definitions/cheeses_create.rb'</strong>.</p>
<pre class="code ruby"><code class="ruby"><span class='kw'>module</span> <span class='const'>ApiDefinitions</span>
<span class='kw'>module</span> <span class='const'>CheesesCreate</span>
<span class='id identifier rubyid_include'>include</span> <span class='const'><span class='object_link'><a href="Flappi.html" title="Flappi (module)">Flappi</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Flappi/Definition.html" title="Flappi::Definition (module)">Definition</a></span></span>
<span class='kw'>def</span> <span class='id identifier rubyid_endpoint'>endpoint</span>
<span class='id identifier rubyid_title'>title</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Create a cheeses</span><span class='tstring_end'>'</span></span>
<span class='id identifier rubyid_http_method'>http_method</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>POST</span><span class='tstring_end'>'</span></span>
<span class='id identifier rubyid_path'>path</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>/cheeses</span><span class='tstring_end'>'</span></span>
<span class='id identifier rubyid_check_params'>check_params</span> <span class='kw'>true</span>
<span class='id identifier rubyid_param'>param</span> <span class='symbol'>:name</span><span class='comma'>,</span> <span class='label'>doc:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>The name of the cheese</span><span class='tstring_end'>'</span></span>
<span class='kw'>end</span>
<span class='comment'># Post data to the model's create! method
</span> <span class='kw'>def</span> <span class='id identifier rubyid_respond'>respond</span>
<span class='id identifier rubyid_build'>build</span> <span class='label'>type:</span> <span class='const'>Cheese</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:create!</span> <span class='kw'>do</span>
<span class='id identifier rubyid_field'>field</span> <span class='symbol'>:name</span><span class='comma'>,</span> <span class='label'>doc:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>The name of the cheese</span><span class='tstring_end'>'</span></span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
</code></pre>
<p>This implements a POST method <code>/cheeses</code>. It takes one parameter <code>name</code> which will be written into the created record.
Setting <code>check_params true</code> is needed for Rails permitted parameters.
The <code>build</code> statement results in a call to the <code>Cheese.create!</code> method with a hash of the params, creating a record.
The body of <code>build</code> defines the response, returning a hash with the <code>name</code>.</p>
<h1 id="testing">Testing</h1>
<p>Running the command:</p>
<p><code>curl -X POST -d name=Gruyere 'http://localhost:3000/cheeses.json'</code></p>
<p>should now create an entry in the database.</p>
</div></div>
<div id="footer">
Generated on Tue Dec 1 14:56:46 2020 by
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
0.9.25 (ruby-2.6.5).
</div>
</div>
</body>
</html>