Sha256: 6c56dcd7d91c2b2d7906e07866998952ae9faf575d766cbf72afe2af536aadbe

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

#!/usr/bin/env ruby

# gem install em-synchrony
# gem install mysql2

# create database goliath_test
# create user 'goliath'@'localhost' identified by 'goliath'
# grant all on goliath_test.* to 'goliath'@'localhost'
# create table users (id int not null auto_increment primary key, name varchar(255), email varchar(255));
# insert into users (name, email) values ('dan', 'dj2@everyburning.com'), ('Ilya', 'ilya@igvita.com');

# To start server
# ruby ./srv.rb
#
# Example output:
# curl http://localhost:9000/?id=1
#=> "{\"user\":{\"email\":\"dj2@everyburning.com\",\"id\":1,\"name\":\"dan\"}}"
# curl http://localhost:9000/?id=2
#=> "{\"user\":{\"email\":\"ilya@igvita.com\",\"id\":2,\"name\":\"Ilya\"}}"

$: << "../../lib" << "./lib"

require 'goliath'
require 'em-synchrony/activerecord'

require 'yajl' if RUBY_PLATFORM != 'java'

class User < ActiveRecord::Base
end

class Srv < Goliath::API
  use Goliath::Rack::Params
  use Goliath::Rack::DefaultMimeType
  use Goliath::Rack::Render, 'json'

  use Goliath::Rack::Validation::RequiredParam, {:key => 'id', :type => 'ID'}
  use Goliath::Rack::Validation::NumericRange, {:key => 'id', :min => 1}

  def response(env)
    [200, {}, User.find(params['id']).to_json]
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
goliath-1.0.7 examples/activerecord/srv.rb
goliath-1.0.6 examples/activerecord/srv.rb
goliath-1.0.5 examples/activerecord/srv.rb
goliath-1.0.4 examples/activerecord/srv.rb
goliath-1.0.3 examples/activerecord/srv.rb
goliath-1.0.2 examples/activerecord/srv.rb
goliath-1.0.1 examples/activerecord/srv.rb
goliath-1.0.0 examples/activerecord/srv.rb
goliath-1.0.0.beta.1 examples/activerecord/srv.rb