Skip to content

Commit

Permalink
API refactor (ResourceTemplate class moved to toplevel, new ResourceT…
Browse files Browse the repository at this point in the history
…emplate::ResourceTemplates); remove yaml_short format
  • Loading branch information
Michael Burrows authored and Michael Burrows committed May 22, 2009
1 parent a8487b5 commit 6e40339
Show file tree
Hide file tree
Showing 16 changed files with 316 additions and 580 deletions.
7 changes: 7 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
== 0.5.0 2009-05-22

* API refactor, a possible prelude to new resource_template gem:
* Top level class ResourceTemplate (was DescribedRoutes::ResourceTemplate)
* New class ResourceTemplate::ResourceTemplates to which most of ResourceTemplate's class methods have been moved
* Removed the yaml_short format (superseded by the plain text format)

== 0.4.1 2009-05-20

* Configure gem dependency on addressable 2.1.0 (now available on RubyForge)
Expand Down
5 changes: 1 addition & 4 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ lib/described_routes.rb
lib/described_routes/rails_controller.rb
lib/described_routes/rails_routes.rb
lib/described_routes/rake_task_methods.rb
lib/described_routes/resource_template.rb
lib/resource_template.rb
lib/tasks/described_routes.rb
script/console
script/destroy
Expand Down Expand Up @@ -38,13 +38,10 @@ test_rails_app/test/fixtures/build_time/described_routes.json
test_rails_app/test/fixtures/build_time/described_routes.text
test_rails_app/test/fixtures/build_time/described_routes.xml
test_rails_app/test/fixtures/build_time/described_routes.yaml
test_rails_app/test/fixtures/build_time/described_routes.yaml_short
test_rails_app/test/fixtures/build_time/described_routes.yaml_short_no_admin
test_rails_app/test/fixtures/run_time/described_routes.json
test_rails_app/test/fixtures/run_time/described_routes.text
test_rails_app/test/fixtures/run_time/described_routes.xml
test_rails_app/test/fixtures/run_time/described_routes.yaml
test_rails_app/test/fixtures/run_time/described_routes.yaml_short
test_rails_app/test/integration/described_routes_run_time_test.rb
test_rails_app/test/integration/rake_tasks_test.rb
test_rails_app/test/test_helper.rb
3 changes: 0 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Then:
rake described_routes:json # Describe resource structure in JSON format
rake described_routes:xml # Describe resource structure in XML format
rake described_routes:yaml # Describe resource structure in YAML format
rake described_routes:yaml_short # Describe resource structure in YAML format (basic structure only)
rake described_routes:text # Describe resource structure in text (comparable to "rake routes")

The text output looks like this:
Expand Down Expand Up @@ -76,15 +75,13 @@ You (or your client application) can now browse to any of the following top leve
* .../described_routes.json
* .../described_routes.xml
* .../described_routes.yaml
* .../described_routes.yaml?short=true
* .../described_routes.ytxt

and for the named route "users" (say):

* .../described_routes/users.json
* .../described_routes/users.xml
* .../described_routes/users.yaml
* .../described_routes/users.yaml?short=true
* .../described_routes/users.txt

If the application has a route named "root", run-time-generated data will include uri_template attributes based on root_url in addition to the path_template attributes supported at build time.
Expand Down
4 changes: 2 additions & 2 deletions lib/described_routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'described_routes/resource_template'
require 'resource_template'

module DescribedRoutes
# rubygem version
VERSION = "0.4.1"
VERSION = "0.5.0"
end
34 changes: 10 additions & 24 deletions lib/described_routes/rails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,30 @@ module DescribedRoutes
class RailsController < ActionController::Base
def index
base_url = root_url rescue nil
resource_templates = ResourceTemplate.partial_expand(RailsRoutes.get_resource_templates(base_url), request.query_parameters)
resource_templates = RailsRoutes.get_resource_templates(base_url).partial_expand(request.query_parameters)

