webservice 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Manifest.txt +3 -3
- data/Rakefile +2 -2
- data/lib/webservice.rb +19 -1
- data/lib/webservice/base.rb +22 -31
- data/lib/webservice/version.rb +2 -2
- data/test/service/app.rb +15 -5
- data/test/test_app.rb +19 -3
- data/test/test_mustermann.rb +56 -0
- data/test/test_samples.rb +34 -0
- data/test/test_samples_debug.rb +20 -0
- metadata +19 -5
- data/lib/webservice/builder.rb +0 -32
- data/test/test_builder.rb +0 -34
- data/test/test_builder_debug.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca3b0a962413d171c662477e7626930cfbbc39f4
|
4
|
+
data.tar.gz: 437d9beda6547d451098a9cfdb8af7bcef051d08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ccadcc3b2d95a13fda4de2b9f9259a410231b7bfb6ac854f5552da3ce45f8dd15345e785d6f47b62032b3b4304f21f9ec219d9b624022cb61e541b2e69c1b23
|
7
|
+
data.tar.gz: 71d1d5ce9f811b32c498ddf7074f82efd9fa3cb42e45d1e5e6b246b9371033fe0509a443360fb80b415d381d098649d9c34cc5a7d470476f7f4c7ee166165c24
|
data/Manifest.txt
CHANGED
@@ -4,10 +4,10 @@ README.md
|
|
4
4
|
Rakefile
|
5
5
|
lib/webservice.rb
|
6
6
|
lib/webservice/base.rb
|
7
|
-
lib/webservice/builder.rb
|
8
7
|
lib/webservice/version.rb
|
9
8
|
test/helper.rb
|
10
9
|
test/service/app.rb
|
11
10
|
test/test_app.rb
|
12
|
-
test/
|
13
|
-
test/
|
11
|
+
test/test_mustermann.rb
|
12
|
+
test/test_samples.rb
|
13
|
+
test/test_samples_debug.rb
|
data/Rakefile
CHANGED
@@ -19,7 +19,8 @@ Hoe.spec 'webservice' do
|
|
19
19
|
|
20
20
|
self.extra_deps = [
|
21
21
|
['logutils'],
|
22
|
-
['
|
22
|
+
['mustermann', '>=2.0.0'],
|
23
|
+
['rack', '>=2.0.3']
|
23
24
|
]
|
24
25
|
|
25
26
|
self.licenses = ['Public Domain']
|
@@ -28,5 +29,4 @@ Hoe.spec 'webservice' do
|
|
28
29
|
required_ruby_version: '>= 2.3'
|
29
30
|
}
|
30
31
|
|
31
|
-
|
32
32
|
end
|
data/lib/webservice.rb
CHANGED
@@ -9,14 +9,32 @@ require 'pp'
|
|
9
9
|
|
10
10
|
require 'logutils'
|
11
11
|
|
12
|
+
require 'mustermann'
|
13
|
+
require 'mustermann/version'
|
14
|
+
require 'mustermann/sinatra'
|
15
|
+
|
12
16
|
require 'rack'
|
13
17
|
|
18
|
+
|
14
19
|
# our own code
|
15
20
|
require 'webservice/version' # note: let version always go first
|
16
|
-
require 'webservice/builder'
|
17
21
|
require 'webservice/base'
|
18
22
|
|
19
23
|
|
20
24
|
|
25
|
+
module Webservice
|
26
|
+
def self.load_file( path )
|
27
|
+
code = File.open( path, 'r:bom|utf-8' ).read
|
28
|
+
self.load( code )
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.load( code )
|
32
|
+
app_class = Class.new( Base ) ## create new app_class or just use Base itself - why? why not?
|
33
|
+
app_class.class_eval( code ) ## note: use class_eval (NOT instance_eval)
|
34
|
+
app_class
|
35
|
+
end
|
36
|
+
end # module Webservice
|
37
|
+
|
38
|
+
|
21
39
|
# say hello
|
22
40
|
puts Webservice.banner if defined?($RUBYLIBS_DEBUG) && $RUBYLIBS_DEBUG
|
data/lib/webservice/base.rb
CHANGED
@@ -4,49 +4,40 @@
|
|
4
4
|
module Webservice
|
5
5
|
|
6
6
|
class Base
|
7
|
+
|
8
|
+
HTTP_VERBS = %w(GET POST PATCH PUT DELETE HEAD OPTIONS)
|
9
|
+
|
10
|
+
|
7
11
|
class << self
|
8
12
|
|
9
13
|
## todo/check: all verbs needed! (supported) - why, why not??
|
10
|
-
|
14
|
+
HTTP_VERBS.each do |method|
|
11
15
|
define_method( method.downcase ) do |pattern, &block|
|
12
|
-
puts "[debug] Webservice::Base.#{method.downcase} - add route #{method} '#{pattern}' to
|
13
|
-
|
16
|
+
puts "[debug] Webservice::Base.#{method.downcase} - add route #{method} '#{pattern}' to #<#{self.name}:#{self.object_id}> : #{self.class.name}"
|
17
|
+
|
18
|
+
## note: for now use the sintatra-style patterns (with mustermann)
|
19
|
+
routes[method] << [Mustermann::Sinatra.new(pattern), block]
|
14
20
|
end
|
15
21
|
end
|
16
22
|
|
17
|
-
## def builder
|
18
|
-
## @builder ||= Rack::Builder.new
|
19
|
-
## end
|
20
23
|
|
21
24
|
def routes
|
22
25
|
@routes ||= Hash.new { |hash, key| hash[key]=[] }
|
23
26
|
end
|
24
27
|
|
25
28
|
|
29
|
+
## convenience method
|
26
30
|
def run!
|
27
|
-
|
28
|
-
|
29
|
-
app_obj = app_class.new
|
30
|
-
app = Rack::Builder.new
|
31
|
-
app.map( '/' ) { run app_obj } ## e.g. run App
|
31
|
+
puts "[debug] Webservice::Base.run! - self = #<#{self.name}:#{self.object_id}> : #{self.class.name}" # note: assumes self is class
|
32
|
+
app = self.new ## note: use self; will be derived class (e.g. App and not Base)
|
32
33
|
port = 4567
|
33
34
|
Rack::Handler.get('webrick').run( app, Port:port ) do |server|
|
34
35
|
## todo: add traps here - why, why not??
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
39
|
+
end ## class << self
|
38
40
|
|
39
|
-
private
|
40
|
-
|
41
|
-
def compile_pattern( pattern )
|
42
|
-
keys = []
|
43
|
-
pattern.gsub!( /(:\w+)/ ) do |match|
|
44
|
-
keys << $1[1..-1]
|
45
|
-
'([^/?#]+)'
|
46
|
-
end
|
47
|
-
[%r{^#{pattern}$}, keys]
|
48
|
-
end
|
49
|
-
end
|
50
41
|
|
51
42
|
|
52
43
|
attr_reader :request
|
@@ -68,9 +59,6 @@ class Base
|
|
68
59
|
@response.finish
|
69
60
|
end
|
70
61
|
|
71
|
-
def session
|
72
|
-
request.env["rack.session"] || raise("Rack::Session handler is missing")
|
73
|
-
end
|
74
62
|
|
75
63
|
def halt( *args )
|
76
64
|
response.status = args.detect{|arg| arg.is_a?(Fixnum) } || 200
|
@@ -84,11 +72,13 @@ private
|
|
84
72
|
|
85
73
|
def route_eval
|
86
74
|
catch(:halt) do
|
87
|
-
self.class.routes[request.request_method].each do |
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
75
|
+
self.class.routes[request.request_method].each do |pattern, block|
|
76
|
+
## puts "trying matching route >#{request.path_info}<..."
|
77
|
+
url_params = pattern.params( request.path_info )
|
78
|
+
if url_params ## note: params returns nil if no match
|
79
|
+
## puts " BINGO! url_params: #{url_params.inspect}"
|
80
|
+
if !url_params.empty? ## url_params hash NOT empty (e.g. {}) merge with req params
|
81
|
+
@params = url_params.merge( params )
|
92
82
|
end
|
93
83
|
handle_response( instance_eval( &block ))
|
94
84
|
return
|
@@ -98,8 +88,9 @@ private
|
|
98
88
|
end
|
99
89
|
end
|
100
90
|
|
91
|
+
|
101
92
|
def handle_response( obj )
|
102
|
-
puts "[Webservice::Base#handle_response] - obj: #{obj.class.name}"
|
93
|
+
puts "[Webservice::Base#handle_response] - obj : #{obj.class.name}"
|
103
94
|
|
104
95
|
### todo/fix: set content type to json
|
105
96
|
|
data/lib/webservice/version.rb
CHANGED
data/test/service/app.rb
CHANGED
@@ -3,15 +3,13 @@ get '/' do
|
|
3
3
|
"Hello World"
|
4
4
|
end
|
5
5
|
|
6
|
+
|
6
7
|
get '/hello/:name' do
|
7
8
|
"Hello #{params['name']}"
|
8
9
|
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
@name = params['name']
|
13
|
-
"#{@message} #{@name}"
|
14
|
-
end
|
11
|
+
|
12
|
+
## note: /halt/404 must be before /:message/:name !! otherwise no match!!!
|
15
13
|
|
16
14
|
get '/halt/404' do
|
17
15
|
halt 404 # 404 - not found
|
@@ -23,3 +21,15 @@ get '/halt_error' do
|
|
23
21
|
## todo: check why log reports 200-OK (for status code)!!
|
24
22
|
end
|
25
23
|
|
24
|
+
|
25
|
+
get '/:message/:name' do
|
26
|
+
message = params['message']
|
27
|
+
name = params['name']
|
28
|
+
"#{message} #{name}"
|
29
|
+
end
|
30
|
+
|
31
|
+
get '/:key(.:format)?' do
|
32
|
+
key = params['key']
|
33
|
+
fmt = params['format']
|
34
|
+
"#{key} #{fmt}"
|
35
|
+
end
|
data/test/test_app.rb
CHANGED
@@ -15,9 +15,9 @@ class TestApp < MiniTest::Test
|
|
15
15
|
def app
|
16
16
|
## return (rack-ready) app object
|
17
17
|
@@app ||= begin
|
18
|
-
|
19
|
-
pp
|
20
|
-
|
18
|
+
app_class = Webservice.load_file( "#{Webservice.root}/test/service/app.rb" )
|
19
|
+
pp app_class.routes
|
20
|
+
app_class.new
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -51,6 +51,22 @@ class TestApp < MiniTest::Test
|
|
51
51
|
assert_equal %q{"Hallo Welt"}, last_response.body
|
52
52
|
end
|
53
53
|
|
54
|
+
|
55
|
+
def test_format
|
56
|
+
get '/key.format'
|
57
|
+
assert last_response.ok?
|
58
|
+
assert_equal %q{"key format"}, last_response.body
|
59
|
+
|
60
|
+
get '/ottakringer.csv'
|
61
|
+
assert last_response.ok?
|
62
|
+
assert_equal %q{"ottakringer csv"}, last_response.body
|
63
|
+
|
64
|
+
get '/ottakringer'
|
65
|
+
assert last_response.ok?
|
66
|
+
assert_equal %q{"ottakringer "}, last_response.body
|
67
|
+
end
|
68
|
+
|
69
|
+
|
54
70
|
def test_halt
|
55
71
|
## get '/halt/404'
|
56
72
|
get '/halt/404'
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_mustermann.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestMustermann < MiniTest::Test
|
12
|
+
|
13
|
+
def test_hello
|
14
|
+
|
15
|
+
pp Rack::VERSION
|
16
|
+
pp Mustermann::VERSION
|
17
|
+
|
18
|
+
pattern = Mustermann::Sinatra.new( '/:name' )
|
19
|
+
pp pattern
|
20
|
+
pp pattern.names
|
21
|
+
|
22
|
+
m = pattern.match( "/test" )
|
23
|
+
pp m
|
24
|
+
pp m.captures
|
25
|
+
params = pattern.params( "/test" )
|
26
|
+
pp params
|
27
|
+
|
28
|
+
|
29
|
+
m = pattern.match( "/test/test" )
|
30
|
+
pp m
|
31
|
+
params = pattern.params( "/test/test" )
|
32
|
+
pp params
|
33
|
+
|
34
|
+
splat = Mustermann::Sinatra.new( '/:prefix/*.*' )
|
35
|
+
params = splat.params('/a/b.c') # => { "prefix" => "a", splat => ["b", "c"] }
|
36
|
+
pp params
|
37
|
+
|
38
|
+
fmt = Mustermann::Sinatra.new( '/:name(.:format)?' )
|
39
|
+
params = fmt.params( '/hello.json' )
|
40
|
+
pp params
|
41
|
+
params = fmt.params( '/hello' )
|
42
|
+
pp params
|
43
|
+
|
44
|
+
rnd = Mustermann::Sinatra.new( '/beer/(r|rnd|random)' )
|
45
|
+
params = rnd.params( '/beer/rnd' )
|
46
|
+
pp params
|
47
|
+
params = rnd.params( '/beer/ottakringer' )
|
48
|
+
pp params
|
49
|
+
params = rnd.params( '/test/beer/ottakringer' )
|
50
|
+
pp params
|
51
|
+
|
52
|
+
|
53
|
+
assert true # if we get here - test success
|
54
|
+
end
|
55
|
+
|
56
|
+
end # class TestMustermann
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_samples.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestSamples < MiniTest::Test
|
12
|
+
|
13
|
+
def test_beer
|
14
|
+
app_class = Webservice.load_file( "#{Webservice.root}/samples/beer.rb" )
|
15
|
+
pp app_class.routes
|
16
|
+
|
17
|
+
assert true # if we get here - test success
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_football
|
21
|
+
app_class = Webservice.load_file( "#{Webservice.root}/samples/football.rb" )
|
22
|
+
pp app_class.routes
|
23
|
+
|
24
|
+
assert true # if we get here - test success
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_world
|
28
|
+
app_class = Webservice.load_file( "#{Webservice.root}/samples/world.rb" )
|
29
|
+
pp app_class.routes
|
30
|
+
|
31
|
+
assert true # if we get here - test success
|
32
|
+
end
|
33
|
+
|
34
|
+
end # class TestSamples
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
###
|
4
|
+
# to run use
|
5
|
+
# ruby -I ./lib -I ./test test/test_samples_debug.rb
|
6
|
+
|
7
|
+
|
8
|
+
require 'helper'
|
9
|
+
|
10
|
+
|
11
|
+
class TestSamplesDebug < MiniTest::Test
|
12
|
+
|
13
|
+
def test_debug
|
14
|
+
app_class = Webservice.load_file( "#{Webservice.root}/samples/debug.rb" )
|
15
|
+
pp app_class.routes
|
16
|
+
|
17
|
+
assert true # if we get here - test success
|
18
|
+
end
|
19
|
+
|
20
|
+
end # class TestSamplesDebug
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webservice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logutils
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mustermann
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.0
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rack
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,13 +95,13 @@ files:
|
|
81
95
|
- Rakefile
|
82
96
|
- lib/webservice.rb
|
83
97
|
- lib/webservice/base.rb
|
84
|
-
- lib/webservice/builder.rb
|
85
98
|
- lib/webservice/version.rb
|
86
99
|
- test/helper.rb
|
87
100
|
- test/service/app.rb
|
88
101
|
- test/test_app.rb
|
89
|
-
- test/
|
90
|
-
- test/
|
102
|
+
- test/test_mustermann.rb
|
103
|
+
- test/test_samples.rb
|
104
|
+
- test/test_samples_debug.rb
|
91
105
|
homepage: https://github.com/rubylibs/webservice
|
92
106
|
licenses:
|
93
107
|
- Public Domain
|
data/lib/webservice/builder.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Webservice
|
4
|
-
|
5
|
-
class Builder
|
6
|
-
|
7
|
-
def self.load_file( path)
|
8
|
-
code = File.open( path, 'r:bom|utf-8' ).read
|
9
|
-
self.load( code )
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.load( code )
|
13
|
-
app_class = Class.new( Base )
|
14
|
-
app_class.class_eval( code ) ## note: use class_eval (NOT instance_eval)
|
15
|
-
|
16
|
-
builder = Builder.new
|
17
|
-
builder.app_class = app_class
|
18
|
-
builder
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
include LogUtils::Logging
|
23
|
-
|
24
|
-
attr_accessor :app_class
|
25
|
-
|
26
|
-
def initialize
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
end # class Builder
|
31
|
-
|
32
|
-
end # module Webservice
|
data/test/test_builder.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
###
|
4
|
-
# to run use
|
5
|
-
# ruby -I ./lib -I ./test test/test_builder.rb
|
6
|
-
|
7
|
-
|
8
|
-
require 'helper'
|
9
|
-
|
10
|
-
|
11
|
-
class TestBuilder < MiniTest::Test
|
12
|
-
|
13
|
-
def test_beer_builder
|
14
|
-
builder = Webservice::Builder.load_file( "#{Webservice.root}/examples/beer.rb" )
|
15
|
-
pp builder.app_class.routes
|
16
|
-
|
17
|
-
assert true # if we get here - test success
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_football_builder
|
21
|
-
builder = Webservice::Builder.load_file( "#{Webservice.root}/examples/football.rb" )
|
22
|
-
pp builder.app_class.routes
|
23
|
-
|
24
|
-
assert true # if we get here - test success
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_world_builder
|
28
|
-
builder = Webservice::Builder.load_file( "#{Webservice.root}/examples/world.rb" )
|
29
|
-
pp builder.app_class.routes
|
30
|
-
|
31
|
-
assert true # if we get here - test success
|
32
|
-
end
|
33
|
-
|
34
|
-
end # class TestBuilder
|
data/test/test_builder_debug.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
###
|
4
|
-
# to run use
|
5
|
-
# ruby -I ./lib -I ./test test/test_builder_debug.rb
|
6
|
-
|
7
|
-
|
8
|
-
require 'helper'
|
9
|
-
|
10
|
-
|
11
|
-
class TestBuilderDebug < MiniTest::Test
|
12
|
-
|
13
|
-
def test_builder
|
14
|
-
builder = Webservice::Builder.load_file( "#{Webservice.root}/examples/debug.rb" )
|
15
|
-
pp builder.app_class.routes
|
16
|
-
|
17
|
-
assert true # if we get here - test success
|
18
|
-
end
|
19
|
-
|
20
|
-
end # class TestBuilderDebug
|