alephant-broker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4990f7ae94eb498ef2fc00824325d839ca80cb1b
4
+ data.tar.gz: 8b7fe24b2c111717e7023675383943172d194ce2
5
+ SHA512:
6
+ metadata.gz: bfe0972285fd8270c402a313a761c313407ba10d8cca5b28481299f0c1ea96dc2494ac9abb898576868b74d1bce3a8290ed935dccd1b980a95536f844c6aa28c
7
+ data.tar.gz: 20ecc192a81a4a6e5e06240996537c0643bc03d74aa7e127f1611121d2eca43a51c82b96a403adc8d333755ba577f3bf26ca5aaf471bcc6fb1e53f50ea79674d
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.swp
2
+ Gemfile.lock
3
+ .rspec
4
+ *.gem
5
+
6
+ /pkg
7
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in alephant-broker.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard 'rspec' do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/.+\.rb$})
4
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
5
+ watch('spec/spec_helper.rb') { "spec" }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Steven Jack
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # Alephant::Broker
2
+
3
+ Brokers requests for rendered templates stored in S3
4
+
5
+ [![Build Status](https://travis-ci.org/BBC-News/alephant-broker.png?branch=master)](https://travis-ci.org/BBC-News/alephant-broker)
6
+
7
+ [![Gem Version](https://badge.fury.io/rb/alephant-broker.png)](http://badge.fury.io/rb/alephant-broker)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'alephant-broker'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install alephant-broker
22
+
23
+ ## Usage
24
+
25
+ ### Barebones
26
+
27
+ ```ruby
28
+ require 'alephant/broker'
29
+
30
+ request = Alephant::Broker::Request.new('/component/id', 'variant=hello')
31
+ config = {
32
+ :bucket_id => "s3-render-example",
33
+ :path => "foo",
34
+ :lookup_table_name => "example_lookup"
35
+ }
36
+
37
+ broker = Alephant::Broker.handle(request, config)
38
+
39
+ # => #<Alephant::Broker::Response:0x5215005d
40
+ # @content="<p>some HTML response</p>",
41
+ # @content_type="text/html",
42
+ # @status=200>
43
+ ```
44
+
45
+ ### Simple App
46
+
47
+ ```ruby
48
+ require 'alephant/broker/app'
49
+
50
+ config = {
51
+ :bucket_id => "s3-render-example",
52
+ :path => "foo",
53
+ :lookup_table_name => "example_lookup"
54
+ }
55
+
56
+ app = Alephant::Broker::Application.new(config)
57
+ request = app.request_from('/component/id', 'variant=hello')
58
+
59
+ app.handle(request)
60
+
61
+ # => #<Alephant::Broker::Response:0x5215005d
62
+ # @content="<p>some HTML response</p>",
63
+ # @content_type="text/html",
64
+ # @status=200>
65
+ ```
66
+
67
+ ### Rack
68
+
69
+ ```ruby
70
+ require 'alephant/broker/app/rack'
71
+ require 'configuration'
72
+
73
+ module Foo
74
+ class Bar < Alephant::Broker::RackApplication
75
+ def initialize
76
+ super(Configuration.new)
77
+ end
78
+ end
79
+ end
80
+ ```
81
+
82
+ ## Pry'ing
83
+
84
+ If you're using Pry to debug this gem...
85
+
86
+ ```ruby
87
+ export AWS_ACCESS_KEY_ID='xxxx'
88
+ export AWS_SECRET_ACCESS_KEY='xxxx'
89
+ export AWS_REGION='eu-west-1'
90
+
91
+ config = {
92
+ :bucket_id => "s3-render-example",
93
+ :path => "foo",
94
+ :lookup_table_name => "example_lookup"
95
+ }
96
+
97
+ env = {
98
+ "PATH_INFO" => "/component/england_council_header",
99
+ "QUERY_STRING" => ""
100
+ }
101
+
102
+ require 'alephant/broker/app/rack'
103
+
104
+ app = Alephant::Broker::RackApplication.new(config)
105
+ app.call(env)
106
+ ```
107
+
108
+ ## Contributing
109
+
110
+ 1. Fork it ( http://github.com/<my-github-username>/alephant-broker/fork )
111
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
112
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
113
+ 4. Push to the branch (`git push origin my-new-feature`)
114
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ $: << File.join(File.dirname(__FILE__),"..", "lib")
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'alephant/broker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "alephant-broker"
8
+ spec.version = Alephant::Broker::VERSION
9
+ spec.authors = ["Steven Jack"]
10
+ spec.email = ["stevenmajack@gmail.com"]
11
+ spec.summary = "Brokers requests for alephant components"
12
+ spec.description = "Brokers requests for alephant components"
13
+ spec.homepage = "https://github.com/BBC-News/alephant-broker"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rspec"
22
+ spec.add_development_dependency "rspec-nc"
23
+ spec.add_development_dependency "guard"
24
+ spec.add_development_dependency "guard-rspec"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "pry-remote"
27
+ spec.add_development_dependency "pry-nav"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.5"
30
+ spec.add_development_dependency "rake"
31
+
32
+ spec.add_runtime_dependency "alephant-lookup"
33
+ spec.add_runtime_dependency "alephant-cache"
34
+ end
@@ -0,0 +1,13 @@
1
+ require "alephant/broker/version"
2
+ require "alephant/broker/models/request_handler"
3
+
4
+ module Alephant
5
+ module Broker
6
+
7
+ def self.handle(request, config = {})
8
+ @@request_handler ||= RequestHandler.new(config)
9
+ @@request_handler.process(request)
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,23 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require 'alephant/broker'
4
+ require 'alephant/broker/models/request'
5
+
6
+ module Alephant
7
+ module Broker
8
+ class Application
9
+ def initialize(config)
10
+ @config = config
11
+ end
12
+
13
+ def handle(request)
14
+ Alephant::Broker.handle(request, @config)
15
+ end
16
+
17
+ def request_from(path, querystring)
18
+ Request.new(path, querystring)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ $: << File.dirname(__FILE__)
2
+
3
+ require 'alephant/broker/app'
4
+
5
+ module Alephant
6
+ module Broker
7
+ class RackApplication < Application
8
+
9
+ def call(env)
10
+ response = handle(
11
+ request_from(env['PATH_INFO'], env['QUERY_STRING'])
12
+ )
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
@@ -0,0 +1,7 @@
1
+ module Alephant
2
+ module Broker
3
+ class InvalidCacheKey < Exception
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,52 @@
1
+ require 'cgi'
2
+
3
+ module Alephant
4
+ module Broker
5
+ class Request
6
+ attr_reader :type, :component_id, :options, :extension, :content_type
7
+
8
+ DEFAULT_EXTENSION = :html
9
+
10
+ @@extension_mapping = {
11
+ :html => 'text/html',
12
+ :json => 'application/json'
13
+ }
14
+
15
+ def initialize(path, querystring)
16
+ request = request_components(path, querystring)
17
+ case request[:type]
18
+ when "component"
19
+ @type = :asset
20
+
21
+ @component_id = request[:component_id]
22
+ raise Errors::InvalidAssetId.new("No Asset ID specified") if @component_id.nil?
23
+
24
+ @options = request[:options]
25
+ @extension = request[:extension] || DEFAULT_EXTENSION
26
+ @content_type = @@extension_mapping[@extension.to_sym] || @@extension_mapping[DEFAULT_EXTENSION]
27
+ when "status"
28
+ @type = :status
29
+ else
30
+ @type = :notfound
31
+ end
32
+ end
33
+
34
+ def request_components(path, query_string)
35
+ request_parts = path.split('/')
36
+ {
37
+ :type => request_parts[1],
38
+ :component_id => (request_parts[2] || '').split('.')[0],
39
+ :extension => path.split('.')[1],
40
+ :options => options_from(query_string)
41
+ }
42
+ end
43
+
44
+ def options_from(query_string)
45
+ query_string.split('&').reduce({}) do |object, key_pair|
46
+ key, value = key_pair.split('=')
47
+ object.tap { |o| o.store(key, value) }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,22 @@
1
+ require 'alephant/broker/models/request'
2
+ require 'alephant/broker/models/response_factory'
3
+
4
+ module Alephant
5
+ module Broker
6
+ class RequestHandler
7
+
8
+ def initialize(config)
9
+ @response_factory = ResponseFactory.new(config)
10
+ end
11
+
12
+ def process(request)
13
+ begin
14
+ @response_factory.response_from(request)
15
+ rescue Exception => e
16
+ @response_factory.response(500)
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
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
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ require 'alephant/cache'
2
+ require 'alephant/lookup'
3
+ require 'alephant/broker/errors/invalid_cache_key'
4
+
5
+ module Alephant
6
+ module Broker
7
+ class AssetResponse < Response
8
+
9
+ attr_reader :request
10
+
11
+ def initialize(request, config)
12
+ @request = request
13
+ @lookup = Alephant::Lookup.create(config[:lookup_table_name], request.component_id)
14
+ @cache = Cache.new(config[:bucket_id], config[:path])
15
+ super()
16
+ end
17
+
18
+ def setup
19
+ begin
20
+ self.content_type = request.content_type
21
+ self.content = @cache.get cache_id
22
+ rescue AWS::S3::Errors::NoSuchKey, InvalidCacheKey
23
+ self.status = 404
24
+ rescue Exception => e
25
+ self.status = 500
26
+ end
27
+
28
+ end
29
+
30
+ private
31
+
32
+ def cache_id
33
+ @lookup.read(request.options).tap { |cache_id| raise InvalidCacheKey if cache_id.nil? }
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+
40
+
@@ -0,0 +1,33 @@
1
+ require 'alephant/broker/models/response'
2
+ require 'alephant/broker/models/response/asset_response'
3
+
4
+ module Alephant
5
+ module Broker
6
+ class ResponseFactory
7
+
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+
12
+ def response_from(request)
13
+
14
+ case request.type
15
+ when :asset
16
+ AssetResponse.new(request, @config)
17
+ when :status
18
+ response(200)
19
+ when :notfound
20
+ response(404)
21
+ when :error
22
+ response(500)
23
+ end
24
+
25
+ end
26
+
27
+ def response(status)
28
+ Response.new(status)
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module Alephant
2
+ module Broker
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ $: << File.join(File.dirname(__FILE__),"..", "lib")
2
+
3
+ require 'alephant/broker'
4
+
5
+
metadata ADDED
@@ -0,0 +1,217 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alephant-broker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Steven Jack
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ prerelease: false
26
+ type: :development
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-nc
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ prerelease: false
40
+ type: :development
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ prerelease: false
54
+ type: :development
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ prerelease: false
68
+ type: :development
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirement: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ prerelease: false
82
+ type: :development
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-remote
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirement: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ prerelease: false
96
+ type: :development
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-nav
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ prerelease: false
110
+ type: :development
111
+ - !ruby/object:Gem::Dependency
112
+ name: bundler
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '1.5'
118
+ requirement: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ~>
121
+ - !ruby/object:Gem::Version
122
+ version: '1.5'
123
+ prerelease: false
124
+ type: :development
125
+ - !ruby/object:Gem::Dependency
126
+ name: rake
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirement: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ prerelease: false
138
+ type: :development
139
+ - !ruby/object:Gem::Dependency
140
+ name: alephant-lookup
141
+ version_requirements: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirement: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ prerelease: false
152
+ type: :runtime
153
+ - !ruby/object:Gem::Dependency
154
+ name: alephant-cache
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirement: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ prerelease: false
166
+ type: :runtime
167
+ description: Brokers requests for alephant components
168
+ email:
169
+ - stevenmajack@gmail.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - .gitignore
175
+ - Gemfile
176
+ - Guardfile
177
+ - LICENSE.txt
178
+ - README.md
179
+ - Rakefile
180
+ - alephant-broker.gemspec
181
+ - lib/alephant/broker.rb
182
+ - lib/alephant/broker/app.rb
183
+ - lib/alephant/broker/app/rack.rb
184
+ - lib/alephant/broker/errors/invalid_cache_key.rb
185
+ - lib/alephant/broker/models/request.rb
186
+ - lib/alephant/broker/models/request_handler.rb
187
+ - lib/alephant/broker/models/response.rb
188
+ - lib/alephant/broker/models/response/asset_response.rb
189
+ - lib/alephant/broker/models/response_factory.rb
190
+ - lib/alephant/broker/version.rb
191
+ - spec/spec_helper.rb
192
+ homepage: https://github.com/BBC-News/alephant-broker
193
+ licenses:
194
+ - MIT
195
+ metadata: {}
196
+ post_install_message:
197
+ rdoc_options: []
198
+ require_paths:
199
+ - lib
200
+ required_ruby_version: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - '>='
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ required_rubygems_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - '>='
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
210
+ requirements: []
211
+ rubyforge_project:
212
+ rubygems_version: 2.1.9
213
+ signing_key:
214
+ specification_version: 4
215
+ summary: Brokers requests for alephant components
216
+ test_files:
217
+ - spec/spec_helper.rb