Skip to content

Commit

Permalink
With this commits api docs keeps the same features, but it adds the
Browse files Browse the repository at this point in the history
chance to customize the way you save to files the api docs
  • Loading branch information
chischaschos committed Feb 13, 2014
1 parent bc33045 commit bafc1f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
30 changes: 18 additions & 12 deletions lib/api_docs.rb
Expand Up @@ -7,41 +7,47 @@ module ApiDocs


class << self class << self


attr_accessor :store_data_strategy, :file_name_strategy attr_accessor :store_data_strategy, :file_name_strategy, :docs,
:automatic_write


def configure def configure
yield configuration yield configuration
end end


def automatic_write?
self.automatic_write.nil? ? true : self.automatic_write
end

def configuration def configuration
@configuration ||= Configuration.new @configuration ||= Configuration.new
end end
alias :config :configuration alias :config :configuration


def store_data(api_docs, captured_data) def store_data(captured_data)
strategy = store_data_strategy || method(:default_store_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 end


def file_name(captured_data) def docs
strategy = file_name_strategy || method(:default_file_name) @docs ||= Hash.new
strategy.call(captured_data)
end end


private private


def default_store_data(api_docs, captured_data) def default_store_data(api_docs, captured_data, key)
# Marking response as an unique 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 = ""
key << captured_data['method'] key << captured_data['method']
key << captured_data['path'] key << captured_data['path']
key << captured_data['meta'].to_s key << captured_data['meta'].to_s
key << captured_data['params'].to_s key << captured_data['params'].to_s
key << captured_data['status'].to_s key << captured_data['status'].to_s
hashed_key = 'ID-' + Digest::MD5.hexdigest(key) 'ID-' + Digest::MD5.hexdigest(key)

api_docs[captured_data['action']] ||= { }
api_docs[captured_data['action']][hashed_key] = captured_data
end end


def default_file_name(captured_data) def default_file_name(captured_data)
Expand Down
37 changes: 19 additions & 18 deletions lib/api_docs/test_helper.rb
Expand Up @@ -11,14 +11,12 @@ def api_call(method, path, params = {}, headers = {})
parsed_path = path.dup parsed_path = path.dup
parsed_params = params.dup parsed_params = params.dup



parsed_params.each do |k, v| parsed_params.each do |k, v|
parsed_params.delete(k) if parsed_path.gsub!(":#{k}", v.to_s) parsed_params.delete(k) if parsed_path.gsub!(":#{k}", v.to_s)
end end


# Making actual test request. Based on the example above: # Making actual test request. Based on the example above:
# get '/users/12345' # get '/users/12345'

send(method, parsed_path, parsed_params, headers) send(method, parsed_path, parsed_params, headers)


meta = Hash.new meta = Hash.new
Expand All @@ -29,7 +27,7 @@ def api_call(method, path, params = {}, headers = {})
return unless ENV['API_DOCS'] return unless ENV['API_DOCS']
end end


@captured_data = { captured_data = {
'controller' => request.filtered_parameters['controller'], 'controller' => request.filtered_parameters['controller'],
'action' => request.filtered_parameters['action'], 'action' => request.filtered_parameters['action'],
'meta' => meta, 'meta' => meta,
Expand All @@ -41,31 +39,34 @@ def api_call(method, path, params = {}, headers = {})
'body' => response.body 'body' => response.body
} }


ApiDocs.store_data(api_docs, @captured_data) read_api_docs(captured_data) if ApiDocs.automatic_write?

ApiDocs.store_data(captured_data)
write_docs write_api_docs(captured_data) if ApiDocs.automatic_write?
end end


def api_docs def read_api_docs(captured_data)
@api_docs ||= if File.exists?(file_path) calculated_file_path = file_path(captured_data)
YAML.load_file(file_path) rescue Hash.new ApiDocs.docs = if File.exists?(calculated_file_path)
YAML.load_file(calculated_file_path) rescue Hash.new
else else
Hash.new Hash.new
end end
end end


def file_name def file_path(captured_data)
ApiDocs.file_name(@captured_data) File.expand_path(file_name(captured_data), ApiDocs.config.docs_path)
end end


def file_path def file_name(captured_data)
File.expand_path(file_name, ApiDocs.config.docs_path) "#{captured_data['controller'].gsub('/', ':')}.yml"
end end


def write_docs def write_api_docs(captured_data)
FileUtils.mkdir_p(File.dirname(file_path)) calculated_file_path = file_path(captured_data)
File.open(file_path, 'w'){|f| f.write(api_docs.to_yaml)} FileUtils.mkdir_p(File.dirname(calculated_file_path))
File.open(calculated_file_path, 'w') {|f| f.write(ApiDocs.docs.to_yaml)}
end end

end end


# Cleans up params. Removes things like File object handlers # Cleans up params. Removes things like File object handlers
Expand All @@ -74,10 +75,10 @@ def self.api_deep_clean_params(params)
case params case params
when Hash when Hash
params.each_with_object({}) do |(key, value), res| 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 end
when Array when Array
params.collect{|value| ApiDocs::TestHelper.api_deep_clean_params(value)} params.collect{|value| api_deep_clean_params(value)}
else else
case params case params
when Rack::Test::UploadedFile when Rack::Test::UploadedFile
Expand Down

0 comments on commit bafc1f0

Please sign in to comment.