https://github.com/rubycocos/webservice
webservice gem - yet another HTTP JSON API (web service) builder
https://github.com/rubycocos/webservice
gem ruby
Last synced: 11 months ago
JSON representation
webservice gem - yet another HTTP JSON API (web service) builder
- Host: GitHub
- URL: https://github.com/rubycocos/webservice
- Owner: rubycocos
- Created: 2015-04-19T19:42:09.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2022-04-05T07:42:12.000Z (about 4 years ago)
- Last Synced: 2024-10-31T13:57:01.622Z (over 1 year ago)
- Topics: gem, ruby
- Language: Ruby
- Homepage:
- Size: 46.9 KB
- Stars: 8
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
Awesome Lists containing this project
README
# webservice
webservice gem - yet another HTTP JSON API (web service) builder
* home :: [github.com/rubylibs/webservice](https://github.com/rubylibs/webservice)
* bugs :: [github.com/rubylibs/webservice/issues](https://github.com/rubylibs/webservice/issues)
* gem :: [rubygems.org/gems/webservice](https://rubygems.org/gems/webservice)
* rdoc :: [rubydoc.info/gems/webservice](http://rubydoc.info/gems/webservice)
## Usage
[Dynamic Example](#dynamic-example) •
[Classic Example](#classic-example) •
[Rackup Example](#rackup-example)
### Dynamic Example
You can load services at-runtime from files using `Webservice.load_file`.
Example:
```ruby
# service.rb
get '/' do
'Hello, world!'
end
```
and
```ruby
# server.rb
require 'webservice'
App = Webservice.load_file( './service.rb' )
App.run!
```
and to run type
```
$ ruby ./server.rb
```
### Classic Example
```ruby
# server.rb
require 'webservice'
class App < Webservice::Base
get '/' do
'Hello, world!'
end
end
App.run!
```
and to run type
```
$ ruby ./server.rb
```
### Rackup Example
Use `config.ru` and `rackup`. Example:
```ruby
# config.ru
require `webservice`
class App < Webservice::Base
get '/' do
'Hello, world!'
end
end
run App
```
and to run type
```
$ rackup # will (auto-)load config.ru
```
Note: `config.ru` is a shortcut (inline)
version of `Rack::Builder.new do ... end`:
```ruby
# server.rb
require 'webservice'
class App < Webservice::Base
get '/' do
'Hello, world!'
end
end
builder = Rack::Builder.new do
run App
end
Rack::Server.start builder.to_app
```
and to run type
```
$ ruby ./server.rb
```
## "Real World" Examples
See
[**`beerkit / beer.db.service`**](https://github.com/beerkit/beer.db.service) -
beer.db HTTP JSON API (web service) scripts e.g.
```ruby
get '/beer/(r|rnd|rand|random)' do # special keys for random beer
Beer.rnd
end
get '/beer/:key'
Beer.find_by! key: params['key']
end
get '/brewery/(r|rnd|rand|random)' do # special keys for random brewery
Brewery.rnd
end
get '/brewery/:key'
Brewery.find_by! key: params['key']
end
...
```
[**`worlddb / world.db.service`**](https://github.com/worlddb/world.db.service) -
world.db HTTP JSON API (web service) scripts
```ruby
get '/countries(.:format)?' do
Country.by_key.all # sort/order by key
end
get '/cities(.:format)?' do
City.by_key.all # sort/order by key
end
get '/tag/:slug(.:format)?' do # e.g. /tag/north_america.csv
Tag.find_by!( slug: params['slug'] ).countries
end
...
```
[**`sportdb / sport.db.service`**](https://github.com/sportdb/sport.db.service) -
sport.db (football.db) HTTP JSON API (web service) scripts
## License
The `webservice` scripts are dedicated to the public domain.
Use it as you please with no restrictions whatsoever.