Browse files

API refactor (ResourceTemplate class moved to toplevel, new ResourceT…

…emplate::ResourceTemplates); remove yaml_short format
  • Loading branch information...
1 parent a8487b5 commit 6e403390acaf1b4e8be62b8ac05bc08711a95cfe Michael Burrows committed May 22, 2009
View
7 History.txt
@@ -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)
View
5 Manifest.txt
@@ -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
@@ -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
View
3 README.rdoc
@@ -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:
@@ -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.
View
4 lib/described_routes.rb
@@ -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
View
34 lib/described_routes/rails_controller.rb
@@ -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
View
6 lib/described_routes/rails_routes.rb
@@ -1,4 +1,4 @@
-require 'described_routes/resource_template'
+require 'resource_template'
module DescribedRoutes
module RailsRoutes
@@ -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
#
View
26 lib/described_routes/rake_task_methods.rb
@@ -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
View
248 lib/described_routes/resource_template.rb
@@ -1,248 +0,0 @@
-require "json"
-require "addressable/template"
-
-module DescribedRoutes
- class ResourceTemplate
- # The template's name. Optional. Making these unique across the application is helpful for clients
- # that may wish to pick out nested templates by name.
- attr_reader :name
-
- # Optional attribute that describes a resource's relationship to its parent. For example:
- # * a nested route to a resource's edit page would have rel of "edit"
- # * a nested collection of articles under a "user" resource would have have a rel of "articles"
- # Collection members generally don't need a rel as they are identified by their params
- attr_reader :rel
-
- # A template for generating URIs.
- attr_reader :uri_template
-
- # A template for generating paths relative to the application's base.
- attr_reader :path_template
-
- # The parameters required by the path template
- attr_reader :params
-
- # Optional paramaters that may be used by the path template
- attr_reader :optional_params
-
- # "options" in the sense of the HTTP option request - i.e. a list of HTTP methods. Optional.
- attr_reader :options
-
- # An optional list of nested resource templates
- attr_reader :resource_templates
-
- # Initialize a ResourceTemplate. See the attribute descriptions above for explanations of the parameters.
- def initialize(name, rel, uri_template, path_template, params, optional_params, options, resource_templates)
- @name, @rel, @uri_template, @path_template = name, rel, uri_template, path_template
- @params = params || []
- @optional_params = optional_params || []
- @options = options || []
- @resource_templates = resource_templates || []
- end
-
- # Create a ResourceTemplate from its Hash representation
- def self.from_hash(hash)
- attributes = %w(name rel uri_template path_template params optional_params options).map{|k| hash[k]}
- if hash["resource_templates"]
- attributes << hash["resource_templates"].map{|h| from_hash(h)} if hash["resource_templates"]
- else
- attributes << nil
- end
- self.new(*attributes)
- end
-
- # Convert to a hash (equivalent to its JSON or YAML representation)
- def to_hash
- hash = {}
- hash["name"] = name if name && !name.empty?
- hash["rel"] = rel if rel && !rel.empty?
- hash["uri_template"] = uri_template if uri_template && !uri_template.empty?
- hash["path_template"] = path_template if path_template && !path_template.empty?
-
- hash["params"] = params if params && !params.empty?
- hash["optional_params"] = optional_params if optional_params && !optional_params.empty?
-
- hash["options"] = options if options && !options.empty?
-
- hashes = DescribedRoutes::ResourceTemplate.to_parsed(resource_templates)
- hash["resource_templates"] = hashes if hashes && !hashes.empty?
-
- hash
- end
-
- # Convert to JSON
- def to_json
- to_hash.to_json
- end
-
- # Convert to YAML
- def to_yaml
- to_hash.to_yaml
- end
-
- #
- # Produces the XML format, given an XML builder object and an array of ResourceTemplate objects
- #
- def to_xml(xm)
- xm.ResourceTemplate do |xm|
- value_tag(xm, "rel")
- value_tag(xm, "name")
- value_tag(xm, "path_template")
- value_tag(xm, "uri_template")
-
- list_tag(xm, params, "Params", "param")
- list_tag(xm, optional_params, "OptionalParams", "param")
-
- # could use a list of elements here, but let's follow HTTP's lead and reduce the verbosity
- xm.options(options.join(", ")) unless options.empty?
-
- self.class.to_xml(xm, resource_templates) unless resource_templates.empty?
- end
- xm
- end
-
- def value_tag(xm, tag) #:nodoc:
- value = self.send(tag.to_sym)
- xm.tag!(tag, value) unless value.blank?
- end
-
- def list_tag(xm, collection, collection_tag, member_tag) #:nodoc:
- unless collection.nil? or collection.empty?
- xm.tag!(collection_tag) do |xm|
- collection.each do |value|
- xm.tag!(member_tag, value)
- end
- end
- end
- end
-
- # Convert an array of ResourceTemplate objects to array of hashes equivalent to their JSON or YAML representations
- def self.to_parsed(resource_templates)
- resource_templates.map{|resource_template| resource_template.to_hash}
- end
-
- # Convert an array of ResourceTemplate objects to JSON
- def self.to_json(resource_templates)
- self.to_parsed(resource_templates).to_json
- end
-
- # Convert an array of ResourceTemplate objects to YAML
- def self.to_yaml(resource_templates)
- self.to_parsed(resource_templates).to_yaml
- end
-
- # Create an array of ResourceTemplate objects from a JSON string
- def self.parse_json(json)
- self.from_parsed(JSON.parse(json))
- end
-
- # Create an array of ResourceTemplate objects from a JSON string
- def self.parse_yaml(yaml)
- self.from_parsed(YAML::load(yaml))
- end
-
- # Create an array of ResourceTemplate objects from an array of hashes
- def self.from_parsed(parsed)
- raise ArgumentError.new("not an array") unless parsed.kind_of?(Array)
-
- parsed.map do |hash|
- ResourceTemplate.from_hash(hash)
- end
- end
-
- #
- # Produces the XML format, given an XML builder object and an array of ResourceTemplate objects
- #
- def self.to_xml(xm, resource_templates)
- xm.ResourceTemplates do |xm|
- resource_templates.each do |resource_template|
- resource_template.to_xml(xm)
- end
- end
- xm
- end
-
- # Get a hash of all named ResourceTemplate objects contained in the supplied collection, keyed by name
- def self.all_by_name(resource_templates, h = {})
- resource_templates.inject(h) do |hash, resource_template|
- hash[resource_template.name] = resource_template if resource_template.name
- all_by_name(resource_template.resource_templates, hash)
- hash
- end
- h
- end
-
- # for #to_text
- def self.to_table(resource_templates, parent_template = nil, t = [], indent = '')
- resource_templates.inject(t) do |table, resource_template|
- if parent_template
- link = (resource_template.rel || '')
- new_params = resource_template.params - parent_template.params
- else
- link = resource_template.name
- new_params = resource_template.params
- end
- link += new_params.map{|p| "{#{p}}"}.join(', ')
- table << [
- indent + link,
- resource_template.name || '',
- resource_template.options.join(', '),
- resource_template.uri_template || resource_template.path_template
- ]
- to_table(resource_template.resource_templates, resource_template, t, indent + ' ')
- end
- t
- end
-
- # text report
- def self.to_text(resource_templates)
- table = self.to_table(resource_templates)
-
- 0.upto(2) do |i|
- width = table.map{|row| row[i].length}.max
- table.each do |row|
- row[i] = row[i].ljust(width)
- end
- end
-
- table.map{|row| row.join(' ')}.join("\n") + "\n"
- end
-
- # returns params and any optional_params in order, removing the parent's params
- def positional_params(parent)
- all_params = params + optional_params
- if parent
- all_params - parent.params
- else
- all_params
- end
- end
-
- # Partially expand the path_template or uri_template of the given resource templates with the given params,
- # returning new resource templates
- def self.partial_expand(resource_templates, actual_params)
- resource_templates.map do |resource_template|
- resource_template.partial_expand(actual_params)
- end
- end
-
- # Return a new resource template with the path_template or uri_template partially expanded with the given params
- def partial_expand(actual_params)
- self.class.new(
- name,
- rel,
- partial_expand_uri_template(uri_template, actual_params),
- partial_expand_uri_template(path_template, actual_params),
- params - actual_params.keys,
- optional_params - actual_params.keys,
- options,
- self.class.partial_expand(resource_templates, actual_params))
- end
-
- # Partially expand a URI template
- def partial_expand_uri_template(template, params)#:nodoc:
- template && Addressable::Template.new(template).partial_expand(params).pattern
- end
- end
-end
-
View
258 lib/resource_template.rb
@@ -0,0 +1,258 @@
+require "json"
+require "addressable/template"
+
+class ResourceTemplate
+ # The template's name. Optional. Making these unique across the application is helpful for clients
+ # that may wish to pick out nested templates by name.
+ attr_reader :name
+
+ # Optional attribute that describes a resource's relationship to its parent. For example:
+ # * a nested route to a resource's edit page would have rel of "edit"
+ # * a nested collection of articles under a "user" resource would have have a rel of "articles"
+ # Collection members generally don't need a rel as they are identified by their params
+ attr_reader :rel
+
+ # A template for generating URIs.
+ attr_reader :uri_template
+
+ # A template for generating paths relative to the application's base.
+ attr_reader :path_template
+
+ # The parameters required by the path template
+ attr_reader :params
+
+ # Optional paramaters that may be used by the path template
+ attr_reader :optional_params
+
+ # "options" in the sense of the HTTP option request - i.e. a list of HTTP methods. Optional.
+ attr_reader :options
+
+ # Nested resource templates, a Resources object
+ attr_reader :resource_templates
+
+ # Initialize a ResourceTemplate. See the attribute descriptions above for explanations of the parameters.
+ def initialize(name, rel, uri_template, path_template, params, optional_params, options, resource_templates)
+ @name, @rel, @uri_template, @path_template = name, rel, uri_template, path_template
+ @params = params || []
+ @optional_params = optional_params || []
+ @options = options || []
+ @resource_templates = resource_templates || Resources.new
+ end
+
+ # Create a ResourceTemplate from its Hash representation
+ def self.from_hash(hash)
+ attributes = %w(name rel uri_template path_template params optional_params options).map{|k| hash[k]}
+ attributes << ResourceTemplates.new(hash["resource_templates"])
+ self.new(*attributes)
+ end
+
+ # Convert to a hash (equivalent to its JSON or YAML representation)
+ def to_hash
+ hash = {}
+ hash["name"] = name if name && !name.empty?
+ hash["rel"] = rel if rel && !rel.empty?
+ hash["uri_template"] = uri_template if uri_template && !uri_template.empty?
+ hash["path_template"] = path_template if path_template && !path_template.empty?
+
+ hash["params"] = params if params && !params.empty?
+ hash["optional_params"] = optional_params if optional_params && !optional_params.empty?
+
+ hash["options"] = options if options && !options.empty?
+
+ hash["resource_templates"] = resource_templates.to_parsed if !resource_templates.empty?
+
+ hash
+ end
+
+ # Convert to JSON
+ def to_json
+ to_hash.to_json
+ end
+
+ # Convert to YAML
+ def to_yaml
+ to_hash.to_yaml
+ end
+
+ # Text report
+ def to_text
+ ResourceTemplates.new([self]).to_text
+ end
+
+ #
+ # Produces the XML format, given an XML builder object and an array of ResourceTemplate objects
+ #
+ def to_xml(xm)
+ xm.ResourceTemplate do |xm|
+ value_tag(xm, "rel")
+ value_tag(xm, "name")
+ value_tag(xm, "path_template")
+ value_tag(xm, "uri_template")
+
+ list_tag(xm, params, "Params", "param")
+ list_tag(xm, optional_params, "OptionalParams", "param")
+
+ # could use a list of elements here, but let's follow HTTP's lead and reduce the verbosity
+ xm.options(options.join(", ")) unless options.empty?
+
+ resource_templates.to_xml(xm) unless resource_templates.empty?
+ end
+ xm
+ end
+
+ def value_tag(xm, tag) #:nodoc:
+ value = self.send(tag.to_sym)
+ xm.tag!(tag, value) unless value.blank?
+ end
+
+ def list_tag(xm, collection, collection_tag, member_tag) #:nodoc:
+ unless collection.nil? or collection.empty?
+ xm.tag!(collection_tag) do |xm|
+ collection.each do |value|
+ xm.tag!(member_tag, value)
+ end
+ end
+ end
+ end
+
+ # returns params and any optional_params in order, removing the parent's params
+ def positional_params(parent)
+ all_params = params + optional_params
+ if parent
+ all_params - parent.params
+ else
+ all_params
+ end
+ end
+
+ # Return a new resource template with the path_template or uri_template partially expanded with the given params
+ def partial_expand(actual_params)
+ self.class.new(
+ name,
+ rel,
+ partial_expand_uri_template(uri_template, actual_params),
+ partial_expand_uri_template(path_template, actual_params),
+ params - actual_params.keys,
+ optional_params - actual_params.keys,
+ options,
+ resource_templates.partial_expand(actual_params))
+ end
+
+ # Partially expand a URI template
+ def partial_expand_uri_template(template, params)#:nodoc:
+ template && Addressable::Template.new(template).partial_expand(params).pattern
+ end
+
+ class ResourceTemplates < Array
+ # Initialize Resources (i.e. a new collection of ResourceTemplate objects) from given collection of ResourceTemplates or hashes
+ def initialize(collection=[])
+ if collection
+ raise ArgumentError.new("#{collection.inspect} is not a collection") unless collection.kind_of?(Enumerable)
+
+ collection.each do |r|
+ if r.kind_of?(ResourceTemplate)
+ push(r)
+ elsif r.kind_of?(Hash)
+ push(ResourceTemplate.from_hash(r))
+ else
+ raise ArgumentError.new("#{r.inspect} is neither a ResourceTemplate nor a Hash")
+ end
+ end
+ end
+ end
+
+ # Create Resources from a YAML string
+ def self.parse_yaml(yaml)
+ new(YAML::load(yaml))
+ end
+
+ # Create Resources from a JSON string
+ def self.parse_json(json)
+ new(JSON.parse(json))
+ end
+
+ # Create Resources from an XML string
+ def self.parse_xml
+ raise NotImplementedError.new
+ end
+
+ # Convert member ResourceTemplate objects to array of hashes equivalent to their JSON or YAML representations
+ def to_parsed
+ map {|resource_template| resource_template.to_hash}
+ end
+
+ # Convert an array of ResourceTemplate objects to JSON
+ def to_json
+ to_parsed.to_json
+ end
+
+ # Convert an array of ResourceTemplate objects to YAML
+ def to_yaml
+ to_parsed.to_yaml
+ end
+
+ #
+ # Produces the XML format, given an XML builder object and an array of ResourceTemplate objects
+ #
+ def to_xml(xm)
+ xm.ResourceTemplates do |xm|
+ each do |resource_template|
+ resource_template.to_xml(xm)
+ end
+ end
+ xm
+ end
+
+ # Get a hash of all named ResourceTemplate objects contained in the supplied collection, keyed by name
+ def all_by_name(h = {})
+ inject(h) do |hash, resource_template|
+ hash[resource_template.name] = resource_template if resource_template.name
+ resource_template.resource_templates.all_by_name(hash)
+ hash
+ end
+ h
+ end
+
+ # for #to_text
+ def to_table(parent_template = nil, t = [], indent = '')
+ inject(t) do |table, resource_template|
+ if parent_template
+ link = (resource_template.rel || '')
+ new_params = resource_template.params - parent_template.params
+ else
+ link = resource_template.name
+ new_params = resource_template.params
+ end
+ link += new_params.map{|p| "{#{p}}"}.join(', ')
+ table << [
+ indent + link,
+ resource_template.name || '',
+ resource_template.options.join(', '),
+ resource_template.uri_template || resource_template.path_template
+ ]
+ resource_template.resource_templates.to_table(resource_template, t, indent + ' ')
+ end
+ t
+ end
+
+ # text report
+ def to_text
+ table = self.to_table
+
+ 0.upto(2) do |i|
+ width = table.map{|row| row[i].length}.max
+ table.each do |row|
+ row[i] = row[i].ljust(width)
+ end
+ end
+
+ table.map{|row| row.join(' ')}.join("\n") + "\n"
+ end
+
+ # Partially expand the path_template or uri_template of the given resource templates with the given params,
+ # returning new resource templates
+ def partial_expand(actual_params)
+ self.class.new(map{|resource_template| resource_template.partial_expand(actual_params)})
+ end
+ end
+end
View
7 lib/tasks/described_routes.rb
@@ -12,11 +12,6 @@
puts DescribedRoutes::RakeTaskMethods.yaml
end
- desc 'Describe resource structure in YAML format (basic structure only) (optional: add "BASE=http://...")'
- task :yaml_short => :environment do
- puts DescribedRoutes::RakeTaskMethods.yaml_short
- end
-
desc 'Describe resource structure in XML format (optional: add "BASE=http://...")'
task :xml => :environment do
puts DescribedRoutes::RakeTaskMethods.xml
@@ -29,7 +24,7 @@
# unsupported
task :ruby => :environment do
- puts DescribedRoutes::ResourceTemplate.to_parsed(
+ puts ResourceTemplate.to_parsed(
DescribedRoutes::RailsRoutes.get_resource_templates).inspect
end
View
50 test/test_resource_template.rb
@@ -1,33 +1,33 @@
require 'test/unit'
-require 'described_routes/resource_template'
+require 'resource_template'
class TestResourceTemplate < Test::Unit::TestCase
attr_reader :json, :resource_templates, :resource_templates_by_name, :user_articles, :user_article, :edit_user_article
def setup
- @json ||= File.read(File.dirname(__FILE__) + "/fixtures/described_routes_test.json")
- @resource_templates = DescribedRoutes::ResourceTemplate.parse_json(json)
- @resource_templates_by_name = DescribedRoutes::ResourceTemplate.all_by_name(@resource_templates)
- @user_articles = @resource_templates_by_name["user_articles"]
- @user_article = @resource_templates_by_name["user_article"]
- @edit_user_article = @resource_templates_by_name["edit_user_article"]
+ @json ||= File.read(File.dirname(__FILE__) + '/fixtures/described_routes_test.json')
+ @resource_templates = ResourceTemplate::ResourceTemplates.parse_json(json)
+ @resource_templates_by_name = @resource_templates.all_by_name
+ @user_articles = @resource_templates_by_name['user_articles']
+ @user_article = @resource_templates_by_name['user_article']
+ @edit_user_article = @resource_templates_by_name['edit_user_article']
end
def test_fixture
- assert_kind_of(DescribedRoutes::ResourceTemplate, user_articles)
- assert_kind_of(DescribedRoutes::ResourceTemplate, user_article)
- assert_kind_of(DescribedRoutes::ResourceTemplate, edit_user_article)
+ assert_kind_of(ResourceTemplate, user_articles)
+ assert_kind_of(ResourceTemplate, user_article)
+ assert_kind_of(ResourceTemplate, edit_user_article)
- assert_equal("user_article", user_article.name)
- assert_equal(["user_id", "article_id"], user_article.params)
- assert_equal(["format"], user_article.optional_params)
+ assert_equal('user_article', user_article.name)
+ assert_equal(['user_id', 'article_id'], user_article.params)
+ assert_equal(['format'], user_article.optional_params)
- assert_equal("articles", user_articles.rel)
+ assert_equal('articles', user_articles.rel)
assert_nil(user_article.rel)
- assert_equal("edit", edit_user_article.rel)
+ assert_equal('edit', edit_user_article.rel)
- assert_equal("/users/{user_id}/articles{-prefix|.|format}", user_articles.path_template)
- assert_equal("http://localhost:3000/users/{user_id}/articles{-prefix|.|format}", user_articles.uri_template)
+ assert_equal('/users/{user_id}/articles{-prefix|.|format}', user_articles.path_template)
+ assert_equal('http://localhost:3000/users/{user_id}/articles{-prefix|.|format}', user_articles.uri_template)
assert(user_articles.resource_templates.member?(user_article))
assert(user_article.resource_templates.member?(edit_user_article))
@@ -36,26 +36,26 @@ def test_fixture
def test_json
assert_equal(
JSON.parse(json),
- JSON.parse(DescribedRoutes::ResourceTemplate.to_json(resource_templates)))
+ JSON.parse(resource_templates.to_json))
end
def test_yaml
assert_equal(
JSON.parse(json),
- YAML.load(DescribedRoutes::ResourceTemplate.to_yaml(resource_templates)))
+ YAML.load(resource_templates.to_yaml))
end
def test_positional_params
- assert_equal(["user_id", "article_id", "format"], user_article.positional_params(nil))
- assert_equal(["article_id", "format"], user_article.positional_params(user_articles))
+ assert_equal(['user_id', 'article_id', 'format'], user_article.positional_params(nil))
+ assert_equal(['article_id', 'format'], user_article.positional_params(user_articles))
end
def test_partial_expand
- expanded = DescribedRoutes::ResourceTemplate.all_by_name([user_articles.partial_expand("user_id" => "dojo", "format" => "json")])
- expanded_edit_user_article = expanded["edit_user_article"]
+ expanded_user_articles = user_articles.partial_expand('user_id' => 'dojo', 'format' => 'json')
+ expanded_edit_user_article = expanded_user_articles.resource_templates.all_by_name['edit_user_article']
- assert_equal(["article_id"], expanded_edit_user_article.params)
+ assert_equal(['article_id'], expanded_edit_user_article.params)
assert(expanded_edit_user_article.optional_params.empty?)
- assert_equal("/users/dojo/articles/{article_id}/edit.json", expanded_edit_user_article.path_template)
+ assert_equal('/users/dojo/articles/{article_id}/edit.json', expanded_edit_user_article.path_template)
end
end
View
72 test_rails_app/test/fixtures/build_time/described_routes.yaml_short
@@ -1,72 +0,0 @@
----
-- name: root
- path_template: /
-- name: admin_products
- - name: new_admin_product
- path_template: /admin/products/new{-prefix|.|format}
- rel: new_admin_product
- - name: admin_product
- - name: edit_admin_product
- path_template: /admin/products/{product_id}/edit{-prefix|.|format}
- rel: edit
- path_template: /admin/products/{product_id}{-prefix|.|format}
- path_template: /admin/products{-prefix|.|format}
-- name: described_routes
- - name: new_described_route
- path_template: /described_routes/new{-prefix|.|format}
- rel: new_described_route
- - name: described_route
- - name: edit_described_route
- path_template: /described_routes/{route_name}/edit{-prefix|.|format}
- rel: edit
- path_template: /described_routes/{route_name}{-prefix|.|format}
- path_template: /described_routes{-prefix|.|format}
-- name: pages
- - name: new_page
- path_template: /pages/new{-prefix|.|format}
- rel: new_page
- - name: page
- - name: edit_page
- path_template: /pages/{page_id}/edit{-prefix|.|format}
- rel: edit
- - name: summary_page
- path_template: /pages/{page_id}/summary{-prefix|.|format}
- rel: summary
- - name: toggle_visibility_page
- path_template: /pages/{page_id}/toggle_visibility{-prefix|.|format}
- rel: toggle_visibility
- path_template: /pages/{page_id}{-prefix|.|format}
- path_template: /pages{-prefix|.|format}
-- name: users
- - name: new_user
- path_template: /users/new{-prefix|.|format}
- rel: new_user
- - name: user
- - name: edit_user
- path_template: /users/{user_id}/edit{-prefix|.|format}
- rel: edit
- - name: user_articles
- - name: new_user_article
- path_template: /users/{user_id}/articles/new{-prefix|.|format}
- rel: new_user_article
- - name: recent_user_articles
- path_template: /users/{user_id}/articles/recent{-prefix|.|format}
- rel: recent
- - name: user_article
- - name: edit_user_article
- path_template: /users/{user_id}/articles/{article_id}/edit{-prefix|.|format}
- rel: edit
- path_template: /users/{user_id}/articles/{article_id}{-prefix|.|format}
- path_template: /users/{user_id}/articles{-prefix|.|format}
- rel: articles
- - name: user_profile
- - name: edit_user_profile
- path_template: /users/{user_id}/profile/edit{-prefix|.|format}
- rel: edit
- - name: new_user_profile
- path_template: /users/{user_id}/profile/new{-prefix|.|format}
- rel: new
- path_template: /users/{user_id}/profile{-prefix|.|format}
- rel: profile
- path_template: /users/{user_id}{-prefix|.|format}
- path_template: /users{-prefix|.|format}
View
62 test_rails_app/test/fixtures/build_time/described_routes.yaml_short_no_admin
@@ -1,62 +0,0 @@
----
-- name: root
- path_template: /
-- name: described_routes
- - name: new_described_route
- path_template: /described_routes/new{-prefix|.|format}
- rel: new_described_route
- - name: described_route
- - name: edit_described_route
- path_template: /described_routes/{route_name}/edit{-prefix|.|format}
- rel: edit
- path_template: /described_routes/{route_name}{-prefix|.|format}
- path_template: /described_routes{-prefix|.|format}
-- name: pages
- - name: new_page
- path_template: /pages/new{-prefix|.|format}
- rel: new_page
- - name: page
- - name: edit_page
- path_template: /pages/{page_id}/edit{-prefix|.|format}
- rel: edit
- - name: summary_page
- path_template: /pages/{page_id}/summary{-prefix|.|format}
- rel: summary
- - name: toggle_visibility_page
- path_template: /pages/{page_id}/toggle_visibility{-prefix|.|format}
- rel: toggle_visibility
- path_template: /pages/{page_id}{-prefix|.|format}
- path_template: /pages{-prefix|.|format}
-- name: users
- - name: new_user
- path_template: /users/new{-prefix|.|format}
- rel: new_user
- - name: user
- - name: edit_user
- path_template: /users/{user_id}/edit{-prefix|.|format}
- rel: edit
- - name: user_articles
- - name: new_user_article
- path_template: /users/{user_id}/articles/new{-prefix|.|format}
- rel: new_user_article
- - name: recent_user_articles
- path_template: /users/{user_id}/articles/recent{-prefix|.|format}
- rel: recent
- - name: user_article
- - name: edit_user_article
- path_template: /users/{user_id}/articles/{article_id}/edit{-prefix|.|format}
- rel: edit
- path_template: /users/{user_id}/articles/{article_id}{-prefix|.|format}
- path_template: /users/{user_id}/articles{-prefix|.|format}
- rel: articles
- - name: user_profile
- - name: edit_user_profile
- path_template: /users/{user_id}/profile/edit{-prefix|.|format}
- rel: edit
- - name: new_user_profile
- path_template: /users/{user_id}/profile/new{-prefix|.|format}
- rel: new
- path_template: /users/{user_id}/profile{-prefix|.|format}
- rel: profile
- path_template: /users/{user_id}{-prefix|.|format}
- path_template: /users{-prefix|.|format}
View
99 test_rails_app/test/fixtures/run_time/described_routes.yaml_short
@@ -1,99 +0,0 @@
----
-- name: root
- path_template: /
- uri_template: http://www.example.com/
-- name: admin_products
- - name: new_admin_product
- path_template: /admin/products/new{-prefix|.|format}
- uri_template: http://www.example.com/admin/products/new{-prefix|.|format}
- rel: new_admin_product
- - name: admin_product
- - name: edit_admin_product
- path_template: /admin/products/{product_id}/edit{-prefix|.|format}
- uri_template: http://www.example.com/admin/products/{product_id}/edit{-prefix|.|format}
- rel: edit
- path_template: /admin/products/{product_id}{-prefix|.|format}
- uri_template: http://www.example.com/admin/products/{product_id}{-prefix|.|format}
- path_template: /admin/products{-prefix|.|format}
- uri_template: http://www.example.com/admin/products{-prefix|.|format}
-- name: described_routes
- - name: new_described_route
- path_template: /described_routes/new{-prefix|.|format}
- uri_template: http://www.example.com/described_routes/new{-prefix|.|format}
- rel: new_described_route
- - name: described_route
- - name: edit_described_route
- path_template: /described_routes/{route_name}/edit{-prefix|.|format}
- uri_template: http://www.example.com/described_routes/{route_name}/edit{-prefix|.|format}
- rel: edit
- path_template: /described_routes/{route_name}{-prefix|.|format}
- uri_template: http://www.example.com/described_routes/{route_name}{-prefix|.|format}
- path_template: /described_routes{-prefix|.|format}
- uri_template: http://www.example.com/described_routes{-prefix|.|format}
-- name: pages
- - name: new_page
- path_template: /pages/new{-prefix|.|format}
- uri_template: http://www.example.com/pages/new{-prefix|.|format}
- rel: new_page
- - name: page
- - name: edit_page
- path_template: /pages/{page_id}/edit{-prefix|.|format}
- uri_template: http://www.example.com/pages/{page_id}/edit{-prefix|.|format}
- rel: edit
- - name: summary_page
- path_template: /pages/{page_id}/summary{-prefix|.|format}
- uri_template: http://www.example.com/pages/{page_id}/summary{-prefix|.|format}
- rel: summary
- - name: toggle_visibility_page
- path_template: /pages/{page_id}/toggle_visibility{-prefix|.|format}
- uri_template: http://www.example.com/pages/{page_id}/toggle_visibility{-prefix|.|format}
- rel: toggle_visibility
- path_template: /pages/{page_id}{-prefix|.|format}
- uri_template: http://www.example.com/pages/{page_id}{-prefix|.|format}
- path_template: /pages{-prefix|.|format}
- uri_template: http://www.example.com/pages{-prefix|.|format}
-- name: users
- - name: new_user
- path_template: /users/new{-prefix|.|format}
- uri_template: http://www.example.com/users/new{-prefix|.|format}
- rel: new_user
- - name: user
- - name: edit_user
- path_template: /users/{user_id}/edit{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/edit{-prefix|.|format}
- rel: edit
- - name: user_articles
- - name: new_user_article
- path_template: /users/{user_id}/articles/new{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/articles/new{-prefix|.|format}
- rel: new_user_article
- - name: recent_user_articles
- path_template: /users/{user_id}/articles/recent{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/articles/recent{-prefix|.|format}
- rel: recent
- - name: user_article
- - name: edit_user_article
- path_template: /users/{user_id}/articles/{article_id}/edit{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/articles/{article_id}/edit{-prefix|.|format}
- rel: edit
- path_template: /users/{user_id}/articles/{article_id}{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/articles/{article_id}{-prefix|.|format}
- path_template: /users/{user_id}/articles{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/articles{-prefix|.|format}
- rel: articles
- - name: user_profile
- - name: edit_user_profile
- path_template: /users/{user_id}/profile/edit{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/profile/edit{-prefix|.|format}
- rel: edit
- - name: new_user_profile
- path_template: /users/{user_id}/profile/new{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/profile/new{-prefix|.|format}
- rel: new
- path_template: /users/{user_id}/profile{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}/profile{-prefix|.|format}
- rel: profile
- path_template: /users/{user_id}{-prefix|.|format}
- uri_template: http://www.example.com/users/{user_id}{-prefix|.|format}
- path_template: /users{-prefix|.|format}
- uri_template: http://www.example.com/users{-prefix|.|format}
View
6 test_rails_app/test/integration/described_routes_run_time_test.rb
@@ -29,12 +29,6 @@ def test_yaml
assert_equal(read_fixture("yaml"), body)
end
- def test_yaml_short
- get "/described_routes.yaml?short=1"
- assert_response :success
- assert_equal(read_fixture("yaml_short"), body)
- end
-
def test_partial_expand
get "/described_routes/new_user_profile.text?user_id=dojo&format=json"
assert_equal("new_user_profile new_user_profile GET http://www.example.com/users/dojo/profile/new.json", body.chomp)
View
9 test_rails_app/test/integration/rake_tasks_test.rb
@@ -27,16 +27,7 @@ def test_yaml
assert_equal(read_fixture("yaml"), DescribedRoutes::RakeTaskMethods.yaml)
end
- def test_yaml_short
- assert_equal(read_fixture("yaml_short"), DescribedRoutes::RakeTaskMethods.yaml_short)
- end
-
def test_xml
assert_equal(read_fixture("xml"), DescribedRoutes::RakeTaskMethods.xml)
end
-
- def test_parsed_hook
- DescribedRoutes::RailsRoutes.parsed_hook = lambda {|a| a.reject{|h| h["name"] =~ /^admin/}}
- assert_equal(read_fixture("yaml_short_no_admin"), DescribedRoutes::RakeTaskMethods.yaml_short)
- end
end

0 comments on commit 6e40339

Please sign in to comment.