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
@@ -1,26 +0,0 @@
1
- require 'alephant/broker/models/request/error_request.rb'
2
- require 'alephant/broker/models/request/get_request.rb'
3
- require 'alephant/broker/models/request/notfound_request.rb'
4
- require 'alephant/broker/models/request/post_request.rb'
5
- require 'alephant/broker/models/request/status_request.rb'
6
-
7
- module Alephant
8
- module Broker
9
- class RequestFactory
10
- def self.process(type)
11
- case type
12
- when :component
13
- GetRequest.new
14
- when :components_batch
15
- PostRequest.new
16
- when :status
17
- StatusRequest.new
18
- when :notfound
19
- NotFoundRequest.new
20
- when :error
21
- ErrorRequest.new
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,50 +0,0 @@
1
- require 'alephant/broker/models/request_factory'
2
- require 'alephant/broker/models/response_factory'
3
-
4
- module Alephant
5
- module Broker
6
- class RequestHandler
7
- include Logger
8
-
9
- def initialize(config)
10
- @config = config
11
- end
12
-
13
- def process
14
- begin
15
- response_factory.response_from(request)
16
- rescue Exception => e
17
- logger.info("Broker.requestHandler.process: Exception raised (#{e.message})")
18
- response_factory.response(500)
19
- end
20
- end
21
-
22
- private
23
-
24
- def request
25
- @request ||= RequestFactory.process(request_type)
26
- end
27
-
28
- def response_factory
29
- @response_factory ||= ResponseFactory.new(@config)
30
- end
31
-
32
- def request_type
33
- case env.request_type
34
- when 'components'
35
- :components_batch
36
- when 'component'
37
- :component
38
- when 'status'
39
- :status
40
- else
41
- :notfound
42
- end
43
- end
44
-
45
- def env
46
- @env ||= RequestStore.store[:env]
47
- end
48
- end
49
- end
50
- end
@@ -1,27 +0,0 @@
1
- module Alephant
2
- module Broker
3
- class Response
4
- attr_accessor :content, :content_type
5
- attr_reader :status
6
-
7
- STATUS_CODE_MAPPING = {
8
- 200 => 'ok',
9
- 404 => 'Not found',
10
- 500 => 'Error retrieving content'
11
- }
12
-
13
- def initialize(status = 200)
14
- @content_type = "text/html"
15
- self.status = status
16
- setup
17
- end
18
-
19
- def status=(code)
20
- @status = code
21
- @content = STATUS_CODE_MAPPING[code]
22
- end
23
-
24
- def setup; end
25
- end
26
- end
27
- end
@@ -1,90 +0,0 @@
1
- require 'crimp'
2
- require 'alephant/cache'
3
- require 'alephant/lookup'
4
- require 'alephant/broker/errors/invalid_cache_key'
5
- require 'alephant/sequencer'
6
-
7
- module Alephant
8
- module Broker
9
- class AssetResponse < Response
10
- include Logger
11
-
12
- attr_reader :request
13
-
14
- def initialize(request, config)
15
- @request = request
16
- @config = config
17
- super()
18
- end
19
-
20
- def setup
21
- begin
22
- self.content_type = request.content_type
23
- self.content = cache.get(s3_path)
24
- rescue AWS::S3::Errors::NoSuchKey, InvalidCacheKey => e
25
- set_error_for(e, 404)
26
- rescue Exception => e
27
- set_error_for(e, 500)
28
- end
29
- end
30
-
31
- private
32
-
33
- def cache
34
- @cache ||= Alephant::Cache.new(config[:s3_bucket_id], config[:s3_object_path])
35
- end
36
-
37
- def set_error_for(exception, status)
38
- logger.info("Broker.assetResponse.set_error_for: #{status} exception raised (#{exception.message})")
39
- self.status = status
40
- self.content = exception.message
41
- end
42
-
43
- def s3_path
44
- lookup.read(component_id, request.options, version).tap do |lookup_object|
45
- raise InvalidCacheKey if lookup_object.location.nil?
46
- end.location unless version.nil?
47
- end
48
-
49
- def lookup
50
- @lookup ||= Alephant::Lookup.create(config[:lookup_table_name])
51
- end
52
-
53
- def key
54
- request.type == :batch ? renderer_key : component_key
55
- end
56
-
57
- def component_key
58
- "#{component_id}/#{opts_hash}"
59
- end
60
-
61
- def renderer_key
62
- "#{renderer_id}/#{opts_hash}"
63
- end
64
-
65
- def component_id
66
- request.component_id
67
- end
68
-
69
- def renderer_id
70
- request.renderer_id
71
- end
72
-
73
- def opts_hash
74
- @opts_hash ||= Crimp.signature(request.options)
75
- end
76
-
77
- def version
78
- @version ||= sequencer.get_last_seen
79
- end
80
-
81
- def sequencer
82
- @sequencer ||= Alephant::Sequencer.create(config[:sequencer_table_name], key)
83
- end
84
-
85
- def config
86
- @config
87
- end
88
- end
89
- end
90
- end
@@ -1,61 +0,0 @@
1
- require 'alephant/logger'
2
- require 'peach'
3
-
4
- module Alephant
5
- module Broker
6
- class BatchResponse
7
- include Logger
8
- attr_reader :status, :content_type, :content
9
-
10
- def initialize(request, config)
11
- @request = request
12
- @config = config
13
- @status = 200
14
- @content_type = request.content_type
15
- end
16
-
17
- def process
18
- @content = JSON.generate({ "batch_id" => batch_id, "components" => json })
19
- self
20
- end
21
-
22
- private
23
-
24
- def json
25
- get_components.peach do |component|
26
- thread_local_request = @request.clone
27
-
28
- id = component['component']
29
- options = set_keys_to_symbols component.fetch('options', {})
30
-
31
- thread_local_request.set_component(id, options)
32
-
33
- asset = AssetResponse.new(thread_local_request, @config)
34
- component.store('status', asset.status)
35
- component.store('body', asset.content) if valid_status_for asset
36
- end
37
- end
38
-
39
- def set_keys_to_symbols(hash)
40
- Hash[hash.map { |k,v| [k.to_sym, v] }]
41
- end
42
-
43
- def batch_id
44
- @request.components.fetch(:batch_id)
45
- end
46
-
47
- def get_components
48
- @request.components.fetch(:components) do |key|
49
- logger.info("Broker::BatchResponse.process: Received request object but no valid key was found")
50
- []
51
- end
52
- end
53
-
54
- def valid_status_for(asset)
55
- asset.status == 200
56
- end
57
- end
58
- end
59
- end
60
-
61
-
@@ -1,39 +0,0 @@
1
- require 'alephant/broker/models/response'
2
- require 'alephant/broker/models/response/asset_response'
3
- require 'alephant/broker/models/response/batch_response'
4
-
5
- module Alephant
6
- module Broker
7
- class ResponseFactory
8
-
9
- def initialize(config = nil)
10
- @config = config
11
- end
12
-
13
- def response_from(request)
14
- case request.type
15
- when :asset
16
- AssetResponse.new(request, config)
17
- when :batch
18
- BatchResponse.new(request, config).process
19
- when :status
20
- response(200)
21
- when :notfound
22
- response(404)
23
- when :error
24
- response(500)
25
- end
26
- end
27
-
28
- def response(status)
29
- Response.new(status)
30
- end
31
-
32
- private
33
-
34
- def config
35
- @config
36
- end
37
- end
38
- end
39
- end
@@ -1,78 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Alephant::Broker::AssetResponse do
4
- subject { Alephant::Broker::AssetResponse }
5
-
6
- describe "#initialize(request, config)" do
7
- let(:location) { 'test_location' }
8
-
9
- let(:config) {{
10
- :lookup_table_name => 'test_table',
11
- :bucket_id => 'test_bucket',
12
- :path => 'test_path'
13
- }}
14
-
15
- let(:request) { double(
16
- "Alephant::Broker::Request",
17
- :component_id => 'test',
18
- :content_type => 'text/html',
19
- :type => :asset,
20
- :options => { :variant => 'test_variant' }
21
- )
22
- }
23
-
24
- before do
25
- subject
26
- .any_instance
27
- .stub(:s3_path)
28
- .and_return(:foo)
29
- end
30
-
31
- context "successful" do
32
- before(:each) do
33
- subject
34
- .any_instance
35
- .stub(:cache)
36
- .and_return(double(:get => 'Test'))
37
- end
38
-
39
- it "Should return the content from a successful cache lookup" do
40
- instance = subject.new(request, config)
41
-
42
- expect(instance.content).to eq('Test')
43
- expect(instance.status).to eq(200)
44
- end
45
- end
46
-
47
- context "client failure" do
48
- before(:each) do
49
- subject
50
- .any_instance
51
- .stub(:cache)
52
- .and_raise(Alephant::Broker::InvalidCacheKey)
53
- end
54
-
55
- it "should return a 404 if lookup can't find a valid location" do
56
- instance = subject.new(request, config)
57
- expect(instance.status).to eq(404)
58
- end
59
- end
60
-
61
- context "server failure" do
62
- before(:each) do
63
- subject
64
- .any_instance
65
- .stub(:cache)
66
- .and_raise(Exception)
67
- end
68
-
69
- it "should return a 500 for any other exceptions" do
70
- instance = subject.new(request, config)
71
- expect(instance.status).to eq(500)
72
- end
73
- end
74
- end
75
- end
76
-
77
-
78
-
@@ -1,87 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Alephant::Broker::BatchResponse do
4
- subject { Alephant::Broker::BatchResponse }
5
-
6
- let (:config) {{
7
- :lookup_table_name => 'test_table',
8
- :bucket_id => 'test_bucket',
9
- :path => 'test_path'
10
- }}
11
-
12
- let (:post_request) {
13
- double(
14
- 'Alephant::Broker::PostRequest',
15
- :options => {},
16
- :type => :batch,
17
- :content_type => 'application/json',
18
- :set_component => nil,
19
- :component_id => nil,
20
- :renderer_id => nil,
21
- :components => {
22
- :batch_id => :baz,
23
- :components => [
24
- { 'component' => 'foo1', 'options' => { 'variant' => 'bar1' } },
25
- { 'component' => 'foo2', 'options' => { 'variant' => 'bar2' } }
26
- ]}
27
- ).as_null_object
28
- }
29
-
30
- before do
31
- Alephant::Broker::AssetResponse
32
- .any_instance
33
- .stub(:initialize)
34
- end
35
-
36
- describe "#process" do
37
- context "if a component is unrecognised" do
38
- before(:each) do
39
- Alephant::Broker::AssetResponse
40
- .any_instance
41
- .stub(:status)
42
- .and_return(404)
43
-
44
- instance = subject.new(post_request, config)
45
- json = JSON.parse(instance.process.content)
46
- @bad_component = json.fetch('components')[1]
47
- end
48
-
49
- it "set status to 404" do
50
- expect(@bad_component['status']).to eq(404)
51
- end
52
-
53
- it "remove 'body' key" do
54
- expect(@bad_component['body']).to eq(nil)
55
- end
56
- end
57
-
58
- context "if a component is recognised" do
59
- before(:each) do
60
- Alephant::Broker::AssetResponse
61
- .any_instance
62
- .stub(:status)
63
- .and_return(200)
64
-
65
- Alephant::Broker::AssetResponse
66
- .any_instance
67
- .stub(:content)
68
- .and_return('Test')
69
-
70
- @instance = subject.new(post_request, config)
71
- @content = @instance.process.content
72
- @json = JSON.parse(@content)
73
- end
74
-
75
- it "set status to 200" do
76
- @json.fetch('components').each do |component|
77
- expect(component['status']).to eq(200)
78
- end
79
- end
80
-
81
- it "set @content to be JSON string containing retrieved components" do
82
- compiled_json = '{"batch_id":"baz","components":[{"component":"foo1","options":{"variant":"bar1"},"status":200,"body":"Test"},{"component":"foo2","options":{"variant":"bar2"},"status":200,"body":"Test"}]}'
83
- expect(@content).to eq(compiled_json)
84
- end
85
- end
86
- end
87
- end