apipie-rails 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +8 -10
- data/app/controllers/apipie/apipies_controller.rb +12 -4
- data/app/views/apipie/apipies/_disqus.html.erb +11 -0
- data/app/views/apipie/apipies/apipie_404.html.erb +12 -0
- data/app/views/apipie/apipies/method.html.erb +1 -1
- data/app/views/apipie/apipies/resource.html.erb +1 -1
- data/app/views/layouts/apipie/apipie.html.erb +4 -15
- data/lib/apipie-rails.rb +1 -0
- data/lib/apipie/apipie_module.rb +10 -2
- data/lib/apipie/client/base.rb +131 -0
- data/lib/apipie/client/{template/cli_command.rb.tt → cli_command.rb} +21 -22
- data/lib/apipie/client/generator.rb +33 -30
- data/lib/apipie/client/{template/bin.rb.tt → main.rb} +13 -30
- data/lib/apipie/client/{template/rest_client_oauth.rb.tt → rest_client_oauth.rb} +1 -11
- data/lib/apipie/client/template/{client.gemspec.tt → a_name.gemspec.tt} +1 -0
- data/lib/apipie/client/template/bin/bin.rb.tt +30 -0
- data/lib/apipie/client/template/lib/a_name.rb.tt +27 -0
- data/lib/apipie/client/template/{cli.rb.tt → lib/a_name/commands/cli.rb.tt} +5 -8
- data/lib/apipie/client/template/lib/a_name/config.yml +6 -0
- data/lib/apipie/client/template/lib/a_name/resources/resource.rb.tt +22 -0
- data/lib/apipie/client/template/{version.rb.tt → lib/a_name/version.rb.tt} +0 -0
- data/lib/apipie/client/thor.rb +19 -0
- data/lib/apipie/dsl_definition.rb +3 -3
- data/lib/apipie/errors.rb +38 -0
- data/lib/apipie/helpers.rb +17 -0
- data/lib/apipie/param_description.rb +19 -10
- data/lib/apipie/resource_description.rb +2 -2
- data/lib/apipie/validator.rb +13 -33
- data/lib/apipie/version.rb +1 -1
- data/lib/tasks/apipie.rake +5 -0
- data/rubygem-apipie-rails.spec +80 -41
- data/spec/controllers/users_controller_spec.rb +20 -17
- data/spec/dummy/app/controllers/users_controller.rb +1 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -7
- data/spec/dummy/config/initializers/apipie.rb +15 -0
- data/spec/lib/param_description_spec.rb +0 -0
- data/spec/lib/parameter_description_spec.rb +41 -0
- metadata +50 -40
- data/lib/apipie/client/template/base.rb.tt +0 -55
- data/lib/apipie/client/template/client.rb.tt +0 -13
- data/lib/apipie/client/template/resource.rb.tt +0 -19
data/README.rdoc
CHANGED
@@ -73,6 +73,7 @@ of full descriptions.
|
|
73
73
|
need RedCloth. Add those to your gemfile and run bundle if you
|
74
74
|
want to use them. You can also add any other markup language
|
75
75
|
processor.
|
76
|
+
[layout] Name of a layout template to use instead of Apipie's layout. You can use Apipie.include_stylesheets and Apipie.include_javascripts helpers to include Apipie's stylesheets and javascripts.
|
76
77
|
|
77
78
|
Example:
|
78
79
|
|
@@ -313,13 +314,8 @@ So we create apipie_validators.rb initializer with this content:
|
|
313
314
|
end
|
314
315
|
end
|
315
316
|
|
316
|
-
def error
|
317
|
-
"Parameter \#{@param_name} expecting to be \#{@type.name},
|
318
|
-
got: \#{@error_value.class.name}"
|
319
|
-
end
|
320
|
-
|
321
317
|
def description
|
322
|
-
"
|
318
|
+
"Must be #{@type}."
|
323
319
|
end
|
324
320
|
end
|
325
321
|
|
@@ -362,8 +358,10 @@ or delete it if you want to use 'normal' dynamic version again.
|
|
362
358
|
|
363
359
|
== {Extractor}[https://github.com/Pajk/apipie-rails/wiki/Extractor]
|
364
360
|
|
365
|
-
==
|
361
|
+
== Authors
|
362
|
+
|
363
|
+
{Pajk}[https://github.com/Pajk] and {iNecas}[https://github.com/iNecas]
|
364
|
+
|
365
|
+
== Contributors
|
366
366
|
|
367
|
-
|
368
|
-
* Add documentation json API description to readme
|
369
|
-
* Possibility to create requests from web documentation and check responses
|
367
|
+
See {Contributors page}[https://github.com/Pajk/apipie-rails/graphs/contributors]. Special thanks to all of them!
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Apipie
|
2
2
|
class ApipiesController < ActionController::Base
|
3
|
-
layout
|
3
|
+
layout Apipie.configuration.layout
|
4
4
|
|
5
5
|
def index
|
6
6
|
respond_to do |format|
|
@@ -44,11 +44,19 @@ module Apipie
|
|
44
44
|
@doc = @doc[:docs]
|
45
45
|
if params[:resource].present? && params[:method].present?
|
46
46
|
@resource = @doc[:resources].first
|
47
|
-
@method = @resource[:methods].first
|
48
|
-
|
47
|
+
@method = @resource[:methods].first unless @resource == 'null'
|
48
|
+
if @resource == 'null' || @method == 'null'
|
49
|
+
render 'apipie_404', :status => 404
|
50
|
+
else
|
51
|
+
render 'method'
|
52
|
+
end
|
49
53
|
elsif params[:resource].present?
|
50
54
|
@resource = @doc[:resources].first
|
51
|
-
|
55
|
+
if @resource == 'null'
|
56
|
+
render 'apipie_404', :status => 404
|
57
|
+
else
|
58
|
+
render 'resource'
|
59
|
+
end
|
52
60
|
else
|
53
61
|
render 'index'
|
54
62
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<div id="disqus_thread"></div>
|
2
|
+
<script type="text/javascript">
|
3
|
+
var disqus_shortname = "<%= Apipie.configuration.disqus_shortname %>";
|
4
|
+
(function() {
|
5
|
+
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
|
6
|
+
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
|
7
|
+
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
|
8
|
+
})();
|
9
|
+
</script>
|
10
|
+
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
|
11
|
+
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<h1 class='page-header'>
|
2
|
+
Oops!
|
3
|
+
<small>
|
4
|
+
<% if @resource == 'null' %>
|
5
|
+
Resource <code><%= params[:resource] %></code> not found.
|
6
|
+
<% else %>
|
7
|
+
Method <code><%= params[:method] %></code> not found for resource <code><%= params[:resource] %></code>.
|
8
|
+
<% end %>
|
9
|
+
</small>
|
10
|
+
</h1>
|
11
|
+
|
12
|
+
Try going to <a href='<%= @doc[:doc_url] %>.html'><%= @doc[:name] %> API documentation homepage</a>
|
@@ -3,13 +3,8 @@
|
|
3
3
|
<head>
|
4
4
|
<title>API documentation</title>
|
5
5
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
6
|
-
|
7
|
-
|
8
|
-
bundled/prettify.css
|
9
|
-
bundled/bootstrap-responsive.min.css ].each do |file| %>
|
10
|
-
<link type="text/css" rel="stylesheet"
|
11
|
-
href="<%= Apipie.full_url("stylesheets/#{file}") %>"/>
|
12
|
-
<% end %>
|
6
|
+
<%= Apipie.include_stylesheets %>
|
7
|
+
<link type='text/css' rel='stylesheet' href='<%= Apipie.full_url("stylesheets/application.css")%>'/>
|
13
8
|
<!-- IE6-8 support of HTML5 elements -->
|
14
9
|
<!--[if lt IE 9]>
|
15
10
|
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
|
@@ -20,18 +15,12 @@
|
|
20
15
|
<div class="row">
|
21
16
|
<div id='container'>
|
22
17
|
<%= yield %>
|
18
|
+
<%= render 'disqus' if Apipie.configuration.use_disqus? %>
|
23
19
|
</div>
|
24
20
|
</div>
|
25
21
|
<hr>
|
26
22
|
<footer><%= yield :apipie_footer %></footer>
|
27
23
|
</div>
|
28
|
-
|
29
|
-
bundled/jquery-1.7.2.js
|
30
|
-
bundled/bootstrap-collapse.js
|
31
|
-
bundled/prettify.js
|
32
|
-
apipie.js ].each do |file| %>
|
33
|
-
<script type="text/javascript"
|
34
|
-
src="<%= Apipie.full_url("javascripts/#{file}") %>"></script>
|
35
|
-
<% end %>
|
24
|
+
<%= Apipie.include_javascripts %>
|
36
25
|
</body>
|
37
26
|
</html>
|
data/lib/apipie-rails.rb
CHANGED
@@ -4,6 +4,7 @@ require "apipie/apipie_module"
|
|
4
4
|
require "apipie/method_description"
|
5
5
|
require "apipie/resource_description"
|
6
6
|
require "apipie/param_description"
|
7
|
+
require "apipie/errors"
|
7
8
|
require "apipie/error_description"
|
8
9
|
require "apipie/validator"
|
9
10
|
require "apipie/dsl_definition"
|
data/lib/apipie/apipie_module.rb
CHANGED
@@ -26,10 +26,11 @@ module Apipie
|
|
26
26
|
end
|
27
27
|
|
28
28
|
class Configuration
|
29
|
-
attr_accessor :app_name, :app_info, :copyright, :markup,
|
30
|
-
:validate, :api_base_url, :doc_base_url
|
29
|
+
attr_accessor :app_name, :app_info, :copyright, :markup, :disqus_shortname,
|
30
|
+
:validate, :api_base_url, :doc_base_url, :required_by_default, :layout
|
31
31
|
|
32
32
|
alias_method :validate?, :validate
|
33
|
+
alias_method :required_by_default?, :required_by_default
|
33
34
|
|
34
35
|
# matcher to be used in Dir.glob to find controllers to be reloaded e.g.
|
35
36
|
#
|
@@ -87,6 +88,10 @@ module Apipie
|
|
87
88
|
@generated_doc_disclaimer ||= "# DOC GENERATED AUTOMATICALLY: REMOVE THIS LINE TO PREVENT REGENARATING NEXT TIME"
|
88
89
|
end
|
89
90
|
|
91
|
+
def use_disqus?
|
92
|
+
!@disqus_shortname.blank?
|
93
|
+
end
|
94
|
+
|
90
95
|
def app_info
|
91
96
|
Apipie.markup_to_html(@app_info)
|
92
97
|
end
|
@@ -97,8 +102,11 @@ module Apipie
|
|
97
102
|
@app_info = "Another API description"
|
98
103
|
@copyright = nil
|
99
104
|
@validate = true
|
105
|
+
@required_by_default = false
|
100
106
|
@api_base_url = ""
|
101
107
|
@doc_base_url = "/apipie"
|
108
|
+
@layout = "apipie/apipie"
|
109
|
+
@disqus_shortname = nil
|
102
110
|
end
|
103
111
|
end
|
104
112
|
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'rest_client'
|
2
|
+
require 'oauth'
|
3
|
+
require 'json'
|
4
|
+
require 'apipie/client/rest_client_oauth'
|
5
|
+
|
6
|
+
module Apipie::Client
|
7
|
+
|
8
|
+
class Base
|
9
|
+
attr_reader :client, :config
|
10
|
+
|
11
|
+
def initialize(config, options = { })
|
12
|
+
@client = RestClient::Resource.new config[:base_url],
|
13
|
+
:user => config[:username],
|
14
|
+
:password => config[:password],
|
15
|
+
:oauth => config[:oauth],
|
16
|
+
:headers => { :content_type => 'application/json',
|
17
|
+
:accept => 'application/json' }
|
18
|
+
@config = config
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(method, path, params = { }, headers = { })
|
22
|
+
headers ||= { }
|
23
|
+
|
24
|
+
args = [method]
|
25
|
+
if [:post, :put].include?(method)
|
26
|
+
args << params.to_json
|
27
|
+
else
|
28
|
+
headers[:params] = params if params
|
29
|
+
end
|
30
|
+
|
31
|
+
args << headers if headers
|
32
|
+
process_data client[path].send(*args)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.doc
|
36
|
+
raise NotImplementedError
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.validation_hash(method)
|
40
|
+
validation_hashes[method.to_s]
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.method_doc(method)
|
44
|
+
method_docs[method.to_s]
|
45
|
+
end
|
46
|
+
|
47
|
+
def validate_params!(params, rules)
|
48
|
+
return unless params.is_a?(Hash)
|
49
|
+
|
50
|
+
invalid_keys = params.keys.map(&:to_s) - (rules.is_a?(Hash) ? rules.keys : rules)
|
51
|
+
raise ArgumentError, "Invalid keys: #{invalid_keys.join(", ")}" unless invalid_keys.empty?
|
52
|
+
|
53
|
+
if rules.is_a? Hash
|
54
|
+
rules.each do |key, sub_keys|
|
55
|
+
validate_params!(params[key], sub_keys) if params[key]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
protected
|
61
|
+
|
62
|
+
def process_data(response)
|
63
|
+
data = begin
|
64
|
+
JSON.parse(response.body)
|
65
|
+
rescue JSON::ParserError
|
66
|
+
response.body
|
67
|
+
end
|
68
|
+
return data, response
|
69
|
+
end
|
70
|
+
|
71
|
+
def check_params(params, options = { })
|
72
|
+
raise ArgumentError unless (method = options[:method])
|
73
|
+
return unless config[:enable_validations]
|
74
|
+
|
75
|
+
case options[:allowed]
|
76
|
+
when true
|
77
|
+
validate_params!(params, self.class.validation_hash(method))
|
78
|
+
when false
|
79
|
+
raise ArgumentError, "this method '#{method}' does not support params" if params && !params.empty?
|
80
|
+
else
|
81
|
+
raise ArgumentError, "options :allowed should be true or false, it was #{options[:allowed]}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# @return url and rest of the params
|
86
|
+
def fill_params_in_url(url, params)
|
87
|
+
params ||= { }
|
88
|
+
# insert param values
|
89
|
+
url_param_names = params_in_path(url)
|
90
|
+
url = params_in_path(url).inject(url) do |url, param_name|
|
91
|
+
param_value = params[param_name] or
|
92
|
+
raise ArgumentError, "missing param '#{param_name}' in parameters"
|
93
|
+
url.sub(":#{param_name}", param_value.to_s)
|
94
|
+
end
|
95
|
+
|
96
|
+
return url, params.reject { |param_name, _| url_param_names.include? param_name }
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def self.method_docs
|
102
|
+
@method_docs ||= doc['methods'].inject({ }) do |hash, method|
|
103
|
+
hash[method['name']] = method
|
104
|
+
hash
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.validation_hashes
|
109
|
+
@validation_hashes ||= method_docs.inject({ }) do |hash, pair|
|
110
|
+
name, method_doc = pair
|
111
|
+
hash[name] = construct_validation_hash method_doc
|
112
|
+
hash
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.construct_validation_hash(method)
|
117
|
+
if method['params'].any? { |p| p['params'] }
|
118
|
+
method['params'].reduce({ }) do |h, p|
|
119
|
+
h.update(p['name'] => (p['params'] ? p['params'].map { |pp| pp['name'] } : nil))
|
120
|
+
end
|
121
|
+
else
|
122
|
+
method['params'].map { |p| p['name'] }
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def params_in_path(url)
|
127
|
+
url.scan(/:([^\/]*)/).map { |m| m.first }
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
@@ -1,34 +1,33 @@
|
|
1
|
-
module
|
1
|
+
module Apipie::Client
|
2
2
|
class CliCommand < Thor
|
3
3
|
no_tasks do
|
4
4
|
def client
|
5
|
-
resource_class =
|
6
|
-
@client
|
5
|
+
resource_class = apipie_options[:client_module]::Resources.const_get(self.class.name[/[^:]*$/])
|
6
|
+
@client ||= resource_class.new(apipie_options[:config])
|
7
7
|
end
|
8
8
|
|
9
|
-
def transform_options(inline_params, transform_hash = {})
|
10
|
-
ret = inline_params.map { |p| options[p] }
|
11
|
-
|
9
|
+
def transform_options(inline_params, transform_hash = { })
|
12
10
|
# we use not mentioned params without change
|
13
|
-
transformed_options = (options.keys - transform_hash.values.flatten - inline_params).reduce({}) { |h, k| h.update(k => options[k]) }
|
11
|
+
transformed_options = (options.keys - transform_hash.values.flatten - inline_params).reduce({ }) { |h, k| h.update(k => options[k]) }
|
12
|
+
|
13
|
+
inline_params.each { |p| transformed_options[p] = options[p] }
|
14
14
|
|
15
15
|
transform_hash.each do |sub_key, params|
|
16
|
-
transformed_options[sub_key] = {}
|
16
|
+
transformed_options[sub_key] = { }
|
17
17
|
params.each { |p| transformed_options[sub_key][p] = options[p] if options.has_key?(p) }
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
return *ret
|
20
|
+
return transformed_options
|
22
21
|
end
|
23
22
|
|
24
23
|
def print_data(data)
|
25
24
|
case data
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
when Array
|
26
|
+
print_big_table(table_from_array(data))
|
27
|
+
when Hash
|
28
|
+
print_table(table_from_hash(data))
|
29
|
+
else
|
30
|
+
print_unknown(data)
|
32
31
|
end
|
33
32
|
end
|
34
33
|
|
@@ -49,8 +48,8 @@ module <%= class_base %><%= class_suffix %>
|
|
49
48
|
|
50
49
|
def table_from_array(data)
|
51
50
|
return [] if data.empty?
|
52
|
-
table
|
53
|
-
items
|
51
|
+
table = []
|
52
|
+
items = data.map { |item| normalize_item_data(item) }
|
54
53
|
columns = items.first.keys
|
55
54
|
table << columns
|
56
55
|
items.each do |item|
|
@@ -64,7 +63,7 @@ module <%= class_base %><%= class_suffix %>
|
|
64
63
|
return [] if data.empty?
|
65
64
|
table = []
|
66
65
|
normalize_item_data(data).each do |k, v|
|
67
|
-
table << ["#{k}:",v].map(&:to_s)
|
66
|
+
table << ["#{k}:", v].map(&:to_s)
|
68
67
|
end
|
69
68
|
table
|
70
69
|
end
|
@@ -73,7 +72,7 @@ module <%= class_base %><%= class_suffix %>
|
|
73
72
|
say data
|
74
73
|
end
|
75
74
|
|
76
|
-
def print_big_table(table, options={})
|
75
|
+
def print_big_table(table, options={ })
|
77
76
|
return if table.empty?
|
78
77
|
|
79
78
|
formats, ident, colwidth = [], options[:ident].to_i, options[:colwidth]
|
@@ -83,7 +82,7 @@ module <%= class_base %><%= class_suffix %>
|
|
83
82
|
start = colwidth ? 1 : 0
|
84
83
|
|
85
84
|
start.upto(table.first.length - 2) do |i|
|
86
|
-
maxima ||= table.max{|a,b| a[i].size <=> b[i].size }[i].size
|
85
|
+
maxima ||= table.max { |a, b| a[i].size <=> b[i].size }[i].size
|
87
86
|
formats << "%-#{maxima + 2}s"
|
88
87
|
end
|
89
88
|
|
@@ -113,7 +112,7 @@ module <%= class_base %><%= class_suffix %>
|
|
113
112
|
Thor::Util.thor_classes_in(self).each do |klass|
|
114
113
|
list += printable_tasks(false)
|
115
114
|
end
|
116
|
-
list.sort!{ |a,b| a[0] <=> b[0] }
|
115
|
+
list.sort! { |a, b| a[0] <=> b[0] }
|
117
116
|
|
118
117
|
shell.say
|
119
118
|
shell.print_table(list, :indent => 2, :truncate => true)
|
@@ -5,6 +5,7 @@ require 'thor'
|
|
5
5
|
require 'thor/group'
|
6
6
|
require 'fileutils'
|
7
7
|
require 'active_support/inflector'
|
8
|
+
require 'apipie/client/base'
|
8
9
|
|
9
10
|
module Apipie
|
10
11
|
module Client
|
@@ -17,7 +18,7 @@ module Apipie
|
|
17
18
|
argument :subject
|
18
19
|
argument :suffix
|
19
20
|
|
20
|
-
attr_reader :doc, :resource
|
21
|
+
attr_reader :doc, :resource, :resource_key
|
21
22
|
|
22
23
|
def initialize(*args)
|
23
24
|
super
|
@@ -33,7 +34,7 @@ module Apipie
|
|
33
34
|
end
|
34
35
|
|
35
36
|
def self.start(client_name, subject = :all, suffix = '_client')
|
36
|
-
name
|
37
|
+
name = client_name.parameterize.underscore
|
37
38
|
suffix = suffix.parameterize.underscore
|
38
39
|
super([name, subject, suffix], :destination_root => destination_root(name, suffix))
|
39
40
|
end
|
@@ -47,22 +48,21 @@ module Apipie
|
|
47
48
|
template("README.tt", "README")
|
48
49
|
template("Gemfile.tt", "Gemfile")
|
49
50
|
template("Rakefile.tt", "Rakefile")
|
50
|
-
template("
|
51
|
-
template("
|
52
|
-
template("
|
53
|
-
|
54
|
-
|
51
|
+
template("a_name.gemspec.tt", "#{full_name}.gemspec")
|
52
|
+
template("lib/a_name.rb.tt", "lib/#{full_name}.rb")
|
53
|
+
template("lib/a_name/version.rb.tt", "lib/#{full_name}/version.rb")
|
54
|
+
create_file "lib/#{full_name}/documentation.json", JSON.dump(Apipie.to_json)
|
55
|
+
copy_file "lib/a_name/config.yml", "lib/#{full_name}/config.yml"
|
55
56
|
if all?
|
56
|
-
template("bin.rb.tt", "bin/#{full_name}")
|
57
|
+
template("bin/bin.rb.tt", "bin/#{full_name}")
|
57
58
|
chmod("bin/#{full_name}", 0755)
|
58
|
-
template("cli_command.rb.tt", "lib/#{full_name}/cli_command.rb")
|
59
59
|
end
|
60
60
|
doc[:resources].each do |key, resource|
|
61
|
-
@resource = resource
|
61
|
+
@resource_key, @resource = key, resource
|
62
62
|
if all?
|
63
|
-
template("cli.rb.tt", "lib/#{full_name}/commands/#{resource_name}.thor")
|
63
|
+
template("lib/a_name/commands/cli.rb.tt", "lib/#{full_name}/commands/#{resource_name}.thor")
|
64
64
|
end
|
65
|
-
template("resource.rb.tt", "lib/#{full_name}/resources/#{resource_name}.rb")
|
65
|
+
template("lib/a_name/resources/resource.rb.tt", "lib/#{full_name}/resources/#{resource_name}.rb")
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -82,13 +82,13 @@ module Apipie
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def plaintext(text)
|
85
|
-
text.gsub(/<.*?>/, '').gsub("\n",' ').strip
|
85
|
+
text.gsub(/<.*?>/, '').gsub("\n", ' ').strip
|
86
86
|
end
|
87
87
|
|
88
88
|
# Resource related helper methods:
|
89
89
|
|
90
90
|
def resource_name
|
91
|
-
resource[:name].gsub(/\s/,"_").downcase.singularize
|
91
|
+
resource[:name].gsub(/\s/, "_").downcase.singularize
|
92
92
|
end
|
93
93
|
|
94
94
|
def api(method)
|
@@ -100,31 +100,34 @@ module Apipie
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def client_args(method)
|
103
|
-
|
104
|
-
client_args << "params = {}"
|
105
|
-
client_args << 'headers = {}'
|
106
|
-
client_args
|
107
|
-
end
|
108
|
-
|
109
|
-
def validation_hash(method)
|
110
|
-
if method[:params].any? { |p| p[:params] }
|
111
|
-
method[:params].reduce({}) do |h, p|
|
112
|
-
h.update(p[:name] => (p[:params] ? p[:params].map { |pp| pp[:name] } : nil))
|
113
|
-
end
|
114
|
-
else
|
115
|
-
method[:params].map { |p| p[:name] }
|
116
|
-
end
|
103
|
+
params_in_path(method).dup
|
117
104
|
end
|
118
105
|
|
119
106
|
def substituted_url(method)
|
120
|
-
params_in_path(method).reduce(api(method)[:api_url]) { |u, p| u.sub(":#{p}","\#{#{p}}")}
|
107
|
+
params_in_path(method).reduce(api(method)[:api_url]) { |u, p| u.sub(":#{p}", "\#{#{p}}") }
|
121
108
|
end
|
122
109
|
|
123
110
|
def transformation_hash(method)
|
124
|
-
method[:params].find_all { |p| p[:expected_type] == "hash" && !p[:params].nil? }.reduce({}) do |h, p|
|
111
|
+
method[:params].find_all { |p| p[:expected_type] == "hash" && !p[:params].nil? }.reduce({ }) do |h, p|
|
125
112
|
h.update(p[:name] => p[:params].map { |pp| pp[:name] })
|
126
113
|
end
|
127
114
|
end
|
115
|
+
|
116
|
+
def validation(method)
|
117
|
+
stringify = lambda do |object|
|
118
|
+
case object
|
119
|
+
when Hash
|
120
|
+
clone = object.dup
|
121
|
+
object.keys.each { |key| clone[key.to_s] = stringify[clone.delete(key)] }
|
122
|
+
clone
|
123
|
+
when Array
|
124
|
+
object.map { |value| stringify[value] }
|
125
|
+
else
|
126
|
+
object
|
127
|
+
end
|
128
|
+
end
|
129
|
+
Apipie::Client::Base.construct_validation_hash(stringify[method])
|
130
|
+
end
|
128
131
|
end
|
129
132
|
|
130
133
|
end
|