respond_to do |format|
format.html # index.html.erb
format.json { render :json => ResourceTemplate.to_json(resource_templates) }
format.text { render :text => ResourceTemplate.to_text(resource_templates) }
format.yaml do
yaml = ResourceTemplate::to_yaml(resource_templates)
yaml = yaml.grep(/(name|rel|path_template|uri_template|resources):|^---/).to_s if ['true', '1'].member?(params["short"])
render :text => yaml
end
format.xml do
render :xml => ResourceTemplate::to_xml(
Builder::XmlMarkup.new(:indent => 2),
resource_templates).target!
end
format.json { render :json => resource_templates.to_json }
format.text { render :text => resource_templates.to_text }
format.yaml { render :text => resource_templates.to_yaml }
format.xml { render :xml => resource_templates.to_xml(Builder::XmlMarkup.new(:indent => 2)).target! }
end
end

def show
base_url = root_url rescue nil
resources = RailsRoutes.get_resource_templates(base_url)
resource_template = ResourceTemplate.all_by_name(resources)[params[:id]]
resource_templates = RailsRoutes.get_resource_templates(base_url).partial_expand(request.query_parameters)
resource_template = resource_templates.all_by_name[params[:id]]
# TODO 404 if nil
resource_template = resource_template.partial_expand(request.query_parameters)

respond_to do |format|
format.html # show.html.erb
format.json { render :json => resource_template.to_json }
format.text { render :text => ResourceTemplate.to_text([resource_template]) }
format.xml do
render :xml => resource_template.to_xml(Builder::XmlMarkup.new(:indent => 2)).target!
end
format.yaml do
yaml = resource_template.to_yaml
yaml = yaml.grep(/(name|rel|path_template|uri_template|resources):|^---/).to_s if ['true', '1'].member?(params["short"])
render :text => yaml
end
format.text { render :text => resource_template.to_text }
format.yaml { render :text => resource_template.to_yaml }
format.xml { render :xml => resource_template.to_xml(Builder::XmlMarkup.new(:indent => 2)).target! }
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/described_routes/rails_routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'described_routes/resource_template'
require 'resource_template'

module DescribedRoutes
module RailsRoutes
Expand All @@ -11,12 +11,12 @@ module RailsRoutes
mattr_accessor :parsed_hook

#
# Process Rails routes and return an array of DescribedRoutes::ResourceTemplate objects
# Process Rails routes and return an array of ResourceTemplate objects
#
def self.get_resource_templates(base_url = nil)
parsed = get_parsed_rails_resources(base_url)
parsed = parsed_hook.call(parsed) if parsed_hook
DescribedRoutes::ResourceTemplate.from_parsed(parsed)
ResourceTemplate::ResourceTemplates.new(parsed)
end

#
Expand Down
26 changes: 9 additions & 17 deletions lib/described_routes/rake_task_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,27 @@ module DescribedRoutes
module RakeTaskMethods
# Describe resource structure in JSON format
def self.json
DescribedRoutes::ResourceTemplate.to_json(
DescribedRoutes::RailsRoutes.get_resource_templates(ENV['BASE']))
get_routes.to_json
end

# "Describe resource structure in YAML format
def self.yaml
DescribedRoutes::ResourceTemplate.to_yaml(
DescribedRoutes::RailsRoutes.get_resource_templates(ENV['BASE']))
end

# Describe resource structure in YAML format (basic structure only)
def self.yaml_short
DescribedRoutes::ResourceTemplate.to_yaml(
DescribedRoutes::RailsRoutes.get_resource_templates(ENV['BASE'])).grep(
/(name|rel|path_template|uri_template|resources):|^---/).join
get_routes.to_yaml
end

# Describe resource structure in XML format
def self.xml
DescribedRoutes::ResourceTemplate.to_xml(
Builder::XmlMarkup.new(:indent => 2),
DescribedRoutes::RailsRoutes.get_resource_templates(ENV['BASE'])
).target!
get_routes.to_xml(Builder::XmlMarkup.new(:indent => 2)).target!
end

# Describe resource structure in text format
def self.text
DescribedRoutes::ResourceTemplate.to_text(
DescribedRoutes::RailsRoutes.get_resource_templates(ENV['BASE']))
get_routes.to_text
end

# Gets the application's routes
def self.get_routes
DescribedRoutes::RailsRoutes.get_resource_templates(ENV['BASE'])
end
end
end
Loading

0 comments on commit 6e40339

Please sign in to comment.