rddd 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem 'sinatra'
4
- gem 'rddd', :path => "~/development/rddd"
4
+ gem 'rddd', :path => "../../../"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ~/development/rddd
3
3
  specs:
4
- rddd (0.2.4)
4
+ rddd (0.3.0)
5
5
  curb
6
6
 
7
7
  GEM
@@ -2,4 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  gem 'sinatra'
4
4
  gem 'sinatra-contrib'
5
- gem 'rddd', :path => "~/development/rddd"
5
+ gem 'rddd', :path => "../../../"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ~/development/rddd
3
3
  specs:
4
- rddd (0.2.4)
4
+ rddd (0.3.0)
5
5
  curb
6
6
 
7
7
  GEM
@@ -9,7 +9,7 @@ require 'rddd/services/service_bus'
9
9
  require 'services/list_service'
10
10
 
11
11
  Rddd.configure do |config|
12
- config.service_creator = lambda do |name|
12
+ config.service_factory_strategy = lambda do |name|
13
13
  class_name = "#{name.to_s.camel_case}Service"
14
14
  puts class_name
15
15
  Object.const_get(class_name.to_sym)
@@ -8,9 +8,9 @@ module Rddd
8
8
  class Configuration
9
9
  include Singleton
10
10
 
11
- attr_accessor :service_creator,
12
- :repository_creator,
13
- :caching_strategy,
11
+ attr_accessor :service_factory_strategy,
12
+ :repository_creator,
13
+ :caching_strategy,
14
14
  :remote_services
15
15
  end
16
16
  end
@@ -1,6 +1,7 @@
1
1
  require 'json'
2
2
  require 'curl'
3
3
  require 'rddd/services/service'
4
+ require 'rddd/services/transports/factory'
4
5
 
5
6
  module Rddd
6
7
  module Services
@@ -37,24 +38,23 @@ module Rddd
37
38
  # for any change in your client or services code base.
38
39
  #
39
40
  class RemoteService < Service
40
- def initialize(url, attributes = {})
41
+ def initialize(transport, service_name, attributes)
41
42
  super(attributes)
42
43
 
43
- @url = url
44
+ @transport = transport
45
+ @service_name = service_name
44
46
  end
45
47
 
46
48
  def execute
47
- JSON.parse(Curl.post(@url, JSON.unparse(@attributes)) do |http|
48
- http.headers['Content-Type'] = 'application/json'
49
- end.body_str)
49
+ @transport.call(@service_name, @attributes)
50
50
  end
51
51
 
52
- def self.build(namespace, service_name, attributes)
53
- remote = Configuration.instance.remote_services.find do |item|
54
- item[:namespace] == namespace
55
- end
56
-
57
- RemoteService.new("#{remote[:endpoint]}#{service_name}", attributes)
52
+ def self.build(namespace, service_name, attributes = {})
53
+ RemoteService.new(
54
+ Transports::Factory.build(namespace),
55
+ service_name,
56
+ attributes
57
+ )
58
58
  end
59
59
  end
60
60
  end
@@ -3,17 +3,17 @@ require 'rddd/configuration'
3
3
  module Rddd
4
4
  module Services
5
5
  class ServiceFactory
6
- CreatorNotGiven = Class.new(RuntimeError)
6
+ StrategyNotGiven = Class.new(RuntimeError)
7
7
 
8
8
  InvalidService = Class.new(RuntimeError)
9
9
 
10
10
  def self.build(name, attributes)
11
- creator = Configuration.instance.service_creator
11
+ strategy = Configuration.instance.service_factory_strategy
12
12
 
13
- raise CreatorNotGiven unless creator
13
+ raise StrategyNotGiven unless strategy
14
14
 
15
15
  begin
16
- creator.call(name).new(attributes)
16
+ strategy.call(name).new(attributes)
17
17
  rescue
18
18
  raise Rddd::Services::ServiceFactory::InvalidService
19
19
  end
@@ -0,0 +1,22 @@
1
+ require 'json'
2
+ require 'curl'
3
+ require 'rddd/services/service'
4
+ require 'rddd/services/transports/http_json'
5
+
6
+ module Rddd
7
+ module Services
8
+ module Transports
9
+ class Factory
10
+ def self.build(namespace)
11
+ remote = Configuration.instance.remote_services.find do |remote|
12
+ remote[:namespace] == namespace
13
+ end
14
+
15
+ Transports::HttpJson.new(
16
+ :endpoint => remote[:endpoint]
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ module Rddd
2
+ module Services
3
+ module Transports
4
+ class HttpJson
5
+ def initialize(attributes)
6
+ @endpoint = attributes[:endpoint]
7
+ end
8
+
9
+ def call(service_name, attributes)
10
+ JSON.parse(Curl.post("#{@endpoint}#{service_name}", JSON.unparse(attributes)) do |http|
11
+ http.headers['Content-Type'] = 'application/json'
12
+ end.body_str)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Rddd
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -43,7 +43,7 @@ describe 'CreateProject' do
43
43
  Rddd.configure do |config|
44
44
  config.repository_creator = repository_creator
45
45
 
46
- config.service_creator = lambda do |name|
46
+ config.service_factory_strategy = lambda do |name|
47
47
  class_name = "#{name.to_s.camel_case}Service"
48
48
  Object.const_get(class_name.to_sym)
49
49
  end
@@ -6,32 +6,36 @@ module Rddd
6
6
  describe RemoteService do
7
7
  let(:attributes) { {:foo => 'bar'} }
8
8
 
9
- let(:url) { 'http://remote.dev/' }
9
+ let(:endpoint) { 'http://remote.dev/' }
10
10
 
11
- describe '#initialize' do
12
- subject { RemoteService.new(url, attributes) }
11
+ let(:namespace) { 'namespace' }
13
12
 
14
- it 'should store attributes' do
15
- subject.instance_variable_get(:@attributes).should == attributes
16
- end
17
- end
13
+ let(:service_name) { :service_name }
14
+
15
+ before do
16
+ Configuration.instance.remote_services = [
17
+ {:namespace => namespace, :endpoint => endpoint}
18
+ ]
19
+ end
18
20
 
19
21
  describe '#valid?' do
20
- subject { RemoteService.new(url).valid? }
22
+ subject { RemoteService.build(namespace, service_name).valid? }
21
23
 
22
24
  it { should be_true }
23
25
  end
24
26
 
25
27
  describe '#execute' do
26
- subject { RemoteService.new(url, attributes).execute }
28
+ subject { RemoteService.build(namespace, service_name, attributes).execute }
27
29
 
28
- let(:curl) { stub('curl', :body_str => '{"foo": "bar"}', :headers => [])}
30
+ let(:result) { {"foo" => "bar"} }
29
31
 
30
- it 'should raise not implemented' do
31
- Curl.expects(:post).with(url, JSON.unparse(attributes)).yields(curl).returns(curl)
32
- curl.headers.expects(:[]=).with('Content-Type', 'application/json')
32
+ let(:transport) { stub('transport') }
33
33
 
34
- subject.should == {'foo' => 'bar'}
34
+ it 'should raise not implemented' do
35
+ Transports::HttpJson.expects(:new).with(:endpoint => endpoint).returns(transport)
36
+ transport.expects(:call).with(service_name, attributes).returns(result)
37
+
38
+ subject.should == result
35
39
  end
36
40
  end
37
41
  end
@@ -6,7 +6,7 @@ module Rddd
6
6
  describe ServiceFactory do
7
7
  let(:attributes) { stub('attributes') }
8
8
 
9
- let(:service_creator) do
9
+ let(:service_factory_strategy) do
10
10
  lambda do |name|
11
11
  class_name = "#{name.to_s.camel_case}Service"
12
12
  Services.const_get(class_name.to_sym)
@@ -22,13 +22,13 @@ module Rddd
22
22
  end
23
23
 
24
24
  describe '.build' do
25
- context 'configuration service_creator given' do
25
+ context 'configuration service_factory_strategy given' do
26
26
  before do
27
- Rddd.configure { |config| config.service_creator = service_creator }
27
+ Rddd.configure { |config| config.service_factory_strategy = service_factory_strategy }
28
28
  end
29
29
 
30
30
  after do
31
- Rddd.configure { |config| config.service_creator = nil }
31
+ Rddd.configure { |config| config.service_factory_strategy = nil }
32
32
  end
33
33
 
34
34
  it 'should call the appropriate service' do
@@ -40,19 +40,19 @@ module Rddd
40
40
 
41
41
  context 'not existing service' do
42
42
  before do
43
- Rddd.configure { |config| config.service_creator = service_creator }
43
+ Rddd.configure { |config| config.service_factory_strategy = service_factory_strategy }
44
44
  end
45
45
 
46
46
  after do
47
- Rddd.configure { |config| config.service_creator = nil }
47
+ Rddd.configure { |config| config.service_factory_strategy = nil }
48
48
  end
49
49
 
50
50
  it { expect { ServiceFactory.build(:foo, attributes) }.to raise_exception ServiceFactory::InvalidService }
51
51
  end
52
52
 
53
- context 'configuration service_creator not given' do
53
+ context 'configuration service_factory_strategy not given' do
54
54
  it 'should raise exception' do
55
- expect { Rddd::Services::ServiceFactory.build(:create_project, attributes) }.to raise_exception Rddd::Services::ServiceFactory::CreatorNotGiven
55
+ expect { Rddd::Services::ServiceFactory.build(:create_project, attributes) }.to raise_exception Rddd::Services::ServiceFactory::StrategyNotGiven
56
56
  end
57
57
  end
58
58
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'rddd/services/transports/http_json'
3
+
4
+ module Rddd
5
+ module Services
6
+ module Transports
7
+ describe HttpJson do
8
+ let(:transport_attributes) do
9
+ {
10
+ :endpoint => 'http://remote.dev/'
11
+ }
12
+ end
13
+
14
+ let(:transport) { HttpJson.new(transport_attributes) }
15
+
16
+ describe '#call' do
17
+ subject { transport.call(service_name, attributes) }
18
+
19
+ let(:service_name) { :service_name }
20
+
21
+ let(:attributes) { {'foo' => 'bar'} }
22
+
23
+ let(:result) { {'foo' => 'bar'} }
24
+
25
+ let(:curl) { stub('curl', :body_str => '{"foo": "bar"}', :headers => [])}
26
+
27
+ before do
28
+ Curl.expects(:post) \
29
+ .with('http://remote.dev/service_name', JSON.unparse(attributes)) \
30
+ .yields(curl) \
31
+ .returns(curl)
32
+
33
+ curl.headers.expects(:[]=).with('Content-Type', 'application/json')
34
+ end
35
+
36
+
37
+ it { should == result }
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rddd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-19 00:00:00.000000000 Z
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: curb
@@ -74,6 +74,8 @@ files:
74
74
  - lib/rddd/services/service.rb
75
75
  - lib/rddd/services/service_bus.rb
76
76
  - lib/rddd/services/service_factory.rb
77
+ - lib/rddd/services/transports/factory.rb
78
+ - lib/rddd/services/transports/http_json.rb
77
79
  - lib/rddd/version.rb
78
80
  - rddd.gemspec
79
81
  - spec/integration_spec.rb
@@ -90,6 +92,7 @@ files:
90
92
  - spec/lib/services/service_bus_spec.rb
91
93
  - spec/lib/services/service_factory_spec.rb
92
94
  - spec/lib/services/service_spec.rb
95
+ - spec/lib/services/transports/http_json_spec.rb
93
96
  - spec/spec_helper.rb
94
97
  homepage: http://blog.ngneers.com
95
98
  licenses: []
@@ -132,5 +135,6 @@ test_files:
132
135
  - spec/lib/services/service_bus_spec.rb
133
136
  - spec/lib/services/service_factory_spec.rb
134
137
  - spec/lib/services/service_spec.rb
138
+ - spec/lib/services/transports/http_json_spec.rb
135
139
  - spec/spec_helper.rb
136
140
  has_rdoc: