alephant-broker 0.1.6 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/lib/alephant/broker.rb +40 -5
  3. data/lib/alephant/broker/component.rb +74 -0
  4. data/lib/alephant/broker/{call_environment.rb → environment.rb} +11 -7
  5. data/lib/alephant/broker/request.rb +13 -0
  6. data/lib/alephant/broker/request/asset.rb +37 -0
  7. data/lib/alephant/broker/request/batch.rb +38 -0
  8. data/lib/alephant/broker/request/factory.rb +24 -0
  9. data/lib/alephant/broker/request/handler.rb +35 -0
  10. data/lib/alephant/broker/response.rb +23 -0
  11. data/lib/alephant/broker/response/asset.rb +28 -0
  12. data/lib/alephant/broker/response/base.rb +60 -0
  13. data/lib/alephant/broker/response/batch.rb +45 -0
  14. data/lib/alephant/broker/response/factory.rb +27 -0
  15. data/lib/alephant/broker/version.rb +1 -1
  16. data/spec/rack_spec.rb +14 -15
  17. data/spec/spec_helper.rb +0 -1
  18. metadata +14 -27
  19. data/lib/alephant/broker/app.rb +0 -20
  20. data/lib/alephant/broker/app/rack.rb +0 -25
  21. data/lib/alephant/broker/models/request.rb +0 -41
  22. data/lib/alephant/broker/models/request/error_request.rb +0 -11
  23. data/lib/alephant/broker/models/request/get_request.rb +0 -38
  24. data/lib/alephant/broker/models/request/notfound_request.rb +0 -11
  25. data/lib/alephant/broker/models/request/post_request.rb +0 -50
  26. data/lib/alephant/broker/models/request/status_request.rb +0 -11
  27. data/lib/alephant/broker/models/request_factory.rb +0 -26
  28. data/lib/alephant/broker/models/request_handler.rb +0 -50
  29. data/lib/alephant/broker/models/response.rb +0 -27
  30. data/lib/alephant/broker/models/response/asset_response.rb +0 -90
  31. data/lib/alephant/broker/models/response/batch_response.rb +0 -61
  32. data/lib/alephant/broker/models/response_factory.rb +0 -39
  33. data/spec/asset_response_spec.rb +0 -78
  34. data/spec/batch_response_spec.rb +0 -87
  35. data/spec/get_request_spec.rb +0 -72
  36. data/spec/post_request_spec.rb +0 -49
  37. data/spec/response_factory_spec.rb +0 -60
@@ -0,0 +1,45 @@
1
+ require 'alephant/logger'
2
+ require 'peach'
3
+
4
+ module Alephant
5
+ module Broker
6
+ module Response
7
+ class Batch < Base
8
+ include Logger
9
+
10
+ attr_reader :components, :batch_id
11
+
12
+ def initialize(components, batch_id)
13
+ @components = components
14
+ @batch_id = batch_id
15
+
16
+ super(200, 'application/json')
17
+ end
18
+
19
+ def setup
20
+ @content = JSON.generate({
21
+ "batch_id" => batch_id,
22
+ "components" => json
23
+ })
24
+ end
25
+
26
+ private
27
+
28
+ def json
29
+ logger.info("Broker: Batch load started (#{batch_id})")
30
+ result = components.pmap do | component |
31
+ {
32
+ 'component' => component.id,
33
+ 'options' => component.options
34
+ }.merge load(component)
35
+ end
36
+ logger.info("Broker: Batch load done (#{batch_id})")
37
+
38
+ result
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,27 @@
1
+ require 'alephant/broker/response'
2
+
3
+ module Alephant
4
+ module Broker
5
+ module Response
6
+ class Factory
7
+ def self.response_for(request)
8
+ case request
9
+ when Request::Asset
10
+ Asset.new(request.component)
11
+ when Request::Batch
12
+ Batch.new(request.components, request.batch_id)
13
+ when Request::Status
14
+ Status.new
15
+ else
16
+ NotFound.new
17
+ end
18
+ end
19
+
20
+ def self.error
21
+ ServerError.new
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Broker
3
- VERSION = "0.1.6"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -1,8 +1,8 @@
1
1
  ENV['RACK_ENV'] = 'test'
