Skip to content

Commit

Permalink
Merge pull request #4 from NDrive/add-cookies-at-documentation
Browse files Browse the repository at this point in the history
Adds cookies at documentation
  • Loading branch information
Vladimir Krylov committed Dec 2, 2014
2 parents de334b1 + 689f32c commit 86df708
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/api_docs/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ApiDocs::TestHelper

module InstanceMethods
# Method that allows test creation and will document results in a YAML file
# Example usage:
Expand All @@ -11,50 +11,51 @@ 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
yield meta if block_given?

# Not writing anything to the files unless there was a demand
if ApiDocs.config.generate_on_demand
return unless ENV['API_DOCS']
end

# Assertions inside test block didn't fail. Preparing file
# content to be written
c = request.filtered_parameters['controller']
a = request.filtered_parameters['action']

file_path = File.expand_path("#{c.gsub('/', ':')}.yml", ApiDocs.config.docs_path)
key_params = ApiDocs::TestHelper.api_deep_clean_params(params, true)
params = ApiDocs::TestHelper.api_deep_clean_params(params)

# Marking response as an unique
key = 'ID-' + Digest::MD5.hexdigest("
#{method}#{path}#{meta}#{key_params}#{response.status}}
")

data = if File.exists?(file_path)
YAML.load_file(file_path) rescue Hash.new
else
Hash.new
end

data[a] ||= { }
data[a][key] = {
'meta' => meta,
'method' => request.method,
'path' => path,
'headers' => headers,
'cookies' => cookies.to_hash,
'params' => ApiDocs::TestHelper.api_deep_clean_params(params),
'status' => response.status,
'body' => response.body
Expand All @@ -63,7 +64,7 @@ def api_call(method, path, params = {}, headers = {})
File.open(file_path, 'w'){|f| f.write(data.to_yaml)}
end
end

# Cleans up params. Removes things like File object handlers
# Sets up ignored values so we don't generate new keys for same data
def self.api_deep_clean_params(params, for_key = false)
Expand All @@ -80,7 +81,7 @@ def self.api_deep_clean_params(params, for_key = false)
when Array
params.collect{|value| ApiDocs::TestHelper.api_deep_clean_params(value)}
else
case params
case params
when Rack::Test::UploadedFile
'BINARY'
else
Expand Down
5 changes: 5 additions & 0 deletions lib/api_docs/views/_action.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
%span.label.label-info= action['method']
%code= action['path']

- if action['cookies'].present? && action['cookies'].is_a?(Hash)
%tr
%th Cookies
%td= haml :_request_params, locals: { request_params: action['cookies'] }

- if action['headers'].present? && action['headers'].is_a?(Hash)
%tr
%th Headers
Expand Down

0 comments on commit 86df708

Please sign in to comment.