Permalink
Browse files

Merge pull request #1 from dbloete/master

Using request.format instead of params[:format]
  • Loading branch information...
2 parents ae21d67 + 1fd7eb2 commit 16c0cfe2648c3c8e788283632b18047bd64232ec @lassebunk committed Jan 4, 2012
View
@@ -0,0 +1 @@
+.rvmrc
View
@@ -0,0 +1,2 @@
+source "http://rubygems.org"
+gemspec
View
@@ -0,0 +1,50 @@
+PATH
+ remote: .
+ specs:
+ api_builder (1.0.2)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionpack (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ i18n (~> 0.6)
+ rack (~> 1.3.2)
+ rack-cache (~> 1.1)
+ rack-mount (~> 0.8.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.0.2)
+ activemodel (3.1.1)
+ activesupport (= 3.1.1)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activesupport (3.1.1)
+ multi_json (~> 1.0)
+ builder (3.0.0)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ multi_json (1.0.3)
+ rack (1.3.5)
+ rack-cache (1.1)
+ rack (>= 0.4)
+ rack-mount (0.8.3)
+ rack (>= 1.0.0)
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ sprockets (2.0.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ actionpack
+ activesupport
+ api_builder!
View
@@ -8,8 +8,8 @@ task :default => :test
desc 'Test the api_builder plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
- t.libs << 'test'
- t.pattern = 'test/**/*_test.rb'
+ t.libs << 'spec'
+ t.pattern = 'spec/**/*_spec.rb'
t.verbose = true
end
View
@@ -7,7 +7,9 @@ Gem::Specification.new do |s|
s.description = "ApiBuilder is a Ruby on Rails template engine that allows for multiple formats being laid out in a single specification, currently XML and JSON."
s.summary = "Multiple API formats from a single specification."
s.homepage = "http://github.com/lassebunk/api_builder"
-
+
+ s.add_development_dependency("actionpack")
+
s.files = Dir['lib/**/*.rb']
s.require_paths = ["lib"]
end
@@ -75,7 +75,8 @@ def element(name, value = nil, &block)
end
def get_output
- case params[:format].to_sym
+ format = request.format.to_sym
+ case format
when :json
if params[:callback]
"#{params[:callback]}(#{@_out.to_json})"
@@ -85,7 +86,7 @@ def get_output
when :xml
@_out.to_xml
else
- raise "unknown format '#{params[:format]}'"
+ raise ArgumentError, "unknown format '#{format}'"
end
end
end
@@ -0,0 +1,12 @@
+require 'spec_helper'
+
+describe ApiBuilder::Renderer do
+
+ it "can render" do
+ user = OpenStruct.new(id: 1, name: "Api Builder")
+ result = render("user", user: user)
+ result.must_match '"id":1'
+ result.must_match '"name":"Api Builder"'
+ end
+
+end
@@ -0,0 +1,4 @@
+element :user do
+ id user.id
+ name user.name
+end
@@ -0,0 +1,8 @@
+array :users do
+ users.each do |user|
+ element :user do
+ id user.id
+ name user.name
+ end
+ end
+end
View
@@ -0,0 +1,61 @@
+require 'minitest/spec'
+require 'minitest/autorun'
+
+require 'json'
+require 'logger'
+require 'action_view'
+require 'api_builder'
+
+class MiniTest::Spec
+
+ Handler = ActionView::Template::Handlers::ApiBuilder
+
+ class LookupContext
+ def disable_cache
+ yield
+ end
+
+ def find_template(*args)
+ end
+ end
+
+ class Context
+ def initialize
+ @output_buffer = "original"
+ @virtual_path = nil
+ end
+
+ def params
+ {}
+ end
+
+ def request
+ OpenStruct.new(format: :json)
+ end
+
+ def partial
+ ActionView::Template.new(
+ "<%= @virtual_path %>",
+ "partial",
+ Handler,
+ :virtual_path => "partial")
+ end
+
+ def lookup_context
+ @lookup_context ||= LookupContext.new
+ end
+
+ def logger
+ Logger.new(STDERR)
+ end
+ end
+
+ def render(fixture, locals = {})
+ path = File.expand_path("../fixtures/#{fixture}.apibuilder", __FILE__)
+ body = File.read(path)
+ tmpl = ActionView::Template.new(body, "#{fixture} template", Handler, { virtual_path: fixture })
+ tmpl.locals = locals.keys
+ tmpl.render(Context.new, locals)
+ end
+
+end

0 comments on commit 16c0cfe

Please sign in to comment.