2
2
 
3
+ require 'spec_helper'
3
4
  require 'rack/test'
4
- require 'alephant/broker/app/rack'
5
- require 'request_store'
5
+ require 'alephant/broker'
6
6
 
7
7
  RSpec.configure do |conf|
8
8
  conf.include Rack::Test::Methods
@@ -10,25 +10,24 @@ end
10
10
 
11
11
  describe 'Broker Rack Application' do
12
12
  before do
13
- RequestStore.store[:env] = nil
13
+ Alephant::Broker::Component
14
+ .any_instance
15
+ .stub(:load)
16
+ .and_return('Test')
14
17
 
15
- Alephant::Broker::AssetResponse
18
+ Alephant::Broker::Component
16
19
  .any_instance
17
- .stub(:initialize)
20
+ .stub(:version)
21
+ .and_return(1)
18
22
 
19
- Alephant::Broker::AssetResponse
23
+ Alephant::Broker::Response::Asset
20
24
  .any_instance
21
25
  .stub(:status)
22
26
  .and_return(200)
23
-
24
- Alephant::Broker::AssetResponse
25
- .any_instance
26
- .stub(:content)
27
- .and_return('Test')
28
27
  end
29
28
 
30
29
  def app
31
- Alephant::Broker::RackApplication.new({
30
+ Alephant::Broker::Application.new({
32
31
  :lookup_table_name => 'test_table',
33
32
  :bucket_id => 'test_bucket',
34
33
  :path => 'bucket_path'
@@ -62,7 +61,7 @@ describe 'Broker Rack Application' do
62
61
  end
63
62
 
64
63
  it "Tests 404 when lookup doesn't return a valid location" do
65
- Alephant::Broker::AssetResponse
64
+ Alephant::Broker::Response::Asset
66
65
  .any_instance
67
66
  .stub(:status)
68
67
  .and_return(404)
@@ -73,7 +72,7 @@ describe 'Broker Rack Application' do
73
72
  end
74
73
 
75
74
  it "Tests 500 when exception is raised in application" do
76
- Alephant::Broker::AssetResponse
75
+ Alephant::Broker::Response::Asset
77
76
  .any_instance
78
77
  .stub(:status)
79
78
  .and_return(500)
@@ -85,7 +84,7 @@ describe 'Broker Rack Application' do
85
84
 
86
85
  it "Test batch asset data is returned" do
87
86
  json = '{"batch_id":"baz","components":[{"component":"ni_council_results_table"},{"component":"ni_council_results_table"}]}'
88
- compiled_json = '{"batch_id":"baz","components":[{"component":"ni_council_results_table","status":200,"body":"Test"},{"component":"ni_council_results_table","status":200,"body":"Test"}]}'
87
+ compiled_json = '{"batch_id":"baz","components":[{"component":"ni_council_results_table","options":{},"body":"Test","status":200},{"component":"ni_council_results_table","options":{},"body":"Test","status":200}]}'
89
88
 
90
89
  post '/components/batch', json, "CONTENT_TYPE" => "application/json"
91
90
 
@@ -3,4 +3,3 @@ $: << File.join(File.dirname(__FILE__), '..', 'lib')
3
3
  require 'pry'
4
4
  require 'json'
5
5
  require 'alephant/broker'
6
- require 'request_store'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-broker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Jack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -251,30 +251,22 @@ files:
251
251
  - Rakefile
252
252
  - alephant-broker.gemspec
253
253
  - lib/alephant/broker.rb
254
- - lib/alephant/broker/app.rb
255
- - lib/alephant/broker/app/rack.rb
256
- - lib/alephant/broker/call_environment.rb
254
+ - lib/alephant/broker/component.rb
255
+ - lib/alephant/broker/environment.rb
257
256
  - lib/alephant/broker/errors/invalid_asset_id.rb
258
257
  - lib/alephant/broker/errors/invalid_cache_key.rb
259
- - lib/alephant/broker/models/request.rb
260
- - lib/alephant/broker/models/request/error_request.rb
261
- - lib/alephant/broker/models/request/get_request.rb
262
- - lib/alephant/broker/models/request/notfound_request.rb
263
- - lib/alephant/broker/models/request/post_request.rb
264
- - lib/alephant/broker/models/request/status_request.rb
265
- - lib/alephant/broker/models/request_factory.rb
266
- - lib/alephant/broker/models/request_handler.rb
267
- - lib/alephant/broker/models/response.rb
268
- - lib/alephant/broker/models/response/asset_response.rb
269
- - lib/alephant/broker/models/response/batch_response.rb
270
- - lib/alephant/broker/models/response_factory.rb
258
+ - lib/alephant/broker/request.rb
259
+ - lib/alephant/broker/request/asset.rb
260
+ - lib/alephant/broker/request/batch.rb
261
+ - lib/alephant/broker/request/factory.rb
262
+ - lib/alephant/broker/request/handler.rb
263
+ - lib/alephant/broker/response.rb
264
+ - lib/alephant/broker/response/asset.rb
265
+ - lib/alephant/broker/response/base.rb
266
+ - lib/alephant/broker/response/batch.rb
267
+ - lib/alephant/broker/response/factory.rb
271
268
  - lib/alephant/broker/version.rb
272
- - spec/asset_response_spec.rb
273
- - spec/batch_response_spec.rb
274
- - spec/get_request_spec.rb
275
- - spec/post_request_spec.rb
276
269
  - spec/rack_spec.rb
277
- - spec/response_factory_spec.rb
278
270
  - spec/spec_helper.rb
279
271
  homepage: https://github.com/BBC-News/alephant-broker
280
272
  licenses:
@@ -301,10 +293,5 @@ signing_key:
301
293
  specification_version: 4
302
294
  summary: Brokers requests for alephant components
303
295
  test_files:
304
- - spec/asset_response_spec.rb
305
- - spec/batch_response_spec.rb
306
- - spec/get_request_spec.rb
307
- - spec/post_request_spec.rb
308
296
  - spec/rack_spec.rb
309
- - spec/response_factory_spec.rb
310
297
  - spec/spec_helper.rb
@@ -1,20 +0,0 @@
1
- $: << File.dirname(__FILE__)
2
-
3
- require 'alephant/broker'
4
- require 'alephant/logger'
5
-
6
- module Alephant
7
- module Broker
8
- class Application
9
- include Logger
10
-
11
- def initialize(config)
12
- @config = config
13
- end
14
-
15
- def handle
16
- Alephant::Broker.handle(@config)
17
- end
18
- end
19
- end
20
- end
@@ -1,25 +0,0 @@
1
- $: << File.dirname(__FILE__)
2
-
3
- require 'alephant/broker/app'
4
- require 'alephant/broker/call_environment'
5
-
6
- module Alephant
7
- module Broker
8
- class RackApplication < Application
9
-
10
- def call(env)
11
- RequestStore.store[:env] ||= CallEnvironment.new(env)
12
- response = handle
13
- send response
14
- end
15
-
16
- def send(response)
17
- [
18
- response.status,
19
- { "Content-Type" => response.content_type },
20
- [ response.content.to_s ]
21
- ]
22
- end
23
- end
24
- end
25
- end
@@ -1,41 +0,0 @@
1
- require 'alephant/logger'
2
-
3
- module Alephant
4
- module Broker
5
- class Request
6
- include Logger
7
- attr_reader :type
8
-
9
- DEFAULT_EXTENSION = :html
10
-
11
- @@extension_mapping = {
12
- :html => 'text/html',
13
- :json => 'application/json'
14
- }
15
-
16
- def initialize(request_type)
17
- logger.info("Broker.request: Type: #{request_type}")
18
- @type = request_type
19
- end
20
-
21
- def options_from(query_string)
22
- query_string.split('&').reduce({}) do |object, key_pair|
23
- key, value = key_pair.split('=')
24
- object.tap { |o| o.store(key.to_sym, value) }
25
- end
26
- end
27
-
28
- def get_type_from(request_parts)
29
- request_parts[1]
30
- end
31
-
32
- def get_extension_for(path)
33
- path.split('.')[1] ? path.split('.')[1].to_sym : DEFAULT_EXTENSION
34
- end
35
-
36
- def get_component_id_from(request_parts)
37
- (request_parts[2] || '').split('.')[0]
38
- end
39
- end
40
- end
41
- end
@@ -1,11 +0,0 @@
1
- require 'alephant/broker/models/request'
2
-
3
- module Alephant
4
- module Broker
5
- class ErrorRequest < Request
6
- def initialize
7
- super(:error)
8
- end
9
- end
10
- end
11
- end
@@ -1,38 +0,0 @@
1
- require 'alephant/broker/errors/invalid_asset_id'
2
- require 'alephant/broker/models/request'
3
-
4
- module Alephant
5
- module Broker
6
- class GetRequest < Request
7
- attr_reader :type, :component_id, :extension, :options, :content_type
8
-
9
- def initialize
10
- super(:asset)
11
- env = RequestStore.store[:env]
12
- parse requested_components(env.path, env.query)
13
- end
14
-
15
- def requested_components(path, query_string)
16
- request_parts = path.split('/')
17
-
18
- {
19
- :type => get_type_from(request_parts),
20
- :component_id => get_component_id_from(request_parts),
21
- :extension => get_extension_for(path),
22
- :options => options_from(query_string)
23
- }
24
- end
25
-
26
- def parse(request)
27
- @component_id = request[:component_id]
28
- @extension = request[:extension]
29
- @options = request[:options]
30
- @content_type = @@extension_mapping[@extension.to_sym] || @@extension_mapping[DEFAULT_EXTENSION]
31
-
32
- logger.info("Broker.request: Type: #{@type}, Asset ID: #{@component_id}, Options: #{@options.inspect}")
33
-
34
- raise InvalidAssetId.new("No Asset ID specified") if @component_id.nil?
35
- end
36
- end
37
- end
38
- end
@@ -1,11 +0,0 @@
1
- require 'alephant/broker/models/request'
2
-
3
- module Alephant
4
- module Broker
5
- class NotFoundRequest < Request
6
- def initialize
7
- super(:notfound)
8
- end
9
- end
10
- end
11
- end
@@ -1,50 +0,0 @@
1
- require 'alephant/broker/models/request'
2
-
3
- module Alephant
4
- module Broker
5
- class PostRequest < Request
6
- attr_reader :type, :renderer_id, :component_id, :options, :content_type
7
-
8
- def initialize
9
- @renderer_id = batch_id
10
- @content_type = 'application/json'
11
- super(:batch)
12
- end
13
-
14
- def components
15
- @requested_components ||= components_for env.path
16
- end
17
-
18
- def set_component(id, options)
19
- @component_id = id
20
- @options = options
21
- end
22
-
23
- private
24
-
25
- def env
26
- @env ||= RequestStore.store[:env]
27
- end
28
-
29
- def components_for(path)
30
- request_parts = path.split('/')
31
-
32
- {
33
- :batch_id => batch_id,
34
- :type => get_type_from(request_parts),
35
- :component_id => get_component_id_from(request_parts)
36
- }.merge! batched
37
- end
38
-
39
- def batch_id
40
- env.data['batch_id']
41
- end
42
-
43
- def batched
44
- env.data['components'].reduce({ :components => [] }) do |obj, component|
45
- obj.tap { |o| o[:components].push(component) }
46
- end
47
- end
48
- end
49
- end
50
- end
@@ -1,11 +0,0 @@
1
- require 'alephant/broker/models/request'
2
-
3
- module Alephant
4
- module Broker
5
- class StatusRequest < Request
6
- def initialize
7
- super(:status)
8
- end
9
- end
10
- end
11
- end