Last active
August 29, 2015 13:56
-
-
Save laser/9234559 to your computer and use it in GitHub Desktop.
prez
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'barrister-redis' | |
require './service.rb' | |
container = Barrister::RedisContainer.new('./user_service.idl', | |
[HandlerA.new, HandlerB.new], | |
database_url: ENV['OPENREDIS_URL'], | |
list_name: 'user_service') | |
container.start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# services/user_service/app.rb | |
require 'barrister-sinatra' | |
require './service.rb' | |
container = Barrister::SinatraContainer.new('./user_service.idl', | |
[HandlerA.new, HandlerB.new], | |
mount_path: '/user_service', | |
port: 5555, | |
host: 'localhost') | |
container.start | |
# defaults only | |
container = Barrister::RedisContainer.new './user_service.idl', UserService.new | |
container.start |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
├── services | |
│ └── user_service | |
│ ├── container.rb | |
│ ├── models | |
│ │ └── user.rb | |
│ ├── user_service.rb | |
│ ├── user_service.html | |
│ ├── user_service.idl | |
│ └── user_service.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bundle exec rails s -p $PORT 2>&1 & | |
cd services/user_service; bundle exec ruby ./container.rb -p 3001 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import barrister | |
import logging | |
import os | |
from functools import wraps | |
from utilities import dict_get | |
API_URI = dict_get(os.environ, 'API_URI') | |
# disable debug logging | |
logging.getLogger("barrister").setLevel(logging.INFO) | |
# change this to point at wherever your server is running | |
trans = barrister.HttpTransport(API_URI) | |
# automatically connects to endpoint and loads IDL JSON contract | |
client = barrister.Client(trans) | |
svc = client.UserService | |
class TestUserManagement: | |
def testCreateUser(self): | |
# creating a user | |
user = svc.create_user({ "email": "[email protected]", "full_name": "Erin", "phone_number": "2069991212" }) | |
assert user["email"] == "[email protected]" | |
def testListUsers(self): | |
# listing all users | |
users = svc.get_all_users() | |
assert len(users) == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
after_create :notify_added | |
validates :email, uniqueness: true | |
private | |
def notify_added | |
UserMailer.notify_added(self).deliver | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct UserProperties { | |
email string | |
full_name string | |
phone_number string | |
} | |
struct User extends UserProperties { | |
id int | |
} | |
interface UserService { | |
create_user(properties UserProperties) User | |
get_all_users() []User | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record' | |
require './models/user.rb' | |
class UserService | |
USER_ATTRIBUTES = %w(id full_name email phone_number) | |
def initialize(opts={}) | |
ActiveRecord::Base.establish_connection(opts[:db_config]) | |
end | |
def get_all_users() | |
User.all.map &method(:to_serializable) | |
end | |
def create_user(user_properties) | |
to_serializable(User.create!(user_properties)) | |
end | |
private | |
def to_serializable(user_model) | |
user_model.serializable_hash.slice *USER_ATTRIBUTES | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserService | |
def self.instance | |
@@instance ||= create | |
@@instance.UserService | |
end | |
def self.create | |
Barrister::Rails::Client.new 'http://localhost:3001/user_service' | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/user_service_client.rb | |
class UserServiceClient | |
def self.instance | |
@@instance ||= create | |
@@instance.UserService | |
end | |
def self.create | |
Barrister::Rails::Client.new UserService.new, './lib/services/user_service/user_service.idl' | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserService | |
def self.instance | |
@@instance ||= create | |
@@instance.UserService | |
end | |
def self.create | |
redis_transport = Barrister::RedisTransport.new 'redis://localhost:6379', 'foo_bar' | |
Barrister::Rails::Client.new redis_transport | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
skip_before_action :verify_authenticity_token | |
def index | |
@users = UserServiceClient.instance.get_all_users | |
end | |
def new | |
end | |
def create | |
begin | |
user = UserServiceClient.instance.create_user user_params | |
flash[:notice] = "Successfully created user with name #{user.full_name}" | |
rescue Barrister::RpcException => e | |
flash[:notice] = "Error creating user: #{e.message}" | |
end | |
redirect_to action: 'index' | |
end | |
private | |
def user_params | |
params.require(:user).permit(:full_name, :phone_number, :email) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
def index | |
@users = User.all | |
end | |
def new() end | |
def create | |
begin | |
user = User.new(user_params) | |
user.save! | |
flash[:notice] = "Successfully created user with name #{user.full_name}" | |
rescue ActiveRecord::RecordInvalid => e | |
flash[:notice] = "Error creating user: #{e.message}" | |
end | |
redirect_to action: 'index' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment