Permalink
Browse files

renamed params to args, fixed rcov task, added automatic sinatra test…

… server start/stop in specs
  • Loading branch information...
1 parent 5a1fb48 commit 05260f94c83363a7e454707739977680688bcc30 @schorsch schorsch committed May 8, 2010
View
@@ -4,8 +4,8 @@ require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
- gem.name = "sk-hoth"
- gem.summary = %Q{Registry and deployment description abstraction for SOA-Services. Tweaked by SalesKing}
+ gem.name = "king_soa"
+ gem.summary = %Q{Registry and deployment description abstraction for SOA-Services by SalesKing}
gem.description = <<-DESCRIPTION
Creating a SOA requires a centralized location to define all services within the
SOA. Furthermore you want to know where to deploy those services.
@@ -15,13 +15,12 @@ DESCRIPTION
gem.authors = ['Georg Leciejewski']
gem.files = FileList["[A-Z]*.*", "{lib,spec}/**/*"]
- gem.add_dependency "curb"
+ gem.add_dependency "typhoeus"
gem.add_dependency "json"
- gem.add_dependency "king_hmac"
gem.add_development_dependency "rspec"
# gem.add_development_dependency "rack/test"
- gem.add_development_dependency "webmock"
+# gem.add_development_dependency "webmock"
end
Jeweler::GemcutterTasks.new
rescue LoadError
@@ -35,9 +34,11 @@ Spec::Rake::SpecTask.new(:spec) do |spec|
end
Spec::Rake::SpecTask.new(:rcov) do |spec|
- spec.libs << 'lib' << 'spec'
- spec.pattern = 'spec/**/*_spec.rb'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
spec.rcov = true
+ spec.rcov_opts = ['--exclude', 'spec']
+ spec.rcov_opts << '--sort coverage'
+ spec.rcov_opts << '--sort-reverse '
end
task :spec => :check_dependencies
@@ -4,7 +4,7 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- s.name = %q{sk-hoth}
+ s.name = %q{king_soa}
s.version = "0.0.1"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
@@ -34,7 +34,7 @@ SOA. Furthermore you want to know where to deploy those services.
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6}
- s.summary = %q{Registry and deployment description abstraction for SOA-Services. Tweaked by SalesKing}
+ s.summary = %q{Registry and deployment description abstraction for SOA-Services by SalesKing}
s.test_files = [
"spec/spec_helper.rb",
"spec/king_soa/registry_spec.rb",
@@ -48,24 +48,18 @@ SOA. Furthermore you want to know where to deploy those services.
s.specification_version = 3
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<curb>, [">= 0"])
+ s.add_runtime_dependency(%q<typhoeus>, [">= 0"])
s.add_runtime_dependency(%q<json>, [">= 0"])
- s.add_runtime_dependency(%q<king_hmac>, [">= 0"])
s.add_development_dependency(%q<rspec>, [">= 0"])
- s.add_development_dependency(%q<webmock>, [">= 0"])
else
- s.add_dependency(%q<curb>, [">= 0"])
+ s.add_dependency(%q<typhoeus>, [">= 0"])
s.add_dependency(%q<json>, [">= 0"])
- s.add_dependency(%q<king_hmac>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 0"])
- s.add_dependency(%q<webmock>, [">= 0"])
end
else
- s.add_dependency(%q<curb>, [">= 0"])
+ s.add_dependency(%q<typhoeus>, [">= 0"])
s.add_dependency(%q<json>, [">= 0"])
- s.add_dependency(%q<king_hmac>, [">= 0"])
s.add_dependency(%q<rspec>, [">= 0"])
- s.add_dependency(%q<webmock>, [">= 0"])
end
end
View
@@ -21,7 +21,8 @@
#
#
# after defining your services you can call each of them with
-# <tt>Hoth::Services.service_name(params)</tt>
+#
+# <tt>KingSoa.service_name(args)</tt>
#
# KingSoa.sign_document(counter)
# current_number = Hoth::Services.value_of_counter(counter)
@@ -32,12 +33,11 @@ module KingSoa
class << self
def init_from_hash(services)
- # create service
-
+ # create service
end
# Locate service by a given name
- # ==== Params
+ # ==== Parameter
# service<String|Symbol>:: the name to lookup
def find(service)
Registry[service]
@@ -46,7 +46,7 @@ def find(service)
# this is where the services get called
def method_missing(meth, *args, &blk) # :nodoc:
if service = Registry[meth]
- service.execute(*args)
+ service.perform(*args)
else
super
end
@@ -11,19 +11,21 @@ def call(env)
if env["PATH_INFO"] =~ /^\/soa/
begin
req = Rack::Request.new(env)
- # find service
+ # find service TODO rescue service not found
service = KingSoa.find(req.params["name"])
# authenticate
- authenticated?(service, req.params["auth_key"])
- # perform method with decoded params
- result = service.perform(*service.decode( req.params["params"] ))
+ authenticated?(service, req.params["auth"])
+ # perform method with decoded params
+ result = service.perform(*service.decode( req.params["args"] ))
# encode result
encoded_result = service.encode({"result" => result})
# and return
- [ 200,
- {'Content-Type' => 'application/json',
- 'Content-Length' => "#{encoded_result.length}"},
- [encoded_result] ]
+ [
+ 200,
+ {'Content-Type' => 'application/json', 'Content-Length' => "#{encoded_result.length}"},
+ [encoded_result]
+ ]
+
rescue Exception => e
#Hoth::Logger.debug "e: #{e.message}"
if service
@@ -39,8 +41,9 @@ def call(env)
end
end
+ # TODO raise and rescue specific error
def authenticated?(service, key)
- raise "Please provide a valid authentication key" unless service.auth_key == key
+ raise "Please provide a valid authentication key" unless service.auth == key
end
end
View
@@ -1,14 +1,14 @@
module KingSoa
class Service
# endpoint url
- attr_accessor :debug, :name, :auth_key, :queue
+ attr_accessor :debug, :name, :auth, :queue
attr_reader :request
def initialize(opts)
self.name = opts[:name].to_sym
self.url = opts[:url] if opts[:url]
self.queue = opts[:queue] if opts[:queue]
- self.auth_key = opts[:auth_key] if opts[:auth_key]
+ self.auth = opts[:auth] if opts[:auth]
end
def call_remote(*args)
@@ -59,17 +59,20 @@ def url=(url)
@url = "#{url}/soa"
end
- # The params for each soa request consisnt of two values:
- # name => the name of the method to call
- # params => the parameters for the method
+ # The params for each soa request consist of following values:
+ # name => the name of the method to call
+ # args => the arguments for the soa class method
+ # auth => an authentication key. something like a api key or pass. To make
+ # it really secure you MUST use https or do not expose your soa endpoints
+ #
# ==== Parameter
- # params<Hash|Array|String>:: will be json encoded
+ # payload<Hash|Array|String>:: will be json encoded
# === Returns
# <Hash{String=>String}>:: params added to the POST body
def params(payload)
- { 'name' => name.to_s,
- 'params' => encode(payload),
- 'auth_key'=> auth_key }
+ { 'name' => name.to_s,
+ 'args' => encode(payload),
+ 'auth' => auth }
end
def encode(string)
@@ -8,7 +8,7 @@
KingSoa::Registry << @service
end
- it "should be able to handle exceptions" do
+ it "should handle exceptions" do
app = stub("ApplicationStub").as_null_object
middleware = KingSoa::Rack::Middleware.new(app)
env = {"PATH_INFO" => "/soa", "name" => 'a_method'}
@@ -19,18 +19,14 @@
rack_response.last.first.should == "{\"error\":\"An error occurred! (Missing rack.input)\"}"
end
- xit "says hello" do
+ it "should handle result" do
app = stub("ApplicationStub").as_null_object
- middleware = Hoth::Providers::RackProvider.new(app)
+ middleware = KingSoa::Rack::Middleware.new(app)
+ env = {"PATH_INFO" => "/soa", "name" => 'a_method'}
- get '/soa', :name=>'a_method', :params=> "#{{:number=>1}.to_json}"
- last_response.should == 'ads'#be_ok
- last_response.body.should == 'Hello World'
+ rack_response = middleware.call env
+ rack_response.first.should == 500 #status code
+ rack_response.last.should be_a_kind_of(Array)
+ rack_response.last.first.should == "{\"error\":\"An error occurred! (Missing rack.input)\"}"
end
-
-# def app
-# dummy_app = lambda { |env| puts "in dummy"; [200, {}, ""] }
-# KingSoa::Rack::Middleware.new(dummy_app)
-# end
-
end
@@ -2,26 +2,23 @@
describe KingSoa::Registry do
before(:each) do
-
+ @reg = KingSoa::Registry
end
it "should return empty services" do
- reg = KingSoa::Registry.new
- reg.services.should == []
+ @reg.services.should == []
end
- it "should add service" do
- reg = KingSoa::Registry.new
- s = KingSoa::Service.new(:url=>'http://localhost')
- reg << s
- reg.services.should == [s]
+ it "should add service" do
+ s = KingSoa::Service.new(:name=>'process_documents')
+ @reg << s
+ @reg.services.should == [s]
end
it "should return a service by name" do
- reg = KingSoa::Registry.new
s = KingSoa::Service.new(:name=>:save_document, :url=>'http://localhost')
- reg << s
- reg[:save_document].should == s
+ @reg << s
+ @reg[:save_document].should == s
end
@@ -13,34 +13,32 @@
it "should call service" do
s = KingSoa::Service.new(:name=>:local_soa_class)
- res = s.perform(1,2,3)
- res.should == [1,2,3]
+ s.perform(1,2,3).should == [1,2,3]
end
end
-# needs the local testserver !!!
-# ruby spec/server/app
describe KingSoa::Service, 'remote request' do
+ before :all do
+ start_test_server
+ end
+
+ after :all do
+ stop_test_server
+ end
+
it "should call a service remote" do
- s = KingSoa::Service.new(:name=>:soa_test_service, :url=>'http://localhost:4567', :auth_key=>'12345')
+ s = KingSoa::Service.new(:name=>:soa_test_service, :url=>test_url, :auth=>'12345')
s.perform(1,2,3).should == [1,2,3]
end
it "should call a service remote and return auth error" do
- s = KingSoa::Service.new(:name=>:soa_test_service, :url=>'http://localhost:4567', :auth_key=>'wrong')
+ s = KingSoa::Service.new(:name=>:soa_test_service, :url=>test_url, :auth=>'wrong')
s.perform(1,2,3).should == "Please provide a valid authentication key"
end
it "should call a service remote and return auth error" do
- s = KingSoa::Service.new(:name=>:wrong_service, :url=>'http://localhost:4567', :auth_key=>'12345')
- s.perform(1,2,3).should == "An error occurred! (undefined method `auth_key' for nil:NilClass)"
- end
-end
-
-
-class LocalSoaClass
- def self.perform(param1, param2, param3)
- return [param1, param2, param3]
+ s = KingSoa::Service.new(:name=>:wrong_service, :url=>test_url, :auth=>'12345')
+ s.perform(1,2,3).should == "An error occurred! (undefined method `auth' for nil:NilClass)"
end
end
View
@@ -1,26 +1,29 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
-require "#{File.dirname(__FILE__)}/../../lib/king_soa"
+# change since we want the files in here not from installed gem
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
+require "king_soa"
+# this grabs all /soa requests
use KingSoa::Rack::Middleware
+# method to kill this server instance
+get '/die' do
+ exit!
+end
-###################################################
-# method to kill this server instance
-#'/die'
#######################################
-# Somewhere in you app
+# Somewhere in you app define services
#
# setup test registry
-service = KingSoa::Service.new(:name=>'soa_test_service', :auth_key=>'12345')
+service = KingSoa::Service.new(:name=>'soa_test_service', :auth => '12345')
KingSoa::Registry << service
-# the class beeing called localy
+# the local soa class beeing called
class SoaTestService
-
def self.perform(param1, param2, param3)
return [param1, param2, param3]
end
-
end
Oops, something went wrong.

0 comments on commit 05260f9

Please sign in to comment.