haveapi 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/lib/haveapi/action.rb +521 -0
  3. data/lib/haveapi/actions/default.rb +55 -0
  4. data/lib/haveapi/actions/paginable.rb +12 -0
  5. data/lib/haveapi/api.rb +66 -0
  6. data/lib/haveapi/authentication/base.rb +37 -0
  7. data/lib/haveapi/authentication/basic/provider.rb +37 -0
  8. data/lib/haveapi/authentication/chain.rb +110 -0
  9. data/lib/haveapi/authentication/token/provider.rb +166 -0
  10. data/lib/haveapi/authentication/token/resources.rb +107 -0
  11. data/lib/haveapi/authorization.rb +108 -0
  12. data/lib/haveapi/common.rb +38 -0
  13. data/lib/haveapi/context.rb +78 -0
  14. data/lib/haveapi/example.rb +36 -0
  15. data/lib/haveapi/extensions/action_exceptions.rb +25 -0
  16. data/lib/haveapi/extensions/base.rb +9 -0
  17. data/lib/haveapi/extensions/resource_prefetch.rb +7 -0
  18. data/lib/haveapi/hooks.rb +190 -0
  19. data/lib/haveapi/metadata.rb +56 -0
  20. data/lib/haveapi/model_adapter.rb +119 -0
  21. data/lib/haveapi/model_adapters/active_record.rb +352 -0
  22. data/lib/haveapi/model_adapters/hash.rb +27 -0
  23. data/lib/haveapi/output_formatter.rb +57 -0
  24. data/lib/haveapi/output_formatters/base.rb +29 -0
  25. data/lib/haveapi/output_formatters/json.rb +9 -0
  26. data/lib/haveapi/params/param.rb +114 -0
  27. data/lib/haveapi/params/resource.rb +109 -0
  28. data/lib/haveapi/params.rb +314 -0
  29. data/lib/haveapi/public/css/bootstrap-theme.min.css +7 -0
  30. data/lib/haveapi/public/css/bootstrap.min.css +7 -0
  31. data/lib/haveapi/public/js/bootstrap.min.js +6 -0
  32. data/lib/haveapi/public/js/jquery-1.11.1.min.js +4 -0
  33. data/lib/haveapi/resource.rb +120 -0
  34. data/lib/haveapi/route.rb +22 -0
  35. data/lib/haveapi/server.rb +440 -0
  36. data/lib/haveapi/spec/helpers.rb +103 -0
  37. data/lib/haveapi/types.rb +24 -0
  38. data/lib/haveapi/version.rb +3 -0
  39. data/lib/haveapi/views/doc_layout.erb +27 -0
  40. data/lib/haveapi/views/doc_sidebars/create-client.erb +20 -0
  41. data/lib/haveapi/views/doc_sidebars/protocol.erb +42 -0
  42. data/lib/haveapi/views/index.erb +12 -0
  43. data/lib/haveapi/views/main_layout.erb +50 -0
  44. data/lib/haveapi/views/version_page.erb +195 -0
  45. data/lib/haveapi/views/version_sidebar.erb +42 -0
  46. data/lib/haveapi.rb +22 -0
  47. metadata +242 -0
@@ -0,0 +1,50 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>API v<%= @v %> description</title>
6
+
7
+ <link rel="stylesheet" href="/css/bootstrap.min.css">
8
+ <script src="/js/jquery-1.11.1.min.js"></script>
9
+ <script src="/js/bootstrap.min.js"></script>
10
+
11
+ <style>
12
+ /*.resource { margin-left: 10px; }*/
13
+ /*.resource-body { margin-left: 20px; }*/
14
+ /*.actions { margin-left: 10px; }*/
15
+ .action { margin-left: 10px; }
16
+ .parameters { margin-left: 10px; }
17
+
18
+ .table-of-contents {
19
+ position: relative;
20
+ }
21
+ .affix {
22
+ width: inherit;
23
+ position: fixed;
24
+ }
25
+ .affix-bottom {
26
+ width: inherit;
27
+ position: absolute;
28
+ top: auto;
29
+ bottom: 200px;
30
+ }
31
+ .affix-top {
32
+
33
+ }
34
+ .table-of-contents li.active > a {
35
+ font-weight: bold;
36
+ }
37
+ </style>
38
+ </head>
39
+ <body data-spy="scroll" data-target=".table-of-contents">
40
+ <div class="container-fluid">
41
+ <%= yield %>
42
+
43
+ <div class="row-fluid">
44
+ <div class="col-xs-12">
45
+ <p class="text-right">Generated by <a href="https://github.com/vpsfreecz/haveapi">HaveAPI</a> v<%= version %></p>
46
+ </div>
47
+ </div>
48
+ </div>
49
+ </body>
50
+ </html>
@@ -0,0 +1,195 @@
1
+ <% def render_resource_body(resource, info, prefix='root', name=nil) %>
2
+ <% name ||= resource.humanize %>
3
+ <h2 class="resource" id="<%= "#{prefix}-#{resource}" %>"><%= resource.humanize %></h2>
4
+ <div class="resource-body">
5
+ <p><%= info[:description] %></p>
6
+
7
+ <div class="actions">
8
+ <% info[:actions].each do |action, info| %>
9
+ <h3 id="<%= "#{prefix}-#{resource}-#{action}" %>"><%= name %> # <%= action.capitalize %></h3>
10
+ <div class="action">
11
+ <dl>
12
+ <dt>URL:</dt>
13
+ <dd><%= info[:method] %> <%= info[:url] %></dd>
14
+ <dt>Description:</dt>
15
+ <dd><%= info[:description] %></dd>
16
+ <dt>Authentication required:</dt>
17
+ <dd><%= info[:auth] ? 'yes' : 'no' %></dd>
18
+ <dt>Aliases:</dt>
19
+ <dd><%= info[:aliases].join(', ') %></dd>
20
+ </dl>
21
+
22
+ <h4>Input parameters</h4>
23
+ <div class="parameters">
24
+ <% if info[:input][:parameters].empty? %>
25
+ <p>No parameters.</p>
26
+ <% else %>
27
+ <dl>
28
+ <dt>Layout:</dt>
29
+ <dd><%= info[:input][:layout] %></dd>
30
+ <dt>Namespace:</dt>
31
+ <dd><%= info[:input][:namespace] %></dd>
32
+ </dl>
33
+
34
+ <table class="table table-striped table-hover">
35
+ <tr>
36
+ <th>Label</th>
37
+ <th>Name</th>
38
+ <th>Required</th>
39
+ <th>Type</th>
40
+ <th>Validators</th>
41
+ <th>Default</th>
42
+ <th>Description</th>
43
+ </tr>
44
+ <% info[:input][:parameters].each do |param, info| %>
45
+ <tr>
46
+ <td><%= info[:label] %></td>
47
+ <td><%= param %></td>
48
+ <td><%= info[:required] ? 'yes' : 'no' %></td>
49
+ <td><%= info[:type] %></td>
50
+ <td><%= info[:validators] %></td>
51
+ <td><%= info[:default] == :_nil ? '' : info[:default] %></td>
52
+ <td><%= info[:description] %></td>
53
+ </tr>
54
+ <% end %>
55
+ </table>
56
+ <% end %>
57
+ </div>
58
+
59
+ <h4>Output parameters</h4>
60
+ <div class="parameters">
61
+ <% if info[:output][:parameters].empty? && info[:output][:format].nil? %>
62
+ <p>No parameters.</p>
63
+ <% else %>
64
+ <dl>
65
+ <dt>Layout:</dt>
66
+ <dd><%= info[:output][:layout] %></dd>
67
+ <dt>Namespace:</dt>
68
+ <dd><%= info[:output][:namespace] %></dd>
69
+ </dl>
70
+
71
+ <table class="table table-striped table-hover">
72
+ <tr>
73
+ <th>Label</th>
74
+ <th>Name</th>
75
+ <th>Type</th>
76
+ <th>Description</th>
77
+ </tr>
78
+ <% info[:output][:parameters].each do |param, info| %>
79
+ <tr>
80
+ <td><%= info[:label] %></td>
81
+ <td><%= param %></td>
82
+ <td><%= info[:type] %></td>
83
+ <td><%= info[:description] %></td>
84
+ </tr>
85
+ <% end %>
86
+ </table>
87
+ <% end %>
88
+ </div>
89
+
90
+ <div class="action-self-description">
91
+ <h4>Self-description</h4>
92
+ <pre><code><%= JSON.pretty_generate(info) %></code></pre>
93
+ </div>
94
+
95
+ <% unless info[:examples].empty? %>
96
+ <h4>Examples</h4>
97
+ <% info[:examples].each do |example| %>
98
+ <h5><%= example[:label] %></h5>
99
+ <p><%= example[:comment] %></p>
100
+
101
+ <h6>Request</h6>
102
+ <pre><code><%= JSON.pretty_generate(example[:request]) %></code></pre>
103
+
104
+ <h6>Response</h6>
105
+ <pre><code><%= JSON.pretty_generate(example[:response]) %></code></pre>
106
+ <% end %>
107
+ <% end %>
108
+
109
+ </div>
110
+ <% end %>
111
+ </div>
112
+
113
+ <% unless info[:resources].empty? %>
114
+ <% info[:resources].each do |r, i| %>
115
+ <% render_resource_body(r, i, "#{prefix}-#{resource}", "#{name}.#{r.humanize}") %>
116
+ <% end %>
117
+ <% end %>
118
+
119
+ </div> <!-- resource -->
120
+ <% end %>
121
+
122
+ <h1 id="api">API v<%= @v %></h1>
123
+ <p>
124
+ This page contains a list of resources available in API v<%= @v %>, their actions,
125
+ description, parameters and example usage.
126
+ </p>
127
+
128
+ <h1 id="resources">Resources</h1>
129
+ <p>Follows a list of all resources in this API and their actions.</p>
130
+
131
+ <% @help[:resources].each do |resource, info| %>
132
+ <% render_resource_body(resource, info) %>
133
+ <% end %>
134
+ </div>
135
+
136
+ <script type="text/javascript">
137
+ $(document).ready(function(){
138
+ // Login/logout user
139
+ $('.authentication a.login').click(function(event){
140
+ $.ajax({
141
+ type: 'GET',
142
+ url: this.href,
143
+ async: false,
144
+ complete: function (){
145
+ window.location.reload();
146
+ }
147
+ });
148
+
149
+ event.preventDefault();
150
+ });
151
+
152
+ $('.authentication a.logout').click(function(event){
153
+ $.ajax({
154
+ type: 'GET',
155
+ url: this.href,
156
+ async: true,
157
+ username: '_log',
158
+ password: 'out',
159
+ complete: function() {
160
+ window.location.reload();
161
+ }
162
+ });
163
+
164
+ event.preventDefault();
165
+ });
166
+
167
+ // Show/hide button for help messages
168
+ $('.action .action-self-description').each(function(i, el){
169
+ // replace text by show/hide button
170
+ var div = $(el);
171
+ var pre = div.find('pre');
172
+ var h4 = div.find('h4');
173
+ h4.text('');
174
+
175
+ h4.append(
176
+ $('<button>')
177
+ .attr('type', 'button')
178
+ .addClass('btn btn-primary btn-sm')
179
+ .text('Show self-description')
180
+ .click(function(event) {
181
+ var button = $(this);
182
+ button.toggleClass('active');
183
+ pre.toggle('fast');
184
+
185
+ if(button.hasClass('active'))
186
+ button.text('Hide self-description');
187
+ else
188
+ button.text('Show self-description');
189
+ })
190
+ );
191
+
192
+ $(el).find('pre').hide();
193
+ });
194
+ });
195
+ </script>
@@ -0,0 +1,42 @@
1
+ <% def render_resource_nav(resource, info, prefix='root') %>
2
+ <li>
3
+ <a href="#<%= "#{prefix}-#{resource}" %>">.<%= resource.humanize %></a>
4
+
5
+ <ul>
6
+ <% info[:actions].each do |action, info| %>
7
+ <li>
8
+ <a href="#<%= "#{prefix}-#{resource}-#{action}" %>">#<%= action.capitalize %></a>
9
+ </li>
10
+ <% end %>
11
+
12
+ <% info[:resources].each do |r, i| %>
13
+ <% render_resource_nav(r, i, "#{prefix}-#{resource}") %>
14
+ <% end %>
15
+ </ul>
16
+ </li>
17
+ <% end %>
18
+
19
+ <h1>Authentication</h1>
20
+ <p class="authentication">
21
+ <% if current_user %>
22
+ Logged as <%= current_user.login %> [<a class="logout" href="<%= logout_url %>">logout</a>]
23
+ <% else %>
24
+ <a class="login btn btn-default" href="<%= url("#{root}_login") %>">Login</a>
25
+ <% end %>
26
+ </p>
27
+ <p>
28
+ <% if current_user %>
29
+ Listing only accessible resources, actions and parameters.
30
+ <% else %>
31
+ Listing all resources, actions and parameters.
32
+ <% end %>
33
+ </p>
34
+
35
+ <h1>Contents</h1>
36
+ <ul class="top-level">
37
+ <li><a href="#api">API v<%= @v %></a></li>
38
+ <li><a href="#resources">Resources</a></li>
39
+ <% @help[:resources].each do |resource, info| %>
40
+ <% render_resource_nav(resource, info) %>
41
+ <% end %>
42
+ </ul>
data/lib/haveapi.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'require_all'
2
+ require 'active_record'
3
+ require 'sinatra/base'
4
+ require 'sinatra/activerecord'
5
+ require 'pp'
6
+ require 'github/markdown'
7
+
8
+ module HaveAPI
9
+ module Actions
10
+ end
11
+ end
12
+
13
+ require_relative 'haveapi/params'
14
+ require_rel 'haveapi/params/'
15
+ require_rel 'haveapi/*.rb'
16
+ require_rel 'haveapi/model_adapters/'
17
+ require_rel 'haveapi/authentication'
18
+ require_rel 'haveapi/actions/*.rb'
19
+ require_rel 'haveapi/output_formatters/base.rb'
20
+ require_rel 'haveapi/output_formatters/'
21
+ require_rel 'haveapi/extensions'
22
+
metadata ADDED
@@ -0,0 +1,242 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haveapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Jakub Skokan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.1.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.1.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: require_all
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mysql
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sinatra-activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: github-markdown
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.6.6
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: 0.6.6
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: railties
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rack-test
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Framework for creating self-describing APIs
168
+ email: jakub.skokan@vpsfree.cz
169
+ executables: []
170
+ extensions: []
171
+ extra_rdoc_files: []
172
+ files:
173
+ - lib/haveapi.rb
174
+ - lib/haveapi/action.rb
175
+ - lib/haveapi/actions/default.rb
176
+ - lib/haveapi/actions/paginable.rb
177
+ - lib/haveapi/api.rb
178
+ - lib/haveapi/authentication/base.rb
179
+ - lib/haveapi/authentication/basic/provider.rb
180
+ - lib/haveapi/authentication/chain.rb
181
+ - lib/haveapi/authentication/token/provider.rb
182
+ - lib/haveapi/authentication/token/resources.rb
183
+ - lib/haveapi/authorization.rb
184
+ - lib/haveapi/common.rb
185
+ - lib/haveapi/context.rb
186
+ - lib/haveapi/example.rb
187
+ - lib/haveapi/extensions/action_exceptions.rb
188
+ - lib/haveapi/extensions/base.rb
189
+ - lib/haveapi/extensions/resource_prefetch.rb
190
+ - lib/haveapi/hooks.rb
191
+ - lib/haveapi/metadata.rb
192
+ - lib/haveapi/model_adapter.rb
193
+ - lib/haveapi/model_adapters/active_record.rb
194
+ - lib/haveapi/model_adapters/hash.rb
195
+ - lib/haveapi/output_formatter.rb
196
+ - lib/haveapi/output_formatters/base.rb
197
+ - lib/haveapi/output_formatters/json.rb
198
+ - lib/haveapi/params.rb
199
+ - lib/haveapi/params/param.rb
200
+ - lib/haveapi/params/resource.rb
201
+ - lib/haveapi/public/css/bootstrap-theme.min.css
202
+ - lib/haveapi/public/css/bootstrap.min.css
203
+ - lib/haveapi/public/js/bootstrap.min.js
204
+ - lib/haveapi/public/js/jquery-1.11.1.min.js
205
+ - lib/haveapi/resource.rb
206
+ - lib/haveapi/route.rb
207
+ - lib/haveapi/server.rb
208
+ - lib/haveapi/spec/helpers.rb
209
+ - lib/haveapi/types.rb
210
+ - lib/haveapi/version.rb
211
+ - lib/haveapi/views/doc_layout.erb
212
+ - lib/haveapi/views/doc_sidebars/create-client.erb
213
+ - lib/haveapi/views/doc_sidebars/protocol.erb
214
+ - lib/haveapi/views/index.erb
215
+ - lib/haveapi/views/main_layout.erb
216
+ - lib/haveapi/views/version_page.erb
217
+ - lib/haveapi/views/version_sidebar.erb
218
+ homepage:
219
+ licenses:
220
+ - MIT
221
+ metadata: {}
222
+ post_install_message:
223
+ rdoc_options: []
224
+ require_paths:
225
+ - lib
226
+ required_ruby_version: !ruby/object:Gem::Requirement
227
+ requirements:
228
+ - - ~>
229
+ - !ruby/object:Gem::Version
230
+ version: '2.0'
231
+ required_rubygems_version: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - '>='
234
+ - !ruby/object:Gem::Version
235
+ version: '0'
236
+ requirements: []
237
+ rubyforge_project:
238
+ rubygems_version: 2.2.2
239
+ signing_key:
240
+ specification_version: 4
241
+ summary: Framework for creating self-describing APIs
242
+ test_files: []