Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Fixes #125] Dump examples as json

  • Loading branch information...
commit c672f79f656efbc83af0ddcf87b2772fe89a66fa 1 parent 50fc8a2
@johanneswuerbach authored
View
13 lib/apipie/application.rb
@@ -207,18 +207,7 @@ def init_env
def recorded_examples
return @recorded_examples if @recorded_examples
- tape_file = File.join(Rails.root,"doc","apipie_examples.yml")
- if File.exists?(tape_file)
- #if SafeYAML gem is enabled, it will load examples as an array of Hash, instead of hash
- if YAML.respond_to?(:safe_load_file) && defined?(SafeYAML)
- @recorded_examples = SafeYAML.load_file(tape_file, :safe=>false)
- else
- @recorded_examples = YAML.load_file(tape_file)
- end
- else
- @recorded_examples = {}
- end
- @recorded_examples
+ @recorded_examples = Apipie::Extractor::Writer.load_recorded_examples
end
def reload_examples
View
43 lib/apipie/extractor/writer.rb
@@ -6,15 +6,11 @@ class Writer
def initialize(collector)
@collector = collector
- @examples_file = File.join(Rails.root, "doc", "apipie_examples.yml")
end
def write_examples
merged_examples = merge_old_new_examples
- FileUtils.mkdir_p(File.dirname(@examples_file))
- File.open(@examples_file, "w") do |f|
- f << YAML.dump(OrderedHash[*merged_examples.sort_by(&:first).flatten(1)])
- end
+ self.class.write_recorded_examples(merged_examples)
end
def write_docs
@@ -40,8 +36,38 @@ def self.update_action_description(controller, action)
logger.warn("REST_API: Couldn't find action #{action} in #{controller}")
end
+ def self.write_recorded_examples(examples)
+ examples_file = self.examples_file
+ FileUtils.mkdir_p(File.dirname(examples_file))
+ File.open(examples_file, "w") do |f|
+ f << MultiJson.dump(OrderedHash[*examples.sort_by(&:first).flatten(1)], :pretty => true)
+ end
+ end
+
+ def self.load_recorded_examples
+ examples_file = self.examples_file
+ if File.exists?(examples_file)
+ return load_json_examples
+ end
+ return {}
+ end
+
+ def self.examples_file
+ File.join(Rails.root,"doc","apipie_examples.json")
+ end
+
protected
+ def self.load_json_examples
+ examples = MultiJson.load(IO.read(examples_file))
+ return {} if examples.nil?
+ examples.each do |method, records|
+ records.each do |record|
+ record["verb"] = record["verb"].to_sym if record["verb"]
+ end
+ end
+ end
+
def desc_to_s(description)
"#{description[:controller].name}##{description[:action]}"
end
@@ -61,9 +87,13 @@ def ordered_call(call)
return ordered_call
end
+ def load_recorded_examples
+ self.class.load_recorded_examples
+ end
+
def merge_old_new_examples
new_examples = self.load_new_examples
- old_examples = self.load_old_examples
+ old_examples = self.load_recorded_examples
merged_examples = []
(new_examples.keys + old_examples.keys).uniq.each do |key|
if new_examples.has_key?(key)
@@ -364,6 +394,5 @@ def to_yaml(opts = {})
end
end
end
-
end
end
View
16 lib/tasks/apipie.rake
@@ -189,4 +189,20 @@ MESSAGE
end
end
+ desc "Convert your examples from the old yaml into the new json format"
+ task :convert_examples => :environment do
+ yaml_examples_file = File.join(Rails.root, "doc", "apipie_examples.yml")
+ if File.exists?(yaml_examples_file)
+ #if SafeYAML gem is enabled, it will load examples as an array of Hash, instead of hash
+ if defined? SafeYAML
+ examples = YAML.load_file(yaml_examples_file, :safe=>false)
+ else
+ examples = YAML.load_file(yaml_examples_file)
+ end
+ else
+ examples = {}
+ end
+ Apipie::Extractor::Writer.write_recorded_examples(examples)
+ end
+
end
View
1  spec/dummy/doc/apipie_examples.json
@@ -0,0 +1 @@
+{"users#show":[{"verb":"GET","path":"/users/15","query":null,"request_data":null,"response_data":null,"code":"404","show_in_doc":2,"recorded":true},{"verb":"GET","path":"/users/14","query":"verbose=true","request_data":null,"response_data":"{\n \"name\": \"Test User\"\n}","code":"200","show_in_doc":1,"recorded":true},{"verb":"GET","path":"/users/15","query":"verbose=true","request_data":null,"response_data":"{\n \"name\": \"Test User\"\n}","code":"200","show_in_doc":0,"recorded":true}]}
View
28 spec/dummy/doc/apipie_examples.yml
@@ -1,28 +0,0 @@
----
-users#show:
-- verb: :GET
- path: /users/15
- query:
- request_data:
- response_data:
- code: 404
- show_in_doc: 2
- recorded: true
-- verb: :GET
- path: /users/14
- query: "verbose=true"
- request_data:
- response_data:
- name: Test User
- code: 200
- show_in_doc: 1
- recorded: true
-- verb: :GET
- path: /users/15
- query: "verbose=true"
- request_data:
- response_data:
- name: Test User
- code: 200
- show_in_doc: 0
- recorded: true
View
76 spec/lib/extractor/writer_spec.rb
@@ -0,0 +1,76 @@
+require "spec_helper"
+
+describe Apipie::Extractor::Writer do
+
+ let(:collector) { double "collector" }
+ let(:writer_class) { Apipie::Extractor::Writer }
+ let(:writer) { writer_class.new(collector) }
+ let(:test_examples_file) { File.join(Rails.root, "doc", "apipie_examples_test.json") }
+ let(:records) { {
+ "concern_resources#show" =>
+ [{
+ :controller=>ConcernsController,
+ :action=>"show",
+ :verb=>:GET,
+ :path=>"/api/concerns/5",
+ :params=>{},
+ :query=>"session=secret_hash",
+ :request_data=>nil,
+ :response_data=>"OK {\"session\"=>\"secret_hash\", \"id\"=>\"5\", \"controller\"=>\"concerns\", \"action\"=>\"show\"}",
+ :code=>"200"
+ }, {
+ :controller=>ConcernsController,
+ :action=>"show",
+ :verb=>:GET,
+ :path=>"/api/concerns/5",
+ :params=>{},
+ :query=>"",
+ :request_data=>nil,
+ :response_data=>"OK {\"id\"=>\"5\", \"controller\"=>\"concerns\", \"action\"=>\"show\"}",
+ :code=>"200"
+ }]
+ }
+ }
+ let(:loaded_records) { {
+ "concern_resources#show" =>
+ [{
+ "verb"=>:GET,
+ "path"=>"/api/concerns/5",
+ "versions"=>["development"],
+ "query"=>"session=secret_hash",
+ "request_data"=>nil,
+ "response_data"=>"OK {\"session\"=>\"secret_hash\", \"id\"=>\"5\", \"controller\"=>\"concerns\", \"action\"=>\"show\"}",
+ "code"=>"200",
+ "show_in_doc"=>1,
+ "recorded"=>true
+ }, {
+ "verb"=>:GET,
+ "path"=>"/api/concerns/5",
+ "versions"=>["development"],
+ "query"=>"",
+ "request_data"=>nil,
+ "response_data"=>"OK {\"id\"=>\"5\", \"controller\"=>\"concerns\", \"action\"=>\"show\"}",
+ "code"=>"200",
+ "show_in_doc"=>0,
+ "recorded"=>true
+ }]
+ }
+ }
+
+
+ describe "storing of examples" do
+ before do
+ writer_class.stub(:examples_file) { test_examples_file }
+ collector.should_receive(:records).and_return(records)
+ end
+
+ it "should read and write examples" do
+ writer.write_examples
+ writer.send(:load_recorded_examples).should eq(loaded_records)
+ end
+
+ after do
+ File.unlink(test_examples_file) if File.exists?(test_examples_file)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.