oreflect 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +3 -0
- data/README.md +7 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/examples/basic.rb +10 -0
- data/examples/rest_blog.rb +73 -0
- data/lib/oreflect.rb +22 -0
- data/lib/oreflect/accessors.rb +50 -0
- data/lib/oreflect/action.rb +50 -0
- data/lib/oreflect/endpoint.rb +44 -0
- data/lib/oreflect/error.rb +50 -0
- data/lib/oreflect/example.rb +30 -0
- data/lib/oreflect/param.rb +31 -0
- data/lib/oreflect/service.rb +63 -0
- data/oreflect.gemspec +75 -0
- data/test/helper.rb +12 -0
- data/test/test_action.rb +66 -0
- data/test/test_endpoint.rb +43 -0
- data/test/test_error.rb +30 -0
- data/test/test_example.rb +39 -0
- data/test/test_oreflect.rb +22 -0
- data/test/test_param.rb +45 -0
- data/test/test_service.rb +116 -0
- metadata +105 -0
data/.document
ADDED
data/.gitignore
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gem|
|
6
|
+
gem.name = 'oreflect'
|
7
|
+
gem.summary = 'A library for building reflection endpoints to REST services and consuming them.'
|
8
|
+
gem.description = 'A library for building reflection endpoints to REST services and consuming them.'
|
9
|
+
gem.email = 'heycarsten@gmail.com'
|
10
|
+
gem.homepage = 'http://oreflect.org'
|
11
|
+
gem.author = 'Carsten Nielsen'
|
12
|
+
gem.add_development_dependency 'shoulda', '>= 0'
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts 'Jeweler (or a dependency) not available. Install it with: gem install jeweler'
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/test_*.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort 'RCov is not available. In order to run rcov, you must: sudo gem install rcov'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task :test => :check_dependencies
|
40
|
+
task :default => :test
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "oreflect #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.7.0
|
data/examples/basic.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
service do
|
2
|
+
version 2
|
3
|
+
name 'REST Blog'
|
4
|
+
desc 'A blog system that conforms to the ideals of REST architecture'
|
5
|
+
base_uri 'http://restblog.example.com'
|
6
|
+
|
7
|
+
resource '/articles.:format' do
|
8
|
+
arg :format do
|
9
|
+
desc 'The input format'
|
10
|
+
accepted_values 'xml', 'json'
|
11
|
+
end
|
12
|
+
|
13
|
+
action :get do
|
14
|
+
name 'Show Articles'
|
15
|
+
desc 'Get all Articles matching criteria'
|
16
|
+
|
17
|
+
query :before_date do
|
18
|
+
desc 'Shows articles before this date or time'
|
19
|
+
examples '2009-09-26'
|
20
|
+
end
|
21
|
+
|
22
|
+
query :after_date do
|
23
|
+
desc 'Show articles after this date'
|
24
|
+
examples '2009-09-26'
|
25
|
+
end
|
26
|
+
|
27
|
+
query :tag do
|
28
|
+
is_multiparam!
|
29
|
+
desc 'Show articles that contain this tag or tags if many are provided.'
|
30
|
+
examples 'heycarsten', 'unspace', 'leftist', 'top100'
|
31
|
+
end
|
32
|
+
|
33
|
+
query :page do
|
34
|
+
desc 'Show this page of articles'
|
35
|
+
examples '1', '2', '3'
|
36
|
+
end
|
37
|
+
|
38
|
+
example do
|
39
|
+
path '/articles.json'
|
40
|
+
desc 'View the latest articles'
|
41
|
+
end
|
42
|
+
|
43
|
+
example do
|
44
|
+
path '/articles.json?page=2'
|
45
|
+
desc 'View page two of the latest articles'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
action :post do
|
50
|
+
is_authenticated!
|
51
|
+
name 'Create Article'
|
52
|
+
desc 'Creates a new article'
|
53
|
+
body 'article[title]'
|
54
|
+
body 'article[author_name]'
|
55
|
+
body 'article[author_email]'
|
56
|
+
body 'article[publish_on]'
|
57
|
+
body 'article[body]'
|
58
|
+
|
59
|
+
example do
|
60
|
+
path '/articles.json'
|
61
|
+
body 'article[title]', 'Test Article'
|
62
|
+
body 'article[author_name]', 'Carsten Nielsen'
|
63
|
+
body 'article[author_email]', 'heycarsten@example.com'
|
64
|
+
body 'article[publish_on]', '2010-02-22 00:00:00 -0500'
|
65
|
+
body 'article[:body]', "# Title\n\nThis is a test.\n"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
resource '/articles/:id.:format'
|
71
|
+
resource '/articles/:article_id/comments.:format'
|
72
|
+
resource '/articles/:article_id/comments/:id.:format'
|
73
|
+
end
|
data/lib/oreflect.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'oreflect/accessors'
|
2
|
+
require 'oreflect/action'
|
3
|
+
require 'oreflect/endpoint'
|
4
|
+
require 'oreflect/error'
|
5
|
+
require 'oreflect/example'
|
6
|
+
require 'oreflect/param'
|
7
|
+
require 'oreflect/service'
|
8
|
+
|
9
|
+
|
10
|
+
module OReflect
|
11
|
+
|
12
|
+
def service(&block)
|
13
|
+
Service.new(&block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_file(filepath)
|
17
|
+
class_eval File.read(filepath)
|
18
|
+
end
|
19
|
+
|
20
|
+
module_function :service, :load_file
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module OReflect
|
2
|
+
module Accessors
|
3
|
+
|
4
|
+
def self.included(mod)
|
5
|
+
mod.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def boolean_accessor(*names)
|
10
|
+
names.each do |attr_name|
|
11
|
+
dynamic_accessor attr_name
|
12
|
+
|
13
|
+
define_method "#{attr_name}?" do
|
14
|
+
instance_variable_get("@#{attr_name}") ? true : false
|
15
|
+
end
|
16
|
+
|
17
|
+
define_method "#{attr_name}!" do
|
18
|
+
instance_variable_set "@#{attr_name}", true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def splat_accessor(*names)
|
24
|
+
names.each do |attr_name|
|
25
|
+
define_method attr_name do |*values|
|
26
|
+
instance_variable_set("@#{attr_name}", []) unless instance_variable_get("@#{attr_name}")
|
27
|
+
if values.empty?
|
28
|
+
instance_variable_get "@#{attr_name}"
|
29
|
+
else
|
30
|
+
instance_variable_get("@#{attr_name}").concat(values)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def dynamic_accessor(*names)
|
37
|
+
names.each do |attr_name|
|
38
|
+
define_method attr_name do |*args|
|
39
|
+
if args[0]
|
40
|
+
instance_variable_set "@#{attr_name}", args[0]
|
41
|
+
else
|
42
|
+
instance_variable_get "@#{attr_name}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module OReflect
|
2
|
+
class Action
|
3
|
+
|
4
|
+
include Accessors
|
5
|
+
|
6
|
+
attr_reader :form_params, :query_params, :examples, :endpoint
|
7
|
+
dynamic_accessor :method, :name, :description, :identifier
|
8
|
+
boolean_accessor :is_authenticated
|
9
|
+
alias_method :desc, :description
|
10
|
+
alias_method :id, :identifier
|
11
|
+
|
12
|
+
def initialize(method, identifier = nil, endpoint = nil, &block)
|
13
|
+
@method = method
|
14
|
+
@identifier = identifier
|
15
|
+
@endpoint = endpoint
|
16
|
+
@query_params = []
|
17
|
+
@form_params = []
|
18
|
+
@examples = []
|
19
|
+
instance_eval(&block) if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
def example(&block)
|
23
|
+
@examples << Example.new(&block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def query(key, &block)
|
27
|
+
@query_params << Param.new(key, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def body(key, &block)
|
31
|
+
@form_params << Param.new(key, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def as_hash
|
35
|
+
h = {}
|
36
|
+
h[:name] = name
|
37
|
+
h[:identifier] = identifier
|
38
|
+
h[:method] = method
|
39
|
+
h[:is_authenticated] = is_authenticated?
|
40
|
+
h[:description] = description
|
41
|
+
h[:examples] = examples.map { |example| example.as_hash }
|
42
|
+
h[:query_params] = query_params.inject({}) { |hsh, param|
|
43
|
+
hsh.merge(param.key => param.as_hash) }
|
44
|
+
h[:form_params] = form_params.inject({}) { |hsh, param|
|
45
|
+
hsh.merge(param.key => param.as_hash) }
|
46
|
+
h
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module OReflect
|
2
|
+
class Endpoint
|
3
|
+
|
4
|
+
include Accessors
|
5
|
+
|
6
|
+
attr_reader :actions, :arguments
|
7
|
+
dynamic_accessor :name, :description, :template
|
8
|
+
alias_method :desc, :description
|
9
|
+
|
10
|
+
def initialize(template, is_collection = false, &block)
|
11
|
+
@template = template
|
12
|
+
@is_collection = is_collection
|
13
|
+
@actions = []
|
14
|
+
@arguments = []
|
15
|
+
instance_eval(&block) if block_given?
|
16
|
+
end
|
17
|
+
|
18
|
+
def is_collection?
|
19
|
+
@is_collection ? true : false
|
20
|
+
end
|
21
|
+
|
22
|
+
def arg(key, &block)
|
23
|
+
@arguments << Param.new(key, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def action(method, id = nil, &block)
|
27
|
+
@actions << Action.new(method, id, self, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def as_hash
|
31
|
+
h = {}
|
32
|
+
h[:name] = name
|
33
|
+
h[:description] = description
|
34
|
+
h[:template] = template
|
35
|
+
h[:is_collection] = is_collection?
|
36
|
+
h[:arguments] = arguments.inject({}) do |hsh, arg|
|
37
|
+
hsh.merge(arg.key => arg.as_hash(:is_required))
|
38
|
+
end
|
39
|
+
h[:actions] = actions.map { |action| action.as_hash }
|
40
|
+
h
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module OReflect
|
2
|
+
class Error
|
3
|
+
|
4
|
+
include Accessors
|
5
|
+
|
6
|
+
DEFAULT_ERRORS = lambda do
|
7
|
+
error 500 do
|
8
|
+
title 'Application Error'
|
9
|
+
desc 'An error occurred on the server'
|
10
|
+
end
|
11
|
+
|
12
|
+
error 503 do
|
13
|
+
title 'Down For Maintenance'
|
14
|
+
desc 'The service is temporarily unavailable due to maintenance'
|
15
|
+
end
|
16
|
+
|
17
|
+
error 404 do
|
18
|
+
title 'Resource Not Found'
|
19
|
+
desc 'The resource you identified does not exist on the server'
|
20
|
+
end
|
21
|
+
|
22
|
+
error 403 do
|
23
|
+
title 'Authorization Required'
|
24
|
+
desc 'You need to authenticate to access the resource'
|
25
|
+
end
|
26
|
+
|
27
|
+
error 400 do
|
28
|
+
title 'Invalid Request'
|
29
|
+
desc 'One or more params were malformed and the server has rejected the request'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
dynamic_accessor :code, :desc, :title
|
34
|
+
alias_method :description, :desc
|
35
|
+
|
36
|
+
def initialize(code, &block)
|
37
|
+
@code = code
|
38
|
+
instance_eval(&block) if block_given?
|
39
|
+
end
|
40
|
+
|
41
|
+
def as_hash
|
42
|
+
h = {}
|
43
|
+
h[:code] = code
|
44
|
+
h[:description] = description
|
45
|
+
h[:title] = title
|
46
|
+
h
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module OReflect
|
2
|
+
class Example
|
3
|
+
|
4
|
+
include Accessors
|
5
|
+
|
6
|
+
attr_reader :form_values
|
7
|
+
dynamic_accessor :path, :description, :title, :response
|
8
|
+
alias_method :desc, :description
|
9
|
+
|
10
|
+
def initialize(&block)
|
11
|
+
@form_values = {}
|
12
|
+
instance_eval(&block) if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
def body(key, value)
|
16
|
+
@form_values[key] = value
|
17
|
+
end
|
18
|
+
|
19
|
+
def as_hash
|
20
|
+
h = {}
|
21
|
+
h[:title] = title
|
22
|
+
h[:path] = path
|
23
|
+
h[:description] = description
|
24
|
+
h[:response] = response
|
25
|
+
h[:form_values] = form_values
|
26
|
+
h
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module OReflect
|
2
|
+
class Param
|
3
|
+
|
4
|
+
include Accessors
|
5
|
+
|
6
|
+
dynamic_accessor :key, :description
|
7
|
+
boolean_accessor :is_required, :is_multiparam
|
8
|
+
splat_accessor :excluded_params, :required_params, :accepted_values, :examples
|
9
|
+
alias_method :desc, :description
|
10
|
+
|
11
|
+
def initialize(key, &block)
|
12
|
+
@key = key
|
13
|
+
instance_eval(&block) if block_given?
|
14
|
+
end
|
15
|
+
|
16
|
+
def as_hash(*exclusions)
|
17
|
+
h = {}
|
18
|
+
h[:is_required] = is_required?
|
19
|
+
h[:is_multiparam] = is_multiparam?
|
20
|
+
h[:key] = key
|
21
|
+
h[:description] = description
|
22
|
+
h[:examples] = examples
|
23
|
+
h[:accepted_values] = accepted_values
|
24
|
+
h[:required_params] = required_params
|
25
|
+
h[:excluded_params] = excluded_params
|
26
|
+
exclusions.each { |key| h.delete(key) }
|
27
|
+
h
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module OReflect
|
2
|
+
class Service
|
3
|
+
|
4
|
+
include Accessors
|
5
|
+
|
6
|
+
DEFAULT_VERSION = 1
|
7
|
+
|
8
|
+
attr_reader :errors, :endpoints
|
9
|
+
dynamic_accessor :base_uri, :name, :description
|
10
|
+
alias_method :desc, :description
|
11
|
+
|
12
|
+
def initialize(&block)
|
13
|
+
@version = DEFAULT_VERSION
|
14
|
+
@endpoints = []
|
15
|
+
@errors = {}
|
16
|
+
instance_eval(&Error::DEFAULT_ERRORS)
|
17
|
+
instance_eval(&block) if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
def actions
|
21
|
+
endpoints.map { |endpoint| endpoint.actions }.flatten
|
22
|
+
end
|
23
|
+
|
24
|
+
def action_by_id(action_id)
|
25
|
+
actions.detect { |a| a.id.to_s == action_id.to_s }
|
26
|
+
end
|
27
|
+
|
28
|
+
def action_by_dashed_id(dashed_action_id)
|
29
|
+
action_by_id dashed_action_id.gsub('-', '_')
|
30
|
+
end
|
31
|
+
|
32
|
+
def version(value = nil)
|
33
|
+
return @version unless value
|
34
|
+
raise ArgumentError, 'value must be an integer' unless value.is_a?(Fixnum)
|
35
|
+
@version = value
|
36
|
+
end
|
37
|
+
|
38
|
+
def resource(template, &block)
|
39
|
+
@endpoints << Endpoint.new(template, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def resources(template, &block)
|
43
|
+
@endpoints << Endpoint.new(template, true, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def error(code, &block)
|
47
|
+
@errors[code] = Error.new(code, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
def as_hash
|
51
|
+
h = {}
|
52
|
+
h[:version] = version
|
53
|
+
h[:name] = name
|
54
|
+
h[:description] = description
|
55
|
+
h[:base_uri] = base_uri
|
56
|
+
h[:errors] = errors.values.inject({}) { |hsh, err|
|
57
|
+
hsh.merge(err.code => err.as_hash) }
|
58
|
+
h[:endpoints] = endpoints.map { |endpoint| endpoint.as_hash }
|
59
|
+
h
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
data/oreflect.gemspec
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{oreflect}
|
8
|
+
s.version = "0.7.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Carsten Nielsen"]
|
12
|
+
s.date = %q{2010-05-26}
|
13
|
+
s.description = %q{A library for building reflection endpoints to REST services and consuming them.}
|
14
|
+
s.email = %q{heycarsten@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
".gitignore",
|
21
|
+
"README.md",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"examples/basic.rb",
|
25
|
+
"examples/rest_blog.rb",
|
26
|
+
"lib/oreflect.rb",
|
27
|
+
"lib/oreflect/accessors.rb",
|
28
|
+
"lib/oreflect/action.rb",
|
29
|
+
"lib/oreflect/endpoint.rb",
|
30
|
+
"lib/oreflect/error.rb",
|
31
|
+
"lib/oreflect/example.rb",
|
32
|
+
"lib/oreflect/param.rb",
|
33
|
+
"lib/oreflect/service.rb",
|
34
|
+
"oreflect.gemspec",
|
35
|
+
"test/helper.rb",
|
36
|
+
"test/test_action.rb",
|
37
|
+
"test/test_endpoint.rb",
|
38
|
+
"test/test_error.rb",
|
39
|
+
"test/test_example.rb",
|
40
|
+
"test/test_oreflect.rb",
|
41
|
+
"test/test_param.rb",
|
42
|
+
"test/test_service.rb"
|
43
|
+
]
|
44
|
+
s.homepage = %q{http://oreflect.org}
|
45
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
46
|
+
s.require_paths = ["lib"]
|
47
|
+
s.rubygems_version = %q{1.3.6}
|
48
|
+
s.summary = %q{A library for building reflection endpoints to REST services and consuming them.}
|
49
|
+
s.test_files = [
|
50
|
+
"test/helper.rb",
|
51
|
+
"test/test_action.rb",
|
52
|
+
"test/test_endpoint.rb",
|
53
|
+
"test/test_error.rb",
|
54
|
+
"test/test_example.rb",
|
55
|
+
"test/test_oreflect.rb",
|
56
|
+
"test/test_param.rb",
|
57
|
+
"test/test_service.rb",
|
58
|
+
"examples/basic.rb",
|
59
|
+
"examples/rest_blog.rb"
|
60
|
+
]
|
61
|
+
|
62
|
+
if s.respond_to? :specification_version then
|
63
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
64
|
+
s.specification_version = 3
|
65
|
+
|
66
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
67
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
68
|
+
else
|
69
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
70
|
+
end
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
data/test/helper.rb
ADDED
data/test/test_action.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestAction < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'An instance of Action' do
|
6
|
+
setup do
|
7
|
+
@action = OReflect::Action.new(:get, :get_test) do
|
8
|
+
is_authenticated!
|
9
|
+
name 'Test'
|
10
|
+
desc 'test desc'
|
11
|
+
|
12
|
+
body :test_param do
|
13
|
+
is_multiparam!
|
14
|
+
desc 'test desc'
|
15
|
+
examples 'example1', 'example2'
|
16
|
+
accepted_values 'test1'
|
17
|
+
required_params 'testfest'
|
18
|
+
excluded_params 'testnest'
|
19
|
+
end
|
20
|
+
|
21
|
+
query :q do
|
22
|
+
is_required!
|
23
|
+
desc 'test'
|
24
|
+
examples 'test1', 'test2'
|
25
|
+
end
|
26
|
+
|
27
|
+
example do
|
28
|
+
title 'test'
|
29
|
+
path '/test'
|
30
|
+
desc 'test desc'
|
31
|
+
response '{}'
|
32
|
+
body :key1, 'value1'
|
33
|
+
body :key2, 'value2'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
should 'provide required accessors' do
|
39
|
+
assert_respond_to @action, :identifier
|
40
|
+
assert_respond_to @action, :form_params
|
41
|
+
assert_respond_to @action, :query_params
|
42
|
+
assert_respond_to @action, :description
|
43
|
+
assert_respond_to @action, :method
|
44
|
+
assert_respond_to @action, :examples
|
45
|
+
assert_respond_to @action, :name
|
46
|
+
assert_respond_to @action, :is_authenticated?
|
47
|
+
end
|
48
|
+
|
49
|
+
context '#as_hash' do
|
50
|
+
setup { @h = @action.as_hash }
|
51
|
+
|
52
|
+
should 'return a hash with correct keys and values' do
|
53
|
+
assert_equal true, @h[:is_authenticated]
|
54
|
+
assert_equal :get, @h[:method]
|
55
|
+
assert_equal :get_test, @h[:identifier]
|
56
|
+
assert_equal 'Test', @h[:name]
|
57
|
+
assert_equal 'test desc', @h[:description]
|
58
|
+
assert_instance_of Hash, @h[:form_params][:test_param]
|
59
|
+
assert_instance_of Hash, @h[:query_params][:q]
|
60
|
+
assert_instance_of Array, @h[:examples]
|
61
|
+
assert_equal '{}', @h[:examples][0][:response]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestEndpoint < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'An instance of Endpoint' do
|
6
|
+
setup do
|
7
|
+
@endpoint = OReflect::Endpoint.new('/tests/:arg/items', true) do
|
8
|
+
name 'Test'
|
9
|
+
desc 'test desc'
|
10
|
+
arg :arg
|
11
|
+
action :get, :get_test
|
12
|
+
action :post
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
should 'provide reqired accessors' do
|
17
|
+
assert_respond_to @endpoint, :is_collection?
|
18
|
+
assert_respond_to @endpoint, :template
|
19
|
+
assert_respond_to @endpoint, :name
|
20
|
+
assert_respond_to @endpoint, :description
|
21
|
+
assert_respond_to @endpoint, :arguments
|
22
|
+
assert_respond_to @endpoint, :actions
|
23
|
+
end
|
24
|
+
|
25
|
+
context '#as_hash' do
|
26
|
+
setup { @h = @endpoint.as_hash }
|
27
|
+
|
28
|
+
should 'return a hash with correct keys and values' do
|
29
|
+
assert_equal true, @h[:is_collection]
|
30
|
+
assert_equal '/tests/:arg/items', @h[:template]
|
31
|
+
assert_equal 'Test', @h[:name]
|
32
|
+
assert_equal 'test desc', @h[:description]
|
33
|
+
assert_instance_of Hash, @h[:arguments]
|
34
|
+
assert_instance_of Array, @h[:actions]
|
35
|
+
end
|
36
|
+
|
37
|
+
should 'not have an is_required field in argument params' do
|
38
|
+
assert_nil @h[:arguments][:arg][:is_required]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/test/test_error.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestError < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'An instance of Error' do
|
6
|
+
setup do
|
7
|
+
@error = OReflect::Error.new(500) do
|
8
|
+
title 'test'
|
9
|
+
desc 'test desc'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
should 'provide required accessors' do
|
14
|
+
assert_respond_to @error, :code
|
15
|
+
assert_respond_to @error, :title
|
16
|
+
assert_respond_to @error, :description
|
17
|
+
end
|
18
|
+
|
19
|
+
context '#as_hash' do
|
20
|
+
setup { @h = @error.as_hash }
|
21
|
+
|
22
|
+
should 'return appropriate values' do
|
23
|
+
assert_equal 500, @h[:code]
|
24
|
+
assert_equal 'test', @h[:title]
|
25
|
+
assert_equal 'test desc', @h[:description]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestExample < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'An instance of Example' do
|
6
|
+
setup do
|
7
|
+
@example = OReflect::Example.new do
|
8
|
+
title 'test'
|
9
|
+
path '/test'
|
10
|
+
desc 'test desc'
|
11
|
+
response '{}'
|
12
|
+
body :key1, 'value1'
|
13
|
+
body :key2, 'value2'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
should 'provide required accessors' do
|
18
|
+
assert_respond_to @example, :title
|
19
|
+
assert_respond_to @example, :path
|
20
|
+
assert_respond_to @example, :description
|
21
|
+
assert_respond_to @example, :response
|
22
|
+
assert_respond_to @example, :form_values
|
23
|
+
end
|
24
|
+
|
25
|
+
context '#as_hash' do
|
26
|
+
setup { @h = @example.as_hash }
|
27
|
+
|
28
|
+
should 'return proper values' do
|
29
|
+
assert_equal 'test', @h[:title]
|
30
|
+
assert_equal '/test', @h[:path]
|
31
|
+
assert_equal 'test desc', @h[:description]
|
32
|
+
assert_equal '{}', @h[:response]
|
33
|
+
assert_equal 'value1', @h[:form_values][:key1]
|
34
|
+
assert_equal 'value2', @h[:form_values][:key2]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestReflector < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'OReflect' do
|
6
|
+
%w[examples/rest_blog.rb examples/basic.rb].each do |file|
|
7
|
+
context "#load_file('#{file}')" do
|
8
|
+
setup { @service = OReflect.load_file(file) }
|
9
|
+
|
10
|
+
should 'be a valid instance of Service' do
|
11
|
+
assert_instance_of OReflect::Service, @service
|
12
|
+
end
|
13
|
+
|
14
|
+
should 'be able to provide a Hash representation' do
|
15
|
+
assert_instance_of Hash, @service.as_hash
|
16
|
+
assert !@service.as_hash.empty?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/test/test_param.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestParam < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'An instance of Param' do
|
6
|
+
setup do
|
7
|
+
@param = OReflect::Param.new(:test) do
|
8
|
+
is_multiparam!
|
9
|
+
is_required!
|
10
|
+
desc 'test desc'
|
11
|
+
examples 'example1', 'example2'
|
12
|
+
accepted_values 'test1'
|
13
|
+
required_params 'testfest'
|
14
|
+
excluded_params 'testnest'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
should 'provide required accessors' do
|
19
|
+
assert_respond_to @param, :is_multiparam?
|
20
|
+
assert_respond_to @param, :is_required?
|
21
|
+
assert_respond_to @param, :key
|
22
|
+
assert_respond_to @param, :description
|
23
|
+
assert_respond_to @param, :examples
|
24
|
+
assert_respond_to @param, :accepted_values
|
25
|
+
assert_respond_to @param, :required_params
|
26
|
+
assert_respond_to @param, :excluded_params
|
27
|
+
end
|
28
|
+
|
29
|
+
context '#as_hash' do
|
30
|
+
setup { @h = @param.as_hash }
|
31
|
+
|
32
|
+
should 'return the correct values' do
|
33
|
+
assert_equal true, @h[:is_multiparam]
|
34
|
+
assert_equal true, @h[:is_required]
|
35
|
+
assert_equal :test, @h[:key]
|
36
|
+
assert_equal 'test desc', @h[:description]
|
37
|
+
assert_equal 2, @h[:examples].size
|
38
|
+
assert_equal 1, @h[:accepted_values].size
|
39
|
+
assert_equal 1, @h[:required_params].size
|
40
|
+
assert_equal 1, @h[:excluded_params].size
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestService < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'Service' do
|
6
|
+
should 'only accept integers as version' do
|
7
|
+
assert_raise(ArgumentError) { OReflect::Service.new.version(1.0) }
|
8
|
+
assert_raise(ArgumentError) { OReflect::Service.new.version('1') }
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'have a default version' do
|
12
|
+
assert_equal 1, OReflect::Service.new.version
|
13
|
+
end
|
14
|
+
|
15
|
+
should 'have a number of default errors' do
|
16
|
+
assert !OReflect::Service.new.errors.empty?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'An instance of Service' do
|
21
|
+
setup do
|
22
|
+
@service = OReflect::Service.new do
|
23
|
+
version 3
|
24
|
+
name 'Test'
|
25
|
+
desc 'test desc'
|
26
|
+
base_uri 'http://example.com'
|
27
|
+
error 900 do
|
28
|
+
desc 'test error'
|
29
|
+
end
|
30
|
+
resource :test do
|
31
|
+
action :get, :test_action
|
32
|
+
end
|
33
|
+
resources :tests do
|
34
|
+
action :get
|
35
|
+
action :put
|
36
|
+
action :post
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
should 'provide required accessors' do
|
42
|
+
assert_respond_to @service, :version
|
43
|
+
assert_respond_to @service, :name
|
44
|
+
assert_respond_to @service, :description
|
45
|
+
assert_respond_to @service, :base_uri
|
46
|
+
assert_respond_to @service, :errors
|
47
|
+
assert_respond_to @service, :endpoints
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'adding an error' do
|
51
|
+
setup { @service.error(500) { title 'Crash Bang Boom' } }
|
52
|
+
|
53
|
+
should 'replace the default error' do
|
54
|
+
assert_equal 'Crash Bang Boom', @service.errors[500].title
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context '#action_by_id' do
|
59
|
+
should 'return the action when given a symbol' do
|
60
|
+
assert_equal :test_action, @service.action_by_id('test_action').id
|
61
|
+
end
|
62
|
+
|
63
|
+
should 'return the action when given a string' do
|
64
|
+
assert_equal :test_action, @service.action_by_id(:test_action).id
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context '#action_by_dashed_id' do
|
69
|
+
setup { @action = @service.action_by_dashed_id('test-action') }
|
70
|
+
|
71
|
+
should 'return the proper action' do
|
72
|
+
assert_equal :test_action, @action.id
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context '#actions' do
|
77
|
+
setup { @actions = @service.actions }
|
78
|
+
|
79
|
+
should 'return all actions in all endpoints' do
|
80
|
+
assert_equal 4, @actions.size
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context '#version' do
|
85
|
+
should 'return the current version' do
|
86
|
+
assert_equal 3, @service.version
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context '#endpoints' do
|
91
|
+
should 'return available endpoints' do
|
92
|
+
assert_equal 2, @service.endpoints.size
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context '#errors' do
|
97
|
+
should 'return a list of errors' do
|
98
|
+
assert @service.errors.values.any?
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context '#as_hash' do
|
103
|
+
setup { @h = @service.as_hash }
|
104
|
+
|
105
|
+
should 'return the correct values' do
|
106
|
+
assert_equal 3, @h[:version]
|
107
|
+
assert_equal 'Test', @h[:name]
|
108
|
+
assert_equal 'test desc', @h[:description]
|
109
|
+
assert_equal 'http://example.com', @h[:base_uri]
|
110
|
+
assert_instance_of Hash, @h[:errors]
|
111
|
+
assert_instance_of Array, @h[:endpoints]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oreflect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 7
|
8
|
+
- 0
|
9
|
+
version: 0.7.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Carsten Nielsen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-26 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description: A library for building reflection endpoints to REST services and consuming them.
|
33
|
+
email: heycarsten@gmail.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README.md
|
40
|
+
files:
|
41
|
+
- .document
|
42
|
+
- .gitignore
|
43
|
+
- README.md
|
44
|
+
- Rakefile
|
45
|
+
- VERSION
|
46
|
+
- examples/basic.rb
|
47
|
+
- examples/rest_blog.rb
|
48
|
+
- lib/oreflect.rb
|
49
|
+
- lib/oreflect/accessors.rb
|
50
|
+
- lib/oreflect/action.rb
|
51
|
+
- lib/oreflect/endpoint.rb
|
52
|
+
- lib/oreflect/error.rb
|
53
|
+
- lib/oreflect/example.rb
|
54
|
+
- lib/oreflect/param.rb
|
55
|
+
- lib/oreflect/service.rb
|
56
|
+
- oreflect.gemspec
|
57
|
+
- test/helper.rb
|
58
|
+
- test/test_action.rb
|
59
|
+
- test/test_endpoint.rb
|
60
|
+
- test/test_error.rb
|
61
|
+
- test/test_example.rb
|
62
|
+
- test/test_oreflect.rb
|
63
|
+
- test/test_param.rb
|
64
|
+
- test/test_service.rb
|
65
|
+
has_rdoc: true
|
66
|
+
homepage: http://oreflect.org
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --charset=UTF-8
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
requirements: []
|
89
|
+
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.3.6
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: A library for building reflection endpoints to REST services and consuming them.
|
95
|
+
test_files:
|
96
|
+
- test/helper.rb
|
97
|
+
- test/test_action.rb
|
98
|
+
- test/test_endpoint.rb
|
99
|
+
- test/test_error.rb
|
100
|
+
- test/test_example.rb
|
101
|
+
- test/test_oreflect.rb
|
102
|
+
- test/test_param.rb
|
103
|
+
- test/test_service.rb
|
104
|
+
- examples/basic.rb
|
105
|
+
- examples/rest_blog.rb
|