wsdsl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ class HelloWorldController < SinatraServiceController
2
+ def list
3
+ "Hello #{params[:name]}"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ describe_service "hello_world.xml" do |service|
2
+ service.formats :xml
3
+ service.http_verb :get
4
+ service.disable_auth # on by default
5
+
6
+ service.param.string :name, :default => 'World'
7
+
8
+ service.response do |response|
9
+ response.element(:name => "greeting") do |e|
10
+ e.attribute "message" => :string, :doc => "The greeting message sent back."
11
+ end
12
+ end
13
+
14
+ service.documentation do |doc|
15
+ doc.overall "This service provides a simple hello world implementation example."
16
+ doc.params :name, "The name of the person to greet."
17
+ doc.example "<code>http://ps3.yourgame.com/hello_world.xml?name=Matt</code>"
18
+ end
19
+
20
+ end
@@ -0,0 +1,61 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe ParamsVerification do
4
+
5
+ def app
6
+ Sinatra::Application
7
+ end
8
+
9
+ before :all do
10
+ @service = WSList.all.find{|s| s.url == 'services/test.xml'}
11
+ @service.should_not be_nil
12
+ @valid_params = {'framework' => 'RSpec', 'version' => '1.02', 'user' => {'id' => '123'}}
13
+ end
14
+
15
+ it "should validate valid params" do
16
+ lambda{ ParamsVerification.validate!(@valid_params, @service.defined_params) }.should_not raise_exception
17
+ end
18
+
19
+ it "should return the params" do
20
+ returned_params = ParamsVerification.validate!(@valid_params, @service.defined_params)
21
+ returned_params.should be_an_instance_of(Hash)
22
+ returned_params.keys.size.should >= 3
23
+ end
24
+
25
+ it "should set the default values" do
26
+ @valid_params['timestamp'].should be_nil
27
+ returned_params = ParamsVerification.validate!(@valid_params, @service.defined_params)
28
+ returned_params['timestamp'].should_not be_nil
29
+ end
30
+
31
+ it "should raise an exception when a required param is missing" do
32
+ params = @valid_params.dup
33
+ params.delete('framework')
34
+ lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::MissingParam)
35
+ end
36
+
37
+ it "should raise an exception when a param is of the wrong type" do
38
+ params = @valid_params.dup
39
+ params['user']['id'] = 'abc'
40
+ lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamType)
41
+ end
42
+
43
+ it "should raise an exception when a param is under the minvalue" do
44
+ params = @valid_params.dup
45
+ params['num'] = 1
46
+ lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamType)
47
+ end
48
+
49
+ it "should raise an exception when a param isn't in the param option list" do
50
+ params = @valid_params.dup
51
+ params['alpha'] = 'z'
52
+ lambda{ ParamsVerification.validate!(params, @service.defined_params) }.should raise_exception(ParamsVerification::InvalidParamType)
53
+ end
54
+
55
+ it "should validate that no params are passed when accept_no_params! is set on a service" do
56
+ service = WSList.all.find{|s| s.url == "services/test_no_params.xml"}
57
+ service.should_not be_nil
58
+ lambda{ ParamsVerification.validate!(@valid_params, service.defined_params) }.should raise_exception
59
+ end
60
+
61
+ end
@@ -0,0 +1,14 @@
1
+ require 'rspec'
2
+ require 'rack/test'
3
+ require 'sinatra'
4
+
5
+ require_relative "../lib/wsdsl"
6
+ require_relative 'test_services'
7
+ require_relative "../lib/framework_ext/sinatra_controller"
8
+
9
+ ENV["RACK_ENV"] = 'test'
10
+
11
+ RSpec.configure do |conf|
12
+ conf.include Rack::Test::Methods
13
+ end
14
+
@@ -0,0 +1,70 @@
1
+ WSDSLSpecOptions = ['RSpec', 'Bacon'] # usually pulled from a model
2
+
3
+ describe_service "services/test.xml" do |service|
4
+ service.formats :xml, :json
5
+ service.http_verb :get
6
+
7
+ service.params do |p|
8
+ p.string :framework, :in => WSDSLSpecOptions, :null => false, :required => true
9
+
10
+ p.datetime :timestamp, :default => Time.now
11
+ p.string :alpha, :in => ['a', 'b', 'c']
12
+ p.string :version, :null => false
13
+ p.integer :num, :minvalue => 42
14
+ end
15
+
16
+ # service.param :delta, :optional => true, :type => 'float'
17
+ # # if the optional flag isn't passed, the param is considered required.
18
+ # service.param :epsilon, :type => 'string'
19
+
20
+ service.params.namespace :user do |user|
21
+ user.integer :id, :required => :true
22
+ end
23
+
24
+ # the response contains a list of player creation ratings each object in the list
25
+
26
+ service.response do |response|
27
+ response.element(:name => "player_creation_ratings") do |e|
28
+ e.attribute :id => :integer, :doc => "id doc"
29
+ e.attribute :is_accepted => :boolean, :doc => "is accepted doc"
30
+ e.attribute :name => :string, :doc => "name doc"
31
+
32
+ e.array :name => 'player_creation_rating', :type => 'PlayerCreationRating' do |a|
33
+ a.attribute :comments => :string, :doc => "comments doc"
34
+ a.attribute :player_id => :integer, :doc => "player_id doc"
35
+ a.attribute :rating => :integer, :doc => "rating doc"
36
+ a.attribute :username => :string, :doc => "username doc"
37
+ end
38
+ end
39
+ end
40
+
41
+ service.documentation do |doc|
42
+ # doc.overall <markdown description text>
43
+ doc.overall <<-DOC
44
+ This is a test service used to test the framework.
45
+ DOC
46
+
47
+ # doc.params <name>, <definition>
48
+ doc.param :framework, "The test framework used, could be one of the two following: #{WSDSLSpecOptions.join(", ")}."
49
+ doc.param :version, "The version of the framework to use."
50
+
51
+ # doc.example <markdown text>
52
+ doc.example <<-DOC
53
+ The most common way to use this service looks like that:
54
+ http://example.com/services/test.xml?framework=rspec&version=2.0.0
55
+ DOC
56
+ end
57
+ end
58
+
59
+
60
+ describe_service "services/test_no_params.xml" do |service|
61
+ service.formats :xml
62
+ service.http_verb :get
63
+ service.accept_no_params!
64
+ end
65
+
66
+ describe_service "services.xml" do |service|
67
+ service.formats :xml
68
+ service.http_verb :put
69
+
70
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path("spec_helper", File.dirname(__FILE__))
2
+ require 'sinatra'
3
+ require File.expand_path("./../lib/framework_ext/sinatra.rb", File.dirname(__FILE__))
4
+ WSDSL.send(:include, WSDSLSinatraExtension)
5
+
6
+ describe "Hello World example" do
7
+ require_relative "hello_world_service"
8
+ require_relative "hello_world_controller"
9
+
10
+ def app
11
+ Sinatra::Application
12
+ end
13
+
14
+ before(:all) do
15
+ service = WSList.all.find{|s| s.url == 'hello_world.xml'}
16
+ service.should_not be_nil
17
+ service.load_sinatra_route
18
+ end
19
+
20
+ it "should dispatch the hello world service properly" do
21
+ get "/hello_world.xml"
22
+ last_response.body.should include("Hello World")
23
+ end
24
+
25
+ end
26
+
@@ -0,0 +1,190 @@
1
+ require File.expand_path("spec_helper", File.dirname(__FILE__))
2
+
3
+ describe WSDSL do
4
+
5
+ before :all do
6
+ @service = WSList.all.find{|s| s.url == 'services/test.xml'}
7
+ @service.should_not be_nil
8
+ end
9
+
10
+ it "should have an url" do
11
+ # dummy test since that's how we found the service, but oh well
12
+ @service.url.should == 'services/test.xml'
13
+ end
14
+
15
+ it "should have some http verbs defined" do
16
+ @service.verb.should == :get
17
+ end
18
+
19
+ it "should have supported formats defined" do
20
+ @service.formats.should == [:xml, :json]
21
+ end
22
+
23
+ it "should have params info" do
24
+ @service.params.should be_an_instance_of(WSDSL::Params)
25
+ end
26
+
27
+ it "should have direct access to the required params" do
28
+ @service.required_rules.should == @service.params.list_required
29
+ end
30
+
31
+ it "should have direct access to the optional params" do
32
+ @service.optional_rules.should == @service.params.list_optional
33
+ end
34
+
35
+ it "should have direct access to the nested params" do
36
+ @service.nested_params.should == @service.params.namespaced_params
37
+ end
38
+
39
+ it "should set the controller accordingly" do
40
+ @service.controller_name.should_not be_nil
41
+ @service.controller_name.should == 'ServiceController'
42
+ end
43
+
44
+ it "should set the action accordingly" do
45
+ @service.action.should_not be_nil
46
+ @service.action.should == 'test'
47
+ end
48
+
49
+ it "should support restful routes based on the HTTP verb" do
50
+ service = WSList.all.find{|s| s.url == "services.xml"}
51
+ service.should_not be_nil
52
+ service.http_verb.should == :put
53
+ service.action.should_not be_nil
54
+ service.controller_name.should == 'ServiceController'
55
+ service.action.should == 'update'
56
+ end
57
+
58
+ it "should have a default action" do
59
+ service = WSDSL.new('spec_test.xml')
60
+ service.action.should == 'list'
61
+ end
62
+
63
+ it "should route to show when an id is the last passed param" do
64
+ service = WSDSL.new("players/:id.xml")
65
+ service.action.should == 'show'
66
+ end
67
+
68
+ describe WSDSL::Params do
69
+
70
+ before(:all) do
71
+ @sparams = @service.params
72
+ end
73
+
74
+ it "should have the possibility to have a space name" do
75
+ @sparams.should respond_to(:space_name)
76
+ service_params = WSDSL::Params.new(:space_name => 'spec_test')
77
+ service_params.space_name.should == 'spec_test'
78
+ end
79
+
80
+ it "should have a list of required param rules" do
81
+ @sparams.list_required.should be_an_instance_of(Array)
82
+ @sparams.list_required.length.should == 1
83
+ end
84
+
85
+ it "should have a list of optional param rules" do
86
+ @sparams.list_optional.should be_an_instance_of(Array)
87
+ @sparams.list_optional.length.should == 4
88
+ end
89
+
90
+ it "should have a list of namespaced param rules" do
91
+ @sparams.namespaced_params.should be_an_instance_of(Array)
92
+ @sparams.namespaced_params.length.should == 1
93
+ @sparams.namespaced_params.first.space_name.should == :user
94
+ end
95
+
96
+ describe WSDSL::Params::Rule do
97
+ before :all do
98
+ @rule = @sparams.list_required.first
99
+ @rule.should_not be_nil
100
+ end
101
+
102
+ it "should have a name" do
103
+ @rule.name.should == :framework
104
+ end
105
+
106
+ it "should have options" do
107
+ @rule.options[:type].should == :string
108
+ @rule.options[:in].should == WSDSLSpecOptions
109
+ @rule.options[:null].should be_false
110
+ end
111
+ end
112
+
113
+ end
114
+
115
+ it "should have some documentation" do
116
+ @service.doc.should be_an_instance_of(WSDSL::Documentation)
117
+ end
118
+
119
+ describe WSDSL::Documentation do
120
+ before(:all) do
121
+ @doc = @service.doc
122
+ @doc.should_not be_nil
123
+ end
124
+
125
+ it "should have an overall description" do
126
+ @doc.desc.strip.should == "This is a test service used to test the framework."
127
+ end
128
+
129
+ it "should have a list of params doc" do
130
+ @doc.params_doc.should be_an_instance_of(Hash)
131
+ @doc.params_doc.keys.sort.should == [:framework, :version]
132
+ @doc.params_doc[:framework].should == "The test framework used, could be one of the two following: #{WSDSLSpecOptions.join(", ")}."
133
+ end
134
+
135
+ it "should allow to define namespaced params doc" do
136
+ service = WSList.all.find{|s| s.url == "services.xml"}
137
+ service.documentation do |doc|
138
+ doc.namespace :preference do |ns|
139
+ ns.param :id, "Ze id."
140
+ end
141
+ end
142
+ service.doc.namespaced_params.should_not be_empty
143
+ ns = service.doc.namespaced_params.find{|ns| ns.name == :preference}
144
+ ns.should_not be_nil
145
+ ns.params[:id].should == "Ze id."
146
+ end
147
+
148
+ it "should have an optional list of examples" do
149
+ @doc.examples.should be_an_instance_of(Array)
150
+ @doc.examples.first.should == <<-DOC
151
+ The most common way to use this service looks like that:
152
+ http://example.com/services/test.xml?framework=rspec&version=2.0.0
153
+ DOC
154
+ end
155
+
156
+ it "should have the service response documented" do
157
+ @doc.response.should_not be_nil
158
+ end
159
+
160
+ it "should have documentation for the response elements via the response itself" do
161
+ @service.response.elements.first.should_not be_nil
162
+ @service.response.elements.first.doc.should_not be_nil
163
+ @service.response.elements.first.doc.name.should == "player_creation_ratings"
164
+ end
165
+
166
+ it "should have documentation for a response element attribute" do
167
+ @service.response.elements.first.doc.attributes.should_not be_empty
168
+ @service.response.elements.first.doc.attributes[:id].should == "id doc"
169
+ end
170
+
171
+ it "should have documentation for a response element array" do
172
+ element = @service.response.elements.first
173
+ element.arrays.should_not be_empty
174
+ element.arrays.first.name.should == "player_creation_rating"
175
+ element.arrays.first.obj_type.should == "PlayerCreationRating"
176
+ element.arrays.first.attributes.should_not be_empty
177
+ end
178
+
179
+ it "should have documentation for the attributes of an response element array" do
180
+ element = @service.response.elements.first
181
+ array = element.arrays.first
182
+ attribute = array.attributes.find{|att| att.name == :comments }
183
+ attribute.should_not be_nil
184
+ attribute.name.should == :comments # just in case we change the way to find the attribute
185
+ attribute.doc.should == "comments doc"
186
+ end
187
+
188
+ end
189
+
190
+ end
data/wsdsl.gemspec ADDED
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{wsdsl}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Matt Aimonetti"]
12
+ s.date = %q{2011-02-28}
13
+ s.description = %q{A Ruby DSL describing Web Services without implementation details.}
14
+ s.email = %q{mattaimonetti@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.md",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/documentation.rb",
25
+ "lib/framework_ext/sinatra.rb",
26
+ "lib/framework_ext/sinatra_controller.rb",
27
+ "lib/inflection.rb",
28
+ "lib/params.rb",
29
+ "lib/params_verification.rb",
30
+ "lib/response.rb",
31
+ "lib/ws_list.rb",
32
+ "lib/wsdsl.rb",
33
+ "spec/hello_world_controller.rb",
34
+ "spec/hello_world_service.rb",
35
+ "spec/params_verification_spec.rb",
36
+ "spec/spec_helper.rb",
37
+ "spec/test_services.rb",
38
+ "spec/wsdsl_sinatra_ext_spec.rb",
39
+ "spec/wsdsl_spec.rb",
40
+ "wsdsl.gemspec"
41
+ ]
42
+ s.homepage = %q{http://github.com/mattetti/wsdsl}
43
+ s.licenses = ["MIT"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.3.7}
46
+ s.summary = %q{Web Service DSL}
47
+ s.test_files = [
48
+ "spec/hello_world_controller.rb",
49
+ "spec/hello_world_service.rb",
50
+ "spec/params_verification_spec.rb",
51
+ "spec/spec_helper.rb",
52
+ "spec/test_services.rb",
53
+ "spec/wsdsl_sinatra_ext_spec.rb",
54
+ "spec/wsdsl_spec.rb"
55
+ ]
56
+
57
+ if s.respond_to? :specification_version then
58
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
59
+ s.specification_version = 3
60
+
61
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
62
+ else
63
+ end
64
+ else
65
+ end
66
+ end
67
+