alephant-broker 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 565f5f6b0ad1c0dcfd6024bae98ce1fd1afcb21f
4
- data.tar.gz: f3f17262eb4a373874951c16c1cd9a8ec0c3d19b
3
+ metadata.gz: cebf819da714467f9faec319d6343c6e0d086117
4
+ data.tar.gz: d154fe7544fb5f9f904a5f5849817547bb897629
5
5
  SHA512:
6
- metadata.gz: d77b8eca85c8ee60f7dad50445e49cc93da881dd46050e53d8c609661f7312b0c3a9a68f79a0cd98209e3bbf7760e45f9ca060cd773b62ff86c2c536ece0baeb
7
- data.tar.gz: ee3493fc5ceb18f301893127c6dfaf2097631bb310d110e2173b5d7c2960d8fb56ec767867ef1a86802b4c6d83a77e2419b39fefac130678f7ea570b2da672b2
6
+ metadata.gz: 5730739e5d7abc50fcbc385a6e06521b93a4ddd14c396abcf177bfd3a3db8da2ca0befb8fd4b18193582d54b31504d2e60253769746661dbde88d13beb83b2d6
7
+ data.tar.gz: 19029f98faa67945a4947cb40d31a5b296de6d52a228a2817b2f5bd264a481d9b66761c96c3243a7546b55fd25e8086911625653da7c7a34d62dd5a3d4684e92
@@ -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
- send response_for(environment_for(env))
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
- @content ||= @cache.get(cache_key) do
27
- @cached = false
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
@@ -5,6 +5,7 @@ module Alephant
5
5
  require 'alephant/broker/request/batch'
6
6
  require 'alephant/broker/request/factory'
7
7
  require 'alephant/broker/request/handler'
8
+ require 'alephant/broker/request/multi'
8
9
 
9
10
  class NotFound; end
10
11
  class Status; end
@@ -8,26 +8,17 @@ module Alephant
8
8
  class Asset
9
9
  include Logger
10
10
 
11
- attr_reader :component
11
+ attr_accessor :component
12
12
 
13
- def initialize(env)
14
- logger.debug("Request::Asset#initialize(#{env.settings})")
15
- component_id = component_id_for env.path
13
+ def initialize(env = nil)
14
+ return if env.nil?
16
15
 
17
- @component = Component.new(
18
- component_id,
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
- def component_id_for(path)
30
- path.split('/')[2] || nil
21
+ @component = Component.new(component_id, nil, options)
31
22
  end
32
23
 
33
24
  end
@@ -9,6 +9,8 @@ module Alephant::Broker::Request
9
9
 
10
10
  def self.request_for(env)
11
11
  case request_type_from(env)
12
+ when 'multi'
13
+ Multi.new(env)
12
14
  when 'component'
13
15
  Asset.new(env)
14
16
  when 'components'
@@ -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
+
@@ -4,6 +4,7 @@ module Alephant
4
4
  require 'alephant/broker/response/base'
5
5
  require 'alephant/broker/response/asset'
6
6
  require 'alephant/broker/response/batch'
7
+ require 'alephant/broker/response/multi'
7
8
  require 'alephant/broker/response/factory'
8
9
 
9
10
  class NotFound < Base
@@ -15,12 +15,13 @@ module Alephant
15
15
  end
16
16
 
17
17
  def setup
18
+ loaded_content = load(component)
18
19
 
19
- result = load(component)
20
- @status = result['status']
21
- @content = result['body']
22
- @version = component.version.nil? ? 'not available' : component.version
23
- @cached = component.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 = status
18
- @content = STATUS_CODE_MAPPING[status]
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
- body = component.load
30
- status = 200
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 = "#{error_for(e)}"
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
- { 'body' => body.force_encoding('UTF-8'), 'status' => status }
58
+ log(component, data.status, e)
59
+ data.marshal_dump
41
60
  end
42
61
 
43
62
  def log(c, status, e = nil)
@@ -10,6 +10,8 @@ module Alephant
10
10
  Asset.new(request.component)
11
11
  when Request::Batch
12
12
  Batch.new(request.components, request.batch_id)
13
+ when Request::Multi
14
+ Multi.new(request.requests)
13
15
  when Request::Status
14
16
  Status.new
15
17
  else
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Broker
3
- VERSION = "1.0.5"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -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":{},"body":"Test","status":200},{"component":"ni_council_results_table","options":{},"body":"Test","status":200}]}'
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.5
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-05-23 00:00:00.000000000 Z
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