Module: ApiDocs::TestHelper
- Defined in:
- lib/api_docs/test_helper.rb
Instance Method Summary collapse
-
#api_call(method, path, params = { }) {|doc| ... } ⇒ Object
Method that allows test creation and will document results in a YAML file Example usage: api_call(:get, ‘/users/:id’, :id => 12345) do |doc| doc.description = ‘Something for the docs’ …
-
#api_deep_clean_params(params, as_response = false) ⇒ Object
Cleans up params.
Instance Method Details
#api_call(method, path, params = { }) {|doc| ... } ⇒ Object
Method that allows test creation and will document results in a YAML file Example usage:
api_call(:get, '/users/:id', :id => 12345) do |doc|
doc.description = 'Something for the docs'
... regular test code
end
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/api_docs/test_helper.rb', line 9 def api_call(method, path, params = { }, &block) parsed_params = params.dup parsed_path = path.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' doc = OpenStruct.new send(method, parsed_path, parsed_params) yield doc # 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.("#{c.gsub('/', ':')}.yml", ApiDocs.config.docs_path) params = api_deep_clean_params(params) body = JSON.parse(response.body) # Marking response as an unique key = Digest::MD5.hexdigest(" #{method}#{path}#{params}#{response.status}#{api_deep_clean_params(body, :as_response)} ") data = if File.exists?(file_path) YAML.load_file(file_path) rescue Hash.new else Hash.new end data[a] ||= { } data[a][key] = { 'description' => doc.description, 'method' => request.method, 'path' => path, 'params' => api_deep_clean_params(params), 'status' => response.status, 'body' => body } FileUtils.mkdir_p(File.dirname(file_path)) File.open(file_path, 'w'){|f| f.write(data.to_yaml)} end |
#api_deep_clean_params(params, as_response = false) ⇒ Object
Cleans up params. Removes things like File object handlers Sets up ignored values so we don’t generate new keys for same data
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/api_docs/test_helper.rb', line 58 def api_deep_clean_params(params, as_response = false) case params when Hash params.each_with_object({}) do |(key, value), res| if as_response && ApiDocs.config.ignored_attributes.include?(key.to_s) res[key.to_s] = 'IGNORED' else res[key.to_s] = api_deep_clean_params(value, as_response) end end when Array params.collect{|value| api_deep_clean_params(value, as_response)} else case params when Rack::Test::UploadedFile 'BINARY' else params.to_s end end end |