Skip to content
This repository has been archived by the owner on Jan 2, 2018. It is now read-only.

Commit

Permalink
renamed to avoid confusion, thanks Mark Tabler for the name suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
mattetti committed Apr 4, 2012
1 parent 5a66632 commit 9f8c65a
Show file tree
Hide file tree
Showing 21 changed files with 217 additions and 133 deletions.
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,4 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in wsdsl.gemspec
# Specify your gem's dependencies in weasel_diesel.gemspec
gemspec
38 changes: 38 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,38 @@
PATH
remote: .
specs:
wsdsl (1.0.0)

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
rack (1.4.1)
rack-protection (1.2.0)
rack
rack-test (0.6.1)
rack (>= 1.0)
rspec (2.9.0)
rspec-core (~> 2.9.0)
rspec-expectations (~> 2.9.0)
rspec-mocks (~> 2.9.0)
rspec-core (2.9.0)
rspec-expectations (2.9.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.9.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
yard (0.7.5)

PLATFORMS
ruby

DEPENDENCIES
rack-test
rspec
sinatra
wsdsl!
yard
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2011 Matt Aimonetti
Copyright (c) 2012 Matt Aimonetti

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
60 changes: 53 additions & 7 deletions README.md
@@ -1,9 +1,47 @@
# Web Service DSL

WSDSL is a simple DSL allowing developers to simply describe and
document their web APIS. For instance:


Weasel Diesel is a simple DSL allowing developers to simply describe and
document their web APIS.
The DSL is already setup on top of Sinatra in this [example
application](https://github.com/mattetti/sinatra-web-api-example) that
you can simply fork and use as a base for your application.

DSL examples:

``` ruby
describe_service "hello_world" do |service|
service.formats :json
service.http_verb :get
service.disable_auth # on by default

# INPUT
service.param.string :name, :default => 'World'

# OUTPUT
service.response do |response|
response.object do |obj|
obj.string :message, :doc => "The greeting message sent back. Defaults to 'World'"
obj.datetime :at, :doc => "The timestamp of when the message was dispatched"
end
end

# DOCUMENTATION
service.documentation do |doc|
doc.overall "This service provides a simple hello world implementation example."
doc.param :name, "The name of the person to greet."
doc.example "<code>curl -I 'http://localhost:9292/hello_world?name=Matt'</code>"
end

# ACTION/IMPLEMENTATION (specific to the sinatra app example, can
# instead be set to call a controller action)
service.implementation do
{:message => "Hello #{params[:name]}", :at => Time.now}.to_json
end

end
```

``` ruby
describe_service "hello_world" do |service|
service.formats :xml
service.http_verb :get
Expand All @@ -24,10 +62,11 @@ document their web APIS. For instance:
end

end

```

Or a more complex example:

``` ruby
SpecOptions = ['RSpec', 'Bacon'] # usually pulled from a model

describe_service "wsdsl/test.xml" do |service|
Expand Down Expand Up @@ -85,14 +124,15 @@ Or a more complex example:
DOC
end
end

```

## JSON APIs

This library was designed with XML responses in mind and JSON support
was added later on which explains why some response methods are aliases.
Consider the following JSON response:

``` json
{ people: [
{
id : 1,
Expand All @@ -116,6 +156,7 @@ Consider the following JSON response:

It would be described as follows:

``` ruby
describe_service "json_list" do |service|
service.formats :json
service.response do |response|
Expand All @@ -130,6 +171,7 @@ It would be described as follows:
end
end
end
```

Nodes/elements can also use some meta attributes. Currently the
following meta attributes are available:
Expand All @@ -156,23 +198,27 @@ via the backports libary and some tweaks. However, because unlike in
ruby 1.9, the hash insert order isn't kept in 1.8 the following syntax
isn't supported and the alternative version needs to be used:

``` ruby
service.response do |response|
response.element(:name => "player_creation_ratings") do |e|
e.attribute :id => :integer, :doc => "id doc"
e.attribute :is_accepted => :boolean, :doc => "is accepted doc"
e.attribute :name => :string, :doc => "name doc"
end
end
```

Instead the following version should be used:

``` ruby
service.response do |response|
response.element(:name => "player_creation_ratings") do |e|
e.integer :id, :doc => "id doc"
e.boolean :is_accepted, :doc => "is accepted doc"
e.string :name, :doc => "name doc"
end
end
```

Both code snippets do the exact same thing but the first version is 1.9
only.
Expand All @@ -181,5 +227,5 @@ only.

## Copyright

Copyright (c) 2011 Matt Aimonetti. See LICENSE for
Copyright (c) 2012 Matt Aimonetti. See LICENSE for
further details.
2 changes: 1 addition & 1 deletion lib/documentation.rb
@@ -1,4 +1,4 @@
class WSDSL
class WeaselDiesel
# Service documentation class
#
# @api public
Expand Down
8 changes: 4 additions & 4 deletions lib/framework_ext/sinatra.rb
@@ -1,8 +1,8 @@
# Module used to extend {WSDSL} and add {#load_sinatra_route} to services.
# This code is Sinatra specific and therefore lives outside the {WSDSL}
# @see {WSDSL}
# Module used to extend {WeaselDiesel} and add {#load_sinatra_route} to services.
# This code is Sinatra specific and therefore lives outside the {WeaselDiesel}
# @see {WeaselDiesel}
# @api public
module WSDSLSinatraExtension
module WeaselDieselSinatraExtension

# Defines a sinatra service route based on its settings
#
Expand Down
4 changes: 2 additions & 2 deletions lib/framework_ext/sinatra_controller.rb
Expand Up @@ -19,7 +19,7 @@ class SinatraServiceController

class AuthenticationFailed < StandardError; end

# @return [WSDSL] The service served by this controller
# @return [WeaselDiesel] The service served by this controller
# @api public
attr_reader :service

Expand All @@ -46,7 +46,7 @@ class AuthenticationFailed < StandardError; end
attr_accessor :params

# @param [Sinatra::Application] app The Sinatra app used as a reference and to access request params
# @param [WSDSL] service The service served by this controller
# @param [WeaselDiesel] service The service served by this controller
# @raise [ParamError, NoParamsDefined, MissingParam, UnexpectedParam, InvalidParamType, InvalidParamValue]
# If the params don't validate one of the {ParamsVerification} errors will be raised.
# @api public
Expand Down
6 changes: 3 additions & 3 deletions lib/json_response_verification.rb
@@ -1,4 +1,4 @@
# include this module in WSDSL
# include this module in WeaselDiesel
# to add response verification methods.
#
module JSONResponseVerification
Expand Down Expand Up @@ -49,7 +49,7 @@ def validate_hash_against_template_node(hash, node, nested=false, errors=[], arr
end

subhash ||= hash
if node.is_a?(WSDSL::Response::Vector) && !array_item
if node.is_a?(WeaselDiesel::Response::Vector) && !array_item
errors << json_response_error(node, subhash, true) unless subhash.is_a?(Array)
subhash.each do |obj|
validate_hash_against_template_node(obj, node, true, errors, true)
Expand All @@ -72,7 +72,7 @@ def validate_hash_against_template_node(hash, node, nested=false, errors=[], arr
end

def json_response_error(el_or_attr, hash, type_error=false)
if el_or_attr.is_a?(WSDSL::Response::Element)
if el_or_attr.is_a?(WeaselDiesel::Response::Element)
"#{el_or_attr.name || 'top level'} Node/Object/Element is missing"
elsif type_error
error = "#{el_or_attr.name || el_or_attr.inspect} was of wrong type, expected #{el_or_attr.type}"
Expand Down

0 comments on commit 9f8c65a

Please sign in to comment.