Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

With this commits api docs keeps the same features, but it adds the

chance to customize the way you save to files the api docs
  • Loading branch information...
commit bafc1f05e039d98ff08fb7ac9b15df3655258348 1 parent bc33045
@chischaschos chischaschos authored
Showing with 37 additions and 30 deletions.
  1. +18 −12 lib/api_docs.rb
  2. +19 −18 lib/api_docs/test_helper.rb
View
30 lib/api_docs.rb
@@ -7,41 +7,47 @@ module ApiDocs
class << self
- attr_accessor :store_data_strategy, :file_name_strategy
+ attr_accessor :store_data_strategy, :file_name_strategy, :docs,
+ :automatic_write
def configure
yield configuration
end
+ def automatic_write?
+ self.automatic_write.nil? ? true : self.automatic_write
+ end
+
def configuration
@configuration ||= Configuration.new
end
alias :config :configuration
- def store_data(api_docs, captured_data)
+ def store_data(captured_data)
strategy = store_data_strategy || method(:default_store_data)
- strategy.call(api_docs, captured_data)
+ strategy.call(docs, captured_data, default_key(captured_data))
end
- def file_name(captured_data)
- strategy = file_name_strategy || method(:default_file_name)
- strategy.call(captured_data)
+ def docs
+ @docs ||= Hash.new
end
private
- def default_store_data(api_docs, captured_data)
- # Marking response as an unique
+ def default_store_data(api_docs, captured_data, key)
+ api_docs[captured_data['action']] ||= { }
+ api_docs[captured_data['action']][key] = captured_data
+ end
+
+ # Marking response as an unique
+ def default_key(captured_data)
key = ""
key << captured_data['method']
key << captured_data['path']
key << captured_data['meta'].to_s
key << captured_data['params'].to_s
key << captured_data['status'].to_s
- hashed_key = 'ID-' + Digest::MD5.hexdigest(key)
-
- api_docs[captured_data['action']] ||= { }
- api_docs[captured_data['action']][hashed_key] = captured_data
+ 'ID-' + Digest::MD5.hexdigest(key)
end
def default_file_name(captured_data)
View
37 lib/api_docs/test_helper.rb
@@ -11,14 +11,12 @@ def api_call(method, path, params = {}, headers = {})
parsed_path = path.dup
parsed_params = params.dup
-
parsed_params.each do |k, v|
parsed_params.delete(k) if parsed_path.gsub!(":#{k}", v.to_s)
end
# Making actual test request. Based on the example above:
# get '/users/12345'
-
send(method, parsed_path, parsed_params, headers)
meta = Hash.new
@@ -29,7 +27,7 @@ def api_call(method, path, params = {}, headers = {})
return unless ENV['API_DOCS']
end
- @captured_data = {
+ captured_data = {
'controller' => request.filtered_parameters['controller'],
'action' => request.filtered_parameters['action'],
'meta' => meta,
@@ -41,31 +39,34 @@ def api_call(method, path, params = {}, headers = {})
'body' => response.body
}
- ApiDocs.store_data(api_docs, @captured_data)
-
- write_docs
+ read_api_docs(captured_data) if ApiDocs.automatic_write?
+ ApiDocs.store_data(captured_data)
+ write_api_docs(captured_data) if ApiDocs.automatic_write?
end
- def api_docs
- @api_docs ||= if File.exists?(file_path)
- YAML.load_file(file_path) rescue Hash.new
+ def read_api_docs(captured_data)
+ calculated_file_path = file_path(captured_data)
+ ApiDocs.docs = if File.exists?(calculated_file_path)
+ YAML.load_file(calculated_file_path) rescue Hash.new
else
Hash.new
end
end
- def file_name
- ApiDocs.file_name(@captured_data)
+ def file_path(captured_data)
+ File.expand_path(file_name(captured_data), ApiDocs.config.docs_path)
end
- def file_path
- File.expand_path(file_name, ApiDocs.config.docs_path)
+ def file_name(captured_data)
+ "#{captured_data['controller'].gsub('/', ':')}.yml"
end
- def write_docs
- FileUtils.mkdir_p(File.dirname(file_path))
- File.open(file_path, 'w'){|f| f.write(api_docs.to_yaml)}
+ def write_api_docs(captured_data)
+ calculated_file_path = file_path(captured_data)
+ FileUtils.mkdir_p(File.dirname(calculated_file_path))
+ File.open(calculated_file_path, 'w') {|f| f.write(ApiDocs.docs.to_yaml)}
end
+
end
# Cleans up params. Removes things like File object handlers
@@ -74,10 +75,10 @@ def self.api_deep_clean_params(params)
case params
when Hash
params.each_with_object({}) do |(key, value), res|
- res[key.to_s] = ApiDocs::TestHelper.api_deep_clean_params(value)
+ res[key.to_s] = api_deep_clean_params(value)
end
when Array
- params.collect{|value| ApiDocs::TestHelper.api_deep_clean_params(value)}
+ params.collect{|value| api_deep_clean_params(value)}
else
case params
when Rack::Test::UploadedFile
Please sign in to comment.
Something went wrong with that request. Please try again.