alephant-broker 1.0.5 → 1.1.0
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.
- checksums.yaml +4 -4
- data/lib/alephant/broker.rb +32 -2
- data/lib/alephant/broker/component.rb +10 -5
- data/lib/alephant/broker/request.rb +1 -0
- data/lib/alephant/broker/request/asset.rb +6 -15
- data/lib/alephant/broker/request/factory.rb +2 -0
- data/lib/alephant/broker/request/multi.rb +40 -0
- data/lib/alephant/broker/response.rb +1 -0
- data/lib/alephant/broker/response/asset.rb +6 -5
- data/lib/alephant/broker/response/base.rb +29 -10
- data/lib/alephant/broker/response/factory.rb +2 -0
- data/lib/alephant/broker/response/multi.rb +49 -0
- data/lib/alephant/broker/version.rb +1 -1
- data/spec/rack_spec.rb +11 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cebf819da714467f9faec319d6343c6e0d086117
|
4
|
+
data.tar.gz: d154fe7544fb5f9f904a5f5849817547bb897629
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5730739e5d7abc50fcbc385a6e06521b93a4ddd14c396abcf177bfd3a3db8da2ca0befb8fd4b18193582d54b31504d2e60253769746661dbde88d13beb83b2d6
|
7
|
+
data.tar.gz: 19029f98faa67945a4947cb40d31a5b296de6d52a228a2817b2f5bd264a481d9b66761c96c3243a7546b55fd25e8086911625653da7c7a34d62dd5a3d4684e92
|
data/lib/alephant/broker.rb
CHANGED
@@ -2,9 +2,11 @@ require 'alephant/broker/version'
|
|
2
2
|
require 'alephant/broker/request'
|
3
3
|
require 'alephant/broker/environment'
|
4
4
|
require 'alephant/broker'
|
5
|
+
require 'ostruct'
|
5
6
|
|
6
7
|
module Alephant
|
7
8
|
module Broker
|
9
|
+
@@poll = true
|
8
10
|
|
9
11
|
def self.handle(env)
|
10
12
|
Request::Handler.process env
|
@@ -18,13 +20,25 @@ module Alephant
|
|
18
20
|
@@configuration = c
|
19
21
|
end
|
20
22
|
|
23
|
+
def self.poll?
|
24
|
+
@@poll
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.poll=(state)
|
28
|
+
@@poll = state
|
29
|
+
end
|
30
|
+
|
21
31
|
class Application
|
22
32
|
def initialize(c = nil)
|
23
33
|
Broker.config = c unless c.nil?
|
24
34
|
end
|
25
35
|
|
26
36
|
def call(env)
|
27
|
-
|
37
|
+
if ::Alephant::Broker.poll?
|
38
|
+
send response_for(environment_for(env))
|
39
|
+
else
|
40
|
+
send stop_poll_response
|
41
|
+
end
|
28
42
|
end
|
29
43
|
|
30
44
|
def environment_for(env)
|
@@ -42,10 +56,26 @@ module Alephant
|
|
42
56
|
"Content-Type" => response.content_type,
|
43
57
|
"X-Version" => response.version.to_s,
|
44
58
|
"X-Cached" => response.cached.to_s
|
45
|
-
},
|
59
|
+
}.merge(response.headers),
|
46
60
|
[ response.content.to_s ]
|
47
61
|
]
|
48
62
|
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def stop_poll_response
|
67
|
+
response = OpenStruct.new(
|
68
|
+
:status => 420,
|
69
|
+
:content => "Stopped polling",
|
70
|
+
:cached => false,
|
71
|
+
:version => 0,
|
72
|
+
:headers => {
|
73
|
+
"Content-Type" => "plain/text",
|
74
|
+
"X-Cached" => "false",
|
75
|
+
"X-Stop-Polling" => "true"
|
76
|
+
}
|
77
|
+
)
|
78
|
+
end
|
49
79
|
end
|
50
80
|
end
|
51
81
|
end
|
@@ -12,7 +12,7 @@ module Alephant
|
|
12
12
|
class Component
|
13
13
|
include Logger
|
14
14
|
|
15
|
-
attr_reader :id, :batch_id, :options, :content, :cached
|
15
|
+
attr_reader :id, :batch_id, :options, :content, :content_type, :cached
|
16
16
|
|
17
17
|
def initialize(id, batch_id, options)
|
18
18
|
@id = id
|
@@ -23,10 +23,8 @@ module Alephant
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def load
|
26
|
-
@
|
27
|
-
|
28
|
-
s3.get(s3_path)
|
29
|
-
end
|
26
|
+
@content_type = cache_object[:content_type]
|
27
|
+
@content = cache_object[:content]
|
30
28
|
end
|
31
29
|
|
32
30
|
def opts_hash
|
@@ -39,6 +37,13 @@ module Alephant
|
|
39
37
|
|
40
38
|
private
|
41
39
|
|
40
|
+
def cache_object
|
41
|
+
@cache_object ||= @cache.get(cache_key) do
|
42
|
+
@cached = false
|
43
|
+
s3.get(s3_path)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
42
47
|
def cache_key
|
43
48
|
@cache_key ||= "#{id}/#{opts_hash}/#{version}"
|
44
49
|
end
|
@@ -8,26 +8,17 @@ module Alephant
|
|
8
8
|
class Asset
|
9
9
|
include Logger
|
10
10
|
|
11
|
-
|
11
|
+
attr_accessor :component
|
12
12
|
|
13
|
-
def initialize(env)
|
14
|
-
|
15
|
-
component_id = component_id_for env.path
|
13
|
+
def initialize(env = nil)
|
14
|
+
return if env.nil?
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
nil,
|
20
|
-
env.options
|
21
|
-
)
|
16
|
+
component_id = env.path.split('/')[2] || nil
|
17
|
+
options = env.options
|
22
18
|
|
23
|
-
logger.debug("Request::Asset#initialize: id: #{component_id}")
|
24
19
|
raise InvalidAssetId.new("No Asset ID specified") if component_id.nil?
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
20
|
|
29
|
-
|
30
|
-
path.split('/')[2] || nil
|
21
|
+
@component = Component.new(component_id, nil, options)
|
31
22
|
end
|
32
23
|
|
33
24
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'alephant/logger'
|
2
|
+
require 'alephant/broker/component'
|
3
|
+
|
4
|
+
module Alephant
|
5
|
+
module Broker
|
6
|
+
module Request
|
7
|
+
class Multi
|
8
|
+
include Logger
|
9
|
+
|
10
|
+
attr_reader :requests
|
11
|
+
|
12
|
+
def initialize(env)
|
13
|
+
logger.debug("Request::Multi#initialize(#{env.settings})")
|
14
|
+
@requests = requests_for env
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def requests_for(env)
|
20
|
+
env.data['requests'].map do |c|
|
21
|
+
case c['type']
|
22
|
+
when 'asset'
|
23
|
+
asset = Asset.new
|
24
|
+
|
25
|
+
component_id = c['payload']['component_id']
|
26
|
+
options = c['payload']['options']
|
27
|
+
|
28
|
+
component = Component.new(component_id, nil, options)
|
29
|
+
asset.tap { |a| a.component = component }
|
30
|
+
else
|
31
|
+
raise StandardError.new "request type not identified"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
@@ -15,12 +15,13 @@ module Alephant
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def setup
|
18
|
+
loaded_content = load(component)
|
18
19
|
|
19
|
-
|
20
|
-
@
|
21
|
-
@
|
22
|
-
@version
|
23
|
-
@cached
|
20
|
+
@content = loaded_content[:body]
|
21
|
+
@content_type = loaded_content[:content_type]
|
22
|
+
@status = loaded_content[:status]
|
23
|
+
@version = component.version.nil? ? 'not available' : component.version
|
24
|
+
@cached = component.cached
|
24
25
|
end
|
25
26
|
|
26
27
|
end
|
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'aws-sdk'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
4
|
module Alephant
|
4
5
|
module Broker
|
5
6
|
module Response
|
6
7
|
class Base
|
8
|
+
attr_reader :headers
|
7
9
|
attr_accessor :status, :content, :content_type, :version, :cached
|
8
10
|
|
9
11
|
STATUS_CODE_MAPPING = {
|
@@ -13,31 +15,48 @@ module Alephant
|
|
13
15
|
}
|
14
16
|
|
15
17
|
def initialize(status = 200, content_type = "text/html")
|
18
|
+
@headers = {}
|
19
|
+
@version = 'not available'
|
20
|
+
@cached = false
|
16
21
|
@content_type = content_type
|
17
|
-
@status
|
18
|
-
@content
|
22
|
+
@status = status
|
23
|
+
@content = STATUS_CODE_MAPPING[status]
|
19
24
|
|
20
25
|
setup
|
21
26
|
end
|
22
27
|
|
28
|
+
def to_h
|
29
|
+
{
|
30
|
+
:status => @status,
|
31
|
+
:content => @content,
|
32
|
+
:content_type => @content_type,
|
33
|
+
:version => @version,
|
34
|
+
:cached => @cached
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
23
38
|
protected
|
24
39
|
|
25
40
|
def setup; end
|
26
41
|
|
27
42
|
def load(component)
|
28
43
|
begin
|
29
|
-
|
30
|
-
|
44
|
+
|
45
|
+
data = OpenStruct.new(:status => 200, :content_type => content_type)
|
46
|
+
component.load
|
47
|
+
|
48
|
+
data.content_type = component.content_type
|
49
|
+
data.body = component.content.force_encoding('UTF-8')
|
31
50
|
rescue AWS::S3::Errors::NoSuchKey, InvalidCacheKey => e
|
32
|
-
body = "
|
33
|
-
status = 404
|
51
|
+
data.body = "Not found"
|
52
|
+
data.status = 404
|
34
53
|
rescue StandardError => e
|
35
|
-
body = "#{error_for(e)}"
|
36
|
-
status = 500
|
54
|
+
data.body = "#{error_for(e)}"
|
55
|
+
data.status = 500
|
37
56
|
end
|
38
57
|
|
39
|
-
log(component, status, e)
|
40
|
-
|
58
|
+
log(component, data.status, e)
|
59
|
+
data.marshal_dump
|
41
60
|
end
|
42
61
|
|
43
62
|
def log(c, status, e = nil)
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Alephant
|
4
|
+
module Broker
|
5
|
+
module Response
|
6
|
+
class Multi < Base
|
7
|
+
attr_reader :requests
|
8
|
+
|
9
|
+
POLLING_DELAY = 2
|
10
|
+
|
11
|
+
def initialize(requests)
|
12
|
+
@requests = requests
|
13
|
+
|
14
|
+
super()
|
15
|
+
end
|
16
|
+
|
17
|
+
def raw_response
|
18
|
+
requests.reduce(:delay => POLLING_DELAY, :responses => []) do |m,request|
|
19
|
+
response = Factory.response_for request
|
20
|
+
|
21
|
+
case response
|
22
|
+
when Asset
|
23
|
+
m[:responses] << {
|
24
|
+
:type => response.class.to_s.downcase,
|
25
|
+
:datatype => response.content_type,
|
26
|
+
:payload => {
|
27
|
+
:component_id => response.component.id,
|
28
|
+
:options => response.component.options,
|
29
|
+
:body => response.to_h
|
30
|
+
}
|
31
|
+
}
|
32
|
+
when NotFound
|
33
|
+
# Do nothing
|
34
|
+
else
|
35
|
+
raise StandardError.new "response type not identified"
|
36
|
+
end
|
37
|
+
|
38
|
+
m
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def setup
|
43
|
+
@content_type = 'application/json'
|
44
|
+
@content = JSON.generate(raw_response)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/rack_spec.rb
CHANGED
@@ -15,6 +15,16 @@ describe 'Broker Rack Application' do
|
|
15
15
|
.stub(:load)
|
16
16
|
.and_return('Test')
|
17
17
|
|
18
|
+
Alephant::Broker::Component
|
19
|
+
.any_instance
|
20
|
+
.stub(:content)
|
21
|
+
.and_return('Test')
|
22
|
+
|
23
|
+
Alephant::Broker::Component
|
24
|
+
.any_instance
|
25
|
+
.stub(:content_type)
|
26
|
+
.and_return('foo/bar')
|
27
|
+
|
18
28
|
Alephant::Broker::Component
|
19
29
|
.any_instance
|
20
30
|
.stub(:version)
|
@@ -84,7 +94,7 @@ describe 'Broker Rack Application' do
|
|
84
94
|
|
85
95
|
it "Test batch asset data is returned" do
|
86
96
|
json = '{"batch_id":"baz","components":[{"component":"ni_council_results_table"},{"component":"ni_council_results_table"}]}'
|
87
|
-
compiled_json = '{"batch_id":"baz","components":[{"component":"ni_council_results_table","options":{},"
|
97
|
+
compiled_json = '{"batch_id":"baz","components":[{"component":"ni_council_results_table","options":{},"status":200,"content_type":"foo/bar","body":"Test"},{"component":"ni_council_results_table","options":{},"status":200,"content_type":"foo/bar","body":"Test"}]}'
|
88
98
|
|
89
99
|
post '/components/batch', json, "CONTENT_TYPE" => "application/json"
|
90
100
|
|
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: 1.0
|
4
|
+
version: 1.1.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-
|
11
|
+
date: 2014-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -275,11 +275,13 @@ files:
|
|
275
275
|
- lib/alephant/broker/request/batch.rb
|
276
276
|
- lib/alephant/broker/request/factory.rb
|
277
277
|
- lib/alephant/broker/request/handler.rb
|
278
|
+
- lib/alephant/broker/request/multi.rb
|
278
279
|
- lib/alephant/broker/response.rb
|
279
280
|
- lib/alephant/broker/response/asset.rb
|
280
281
|
- lib/alephant/broker/response/base.rb
|
281
282
|
- lib/alephant/broker/response/batch.rb
|
282
283
|
- lib/alephant/broker/response/factory.rb
|
284
|
+
- lib/alephant/broker/response/multi.rb
|
283
285
|
- lib/alephant/broker/version.rb
|
284
286
|
- spec/rack_spec.rb
|
285
287
|
- spec/spec_helper.rb
|