alephant 0.0.7-java → 0.0.8-java

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: df75578b70f5f4edee18315d47acdd5b446b319b
4
- data.tar.gz: 7c21430e5949075f3fb2ea9db699a117a913ac77
3
+ metadata.gz: 494ed4c851d3825b798c74433d330325377d6d85
4
+ data.tar.gz: f6ab02a83752f07bda53b7cf4c5cdfc5c75ecf92
5
5
  SHA512:
6
- metadata.gz: df985a135a85c1fd05f060c897c1e3408b3531d930cc2473862d06be1af3843ee7d54577e085fae4e2276a2abb2c3392d424a3a6308d7856c7125f82ec1ccdfb
7
- data.tar.gz: 0ef61361d4139f8199437adee01356386c37553f261d9b129e262b886cf3cb28622007e0450d4d10207d3b2da628dd61e91ba393191349ff13caec6857c35659
6
+ metadata.gz: c8e671fae65d072ace104187c3c38c41e1dea2d84a74cec074a08c51c48de552c77e1a168e7dfc376d984f1159a46382ce4313e073ad68db95f49468a06fb2a4
7
+ data.tar.gz: a71fd8c93acd6757e44877b34db95f41431adcc36eda26b0eb9d388a811e9307ea878f23b7b669c76ef0a806bd2b32deb463d68c442b240693c72138443a431e
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ /config/*.yaml
1
2
  /config/*.yml
2
3
  Gemfile.lock
3
4
  .rspec
data/README.md CHANGED
@@ -22,17 +22,20 @@ Static publishing to S3 on push notification from SQS
22
22
  ##Setup
23
23
 
24
24
  Ensure you have a `config/aws.yml` in the format:
25
+
25
26
  ```yaml
26
27
  access_key_id: ACCESS_KEY_ID
27
28
  secret_access_key: SECRET_ACCESS_KEY
28
29
  ```
29
30
 
30
31
  Install the gem:
32
+
31
33
  ```sh
32
34
  gem install alephant
33
35
  ```
34
36
 
35
37
  In your application:
38
+
36
39
  ```rb
37
40
  require 'alephant'
38
41
  opts = {
@@ -58,12 +61,14 @@ thread.join
58
61
 
59
62
  logger is optional, and must confirm to the Ruby standard logger interface
60
63
 
61
- Provide a view in a folder:
64
+ Provide a view in a folder (fixtures are optional):
62
65
 
63
66
  ```
64
67
  └── views
65
68
  ├── models
66
69
  │   └── foo.rb
70
+ ├── fixtures
71
+ │   └── foo.json
67
72
  └── templates
68
73
  └── foo.mustache
69
74
  ```
@@ -78,6 +83,7 @@ Provide a view in a folder:
78
83
  ```
79
84
 
80
85
  **foo.rb**
86
+
81
87
  ```rb
82
88
  module MyApp
83
89
  module Views
@@ -91,12 +97,49 @@ end
91
97
  ```
92
98
 
93
99
  **foo.mustache**
100
+
94
101
  ```mustache
95
102
  {{content}}
96
103
  ```
97
104
 
98
105
  **S3 Output**
106
+
99
107
  ```
100
108
  hello world
101
109
  ```
102
110
 
111
+ ###Preview Server
112
+
113
+ `alephant preview`
114
+
115
+ The included preview server allows you to see the html generated by your
116
+ templates, both standalone and in the context of a page.
117
+
118
+ **Standalone**
119
+
120
+ `/component/:id/?:fixture?`
121
+
122
+ ####Full page preview
123
+
124
+ When viewing the component in the context of a page, you'll need to retrieve a
125
+ mustache template to provide the page context.
126
+
127
+ When performing an update a regex is applied to replace the static hostnames in
128
+ the retrieved html.
129
+
130
+ **Environment Variables**
131
+
132
+ ```sh
133
+ STATIC_HOST_REGEX="static.(sandbox.dev|int|test|stage|live).yourapp(i)?.com\/"
134
+ PREVIEW_TEMPLATE_URL="http://yourapp.com/template"
135
+ ```
136
+
137
+ **Example Remote Template**
138
+
139
+ [http://m.int.bbc.co.uk/news/layout/template](http://m.int.bbc.co.uk/news/layout/template)
140
+
141
+ `alephant update`
142
+
143
+ **In page**
144
+ `/preview/:id/:region/?:fixture?`
145
+
@@ -5,23 +5,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'alephant/version'
6
6
 
7
7
  Gem::Specification.new do |s|
8
- s.name = 'alephant'
9
- s.version = Alephant::VERSION
10
- s.date = Time.now.strftime "%Y-%m-%d"
11
- s.summary = "Static Publishing in the Cloud"
12
- s.description = "Static publishing to S3 based on SQS messages"
13
- s.authors = ["Robert Kenny"]
14
- s.email = 'kenoir@gmail.com'
15
- s.license = 'GPLv3'
8
+ s.name = 'alephant'
9
+ s.version = Alephant::VERSION
10
+ s.date = Time.now.strftime "%Y-%m-%d"
11
+ s.summary = "Static Publishing in the Cloud"
12
+ s.description = "Static publishing to S3 based on SQS messages"
13
+ s.authors = ["Robert Kenny"]
14
+ s.email = 'kenoir@gmail.com'
15
+ s.license = 'GPLv3'
16
16
 
17
- s.files = `git ls-files`.split($/)
18
- s.platform = "java"
19
- s.homepage =
20
- 'http://rubygems.org/gems/alephant'
17
+ s.files = `git ls-files`.split($/)
18
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ s.platform = "java"
20
+ s.homepage = 'https://github.com/BBC-News/alephant'
21
21
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
22
22
  s.require_paths = ["lib"]
23
23
 
24
- s.add_development_dependency "rake"
25
24
  s.add_development_dependency "rspec"
26
25
  s.add_development_dependency "rspec-nc"
27
26
  s.add_development_dependency "guard"
@@ -29,7 +28,12 @@ Gem::Specification.new do |s|
29
28
  s.add_development_dependency "pry"
30
29
  s.add_development_dependency "pry-remote"
31
30
  s.add_development_dependency "pry-nav"
31
+ s.add_development_dependency "sinatra"
32
32
 
33
+ s.add_runtime_dependency 'faraday'
34
+ s.add_runtime_dependency 'trollop'
35
+ s.add_runtime_dependency 'rake'
33
36
  s.add_runtime_dependency 'aws-sdk', '~> 1.0'
34
37
  s.add_runtime_dependency 'mustache', '>= 0.99.5'
35
38
  end
39
+
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+ require 'trollop'
5
+
6
+ root = Pathname.new(__FILE__).dirname.parent
7
+ lib_path = (root + 'lib').realdirpath
8
+
9
+ $LOAD_PATH.unshift(lib_path)
10
+
11
+ require 'alephant'
12
+ require 'alephant/tasks'
13
+
14
+ SUB_COMMANDS = %w(preview)
15
+ global_opts = Trollop::options do
16
+ banner <<-EOS
17
+ Static publishing to S3 based on SQS messages
18
+ Usage:
19
+ alephant preview # Runs preview server
20
+ alephant update # Attempts to update preview template
21
+ Dependent on the following environmen variables being set:
22
+ - STATIC_HOST_REGEX="static.(int|test|live).myhost.com"
23
+ - PREVIEW_TEMPLATE_URL"http://myapp.com/mustache_template"
24
+ EOS
25
+ stop_on SUB_COMMANDS
26
+ end
27
+
28
+ cmd = ARGV.shift # get the subcommand
29
+ case cmd
30
+ when "preview"
31
+ Rake::Task['alephant:preview:go'].invoke
32
+ when "update"
33
+ Rake::Task['alephant:preview:update'].invoke
34
+ else
35
+ Trollop::die "unknown subcommand #{cmd.inspect}"
36
+ end
37
+
@@ -34,6 +34,7 @@ module Alephant
34
34
  set_logger(logger)
35
35
  set_opts(opts)
36
36
 
37
+ @logger = ::Alephant.logger
37
38
  @sequencer = Sequencer.new(
38
39
  {
39
40
  :table_name => @table_name
@@ -51,7 +52,7 @@ module Alephant
51
52
  end
52
53
 
53
54
  def parse(msg)
54
- JSON.parse(msg)
55
+ JSON.parse(msg, :symbolize_names => true)
55
56
  end
56
57
 
57
58
  def write(data)
@@ -64,9 +65,13 @@ module Alephant
64
65
  def receive(msg)
65
66
  data = parse(msg.body)
66
67
 
68
+ @logger.info("Alephant.receive: with id #{msg.id} and body digest: #{msg.md5}")
69
+
67
70
  if @sequencer.sequential?(data, &@sequential_proc)
68
71
  write data
69
72
  @sequencer.set_last_seen(data, &@set_last_seen_proc)
73
+ else
74
+ @logger.warn("Alephant.receive: out of sequence message received #{msg.id} (discarded)")
70
75
  end
71
76
  end
72
77
 
@@ -16,7 +16,7 @@ module Alephant
16
16
 
17
17
  def put(id, data)
18
18
  @bucket.objects["#{@path}/#{id}"].write(data)
19
- @logger.info("Cache.put: #{@path}/#{id} and write data #{data}")
19
+ @logger.info("Cache.put: #{@path}/#{id}")
20
20
  end
21
21
 
22
22
  def get(id)
@@ -17,7 +17,7 @@ module Alephant
17
17
  end
18
18
 
19
19
  def render(data)
20
- @logger.info("Renderer.render: rendered template with id #{id} and data #{data}")
20
+ @logger.info("Renderer.render: rendered template with id #{id}")
21
21
  Mustache.render(
22
22
  template(@id),
23
23
  model(@id,data)
@@ -73,8 +73,9 @@ module Alephant
73
73
  :consistent_read => true
74
74
  }
75
75
  ).first["value"].to_i
76
- rescue
77
- @logger.error("Sequencer.get_last_seen: id #{id}")
76
+ rescue Exception => e
77
+ trace = e.backtrace.join('\n')
78
+ @logger.error("Sequencer.get_last_seen: id #{id}\nmessage: #{e.message}\ntrace: #{trace}")
78
79
  0
79
80
  end
80
81
  end
@@ -0,0 +1,61 @@
1
+ require 'sinatra/base'
2
+ require 'alephant/models/renderer'
3
+ require 'alephant/views/preview'
4
+ require 'faraday'
5
+ require 'json'
6
+ require 'uri'
7
+
8
+ module Alephant
9
+ module Preview
10
+ class Server < Sinatra::Base
11
+
12
+ get '/preview/:id/:region/?:fixture?' do
13
+ render_preview
14
+ end
15
+
16
+ get '/component/:id/?:fixture?' do
17
+ render_component
18
+ end
19
+
20
+ def render_preview
21
+ ::Alephant::Views::Preview.new(
22
+ { region => render_component },
23
+ preview_template_location
24
+ ).render
25
+ end
26
+
27
+ def render_component
28
+ Renderer.new(id, base_path).render(fixture_data)
29
+ end
30
+
31
+ private
32
+ def region
33
+ params['region']
34
+ end
35
+
36
+ def id
37
+ params['id']
38
+ end
39
+
40
+ def fixture
41
+ params['fixture'] || id
42
+ end
43
+
44
+ def fixture_data
45
+ JSON.parse(File.open(fixture_location).read)
46
+ end
47
+
48
+ def base_path
49
+ "#{Dir.pwd}/views"
50
+ end
51
+
52
+ def fixture_location
53
+ "#{base_path}/fixtures/#{fixture}.json"
54
+ end
55
+
56
+ def preview_template_location
57
+ "#{base_path}/templates/preview.mustache"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,58 @@
1
+ require 'faraday'
2
+ require 'uri'
3
+
4
+ module Alephant
5
+ module Preview
6
+ class Template
7
+
8
+ def self.update(template_location)
9
+ self.new.update(template_location)
10
+ end
11
+
12
+ def template
13
+ response = Faraday.new(:url => host).get(path)
14
+ raise "Can't get template" if response.status != 200
15
+
16
+ apply_static_host_regex_to response.body
17
+ end
18
+
19
+ def update(template_location)
20
+ File.open(template_location, 'w') { |file|
21
+ file.write(template)
22
+ }
23
+ end
24
+
25
+ def host
26
+ "#{uri.scheme}://#{uri.host}"
27
+ end
28
+
29
+ def path
30
+ uri.path
31
+ end
32
+
33
+ def uri
34
+ return @uri if not @uri.nil?
35
+
36
+ uri_from_env = ENV['PREVIEW_TEMPLATE_URL']
37
+ raise Exception.new("PREVIEW_TEMPLATE_URL is unset!") if uri_from_env.nil?
38
+
39
+ @uri = URI(uri_from_env)
40
+ end
41
+
42
+ def apply_static_host_regex_to(string)
43
+ string.gsub(static_host_regex, '{{{static_host}}}')
44
+ end
45
+
46
+ def static_host_regex
47
+ return @static_host_regex if not @static_host_regex.nil?
48
+
49
+ static_host_regex_from_env = ENV['STATIC_HOST_REGEX']
50
+ raise Exception.new("STATIC_HOST_REGEX is unset!") if static_host_regex_from_env.nil?
51
+
52
+ @static_host_regex = Regexp.new(static_host_regex_from_env)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+ require 'pathname'
3
+
4
+ root = Pathname.new(__FILE__).dirname.parent
5
+ task_path = (root + 'tasks').realdirpath
6
+
7
+ Dir["#{task_path}/**/*.rake"].each { |ext| load ext } if defined?(Rake)
8
+
@@ -1,3 +1,3 @@
1
1
  module Alephant
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,44 @@
1
+ require 'alephant/views/base'
2
+
3
+ module Alephant
4
+ module Views
5
+ class Preview < Mustache
6
+ attr_accessor :regions
7
+
8
+ def initialize(regions, template_location)
9
+ @regions=regions
10
+ self.template_file = template_location
11
+ end
12
+
13
+ def static_host
14
+ 'localhost:8000'
15
+ end
16
+
17
+ def method_missing(name, *args, &block)
18
+ return super unless respond_to? name.to_s
19
+ region @regions[name.to_s]
20
+ end
21
+
22
+ def respond_to?(method)
23
+ valid_regions.include? method.to_s
24
+ end
25
+
26
+ def region(components)
27
+ if components.kind_of?(Array)
28
+ components.join
29
+ else
30
+ components
31
+ end
32
+ end
33
+
34
+ def valid_regions
35
+ self.template.tokens.find_all { |token|
36
+ token.is_a?(Array) && token[0] == :mustache
37
+ }.map{ |token|
38
+ token[2][2][0].to_s
39
+ }
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,16 @@
1
+ require 'alephant/preview/server'
2
+ require 'alephant/preview/template'
3
+
4
+ namespace :alephant do
5
+ namespace :preview do
6
+ task :go do
7
+ Alephant::Preview::Server.run!
8
+ end
9
+ task :update do
10
+ Alephant::Preview::Template.update(
11
+ "#{Dir.pwd}/views/templates/preview.mustache"
12
+ )
13
+ end
14
+ end
15
+ end
16
+
@@ -164,7 +164,10 @@ describe Alephant::Alephant do
164
164
 
165
165
  it "writes data to cache if sequential order is true" do
166
166
  data = "{ \"foo\":\"bar\" }"
167
+
167
168
  msg = double()
169
+ msg.stub(:id).and_return(:id)
170
+ msg.stub(:md5).and_return(:md5)
168
171
  msg.stub(:body).and_return(data)
169
172
 
170
173
  instance = subject.new
@@ -172,11 +175,35 @@ describe Alephant::Alephant do
172
175
  Alephant::Sequencer.any_instance.stub(:sequential?).and_return(true)
173
176
  Alephant::Sequencer.any_instance.stub(:set_last_seen)
174
177
 
175
- instance.should_receive(:write).with(JSON.parse(data))
178
+ instance.should_receive(:write).with(JSON.parse(data, :symbolize_names => true))
176
179
  instance.receive(msg)
177
180
  end
178
181
  end
179
182
 
183
+ describe "parse(msg)" do
184
+ before(:each) do
185
+ sequencer = double()
186
+ queue = double()
187
+ cache = double()
188
+ renderer = double()
189
+
190
+ Alephant::Sequencer.any_instance.stub(:initialize).and_return(sequencer)
191
+ Alephant::Queue.any_instance.stub(:initialize).and_return(queue)
192
+ Alephant::Cache.any_instance.stub(:initialize).and_return(cache)
193
+ Alephant::Renderer.any_instance.stub(:initialize).and_return(renderer)
194
+ end
195
+
196
+ it "should return keys as symbols" do
197
+ data = "{ \"foo\":\"bar\" }"
198
+
199
+ instance = subject.new.parse data
200
+ key = instance.keys[0]
201
+
202
+ key.should be_a Symbol
203
+ instance[key].should eq 'bar'
204
+ end
205
+ end
206
+
180
207
  describe "write(data)" do
181
208
  before(:each) do
182
209
  sequencer = double()
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: java
6
6
  authors:
7
7
  - Robert Kenny
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-26 00:00:00.000000000 Z
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rake
14
+ name: rspec
15
15
  version_requirements: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - '>='
@@ -25,7 +25,7 @@ dependencies:
25
25
  prerelease: false
26
26
  type: :development
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: rspec-nc
29
29
  version_requirements: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='
@@ -39,7 +39,7 @@ dependencies:
39
39
  prerelease: false
40
40
  type: :development
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec-nc
42
+ name: guard
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - '>='
@@ -53,7 +53,7 @@ dependencies:
53
53
  prerelease: false
54
54
  type: :development
55
55
  - !ruby/object:Gem::Dependency
56
- name: guard
56
+ name: guard-rspec
57
57
  version_requirements: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '>='
@@ -67,7 +67,7 @@ dependencies:
67
67
  prerelease: false
68
68
  type: :development
69
69
  - !ruby/object:Gem::Dependency
70
- name: guard-rspec
70
+ name: pry
71
71
  version_requirements: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - '>='
@@ -81,7 +81,7 @@ dependencies:
81
81
  prerelease: false
82
82
  type: :development
83
83
  - !ruby/object:Gem::Dependency
84
- name: pry
84
+ name: pry-remote
85
85
  version_requirements: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - '>='
@@ -95,7 +95,7 @@ dependencies:
95
95
  prerelease: false
96
96
  type: :development
97
97
  - !ruby/object:Gem::Dependency
98
- name: pry-remote
98
+ name: pry-nav
99
99
  version_requirements: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - '>='
@@ -109,7 +109,7 @@ dependencies:
109
109
  prerelease: false
110
110
  type: :development
111
111
  - !ruby/object:Gem::Dependency
112
- name: pry-nav
112
+ name: sinatra
113
113
  version_requirements: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - '>='
@@ -122,6 +122,48 @@ dependencies:
122
122
  version: '0'
123
123
  prerelease: false
124
124
  type: :development
125
+ - !ruby/object:Gem::Dependency
126
+ name: faraday
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: :runtime
139
+ - !ruby/object:Gem::Dependency
140
+ name: trollop
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: rake
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
125
167
  - !ruby/object:Gem::Dependency
126
168
  name: aws-sdk
127
169
  version_requirements: !ruby/object:Gem::Requirement
@@ -152,7 +194,8 @@ dependencies:
152
194
  type: :runtime
153
195
  description: Static publishing to S3 based on SQS messages
154
196
  email: kenoir@gmail.com
155
- executables: []
197
+ executables:
198
+ - alephant
156
199
  extensions: []
157
200
  extra_rdoc_files: []
158
201
  files:
@@ -165,6 +208,7 @@ files:
165
208
  - README.md
166
209
  - Rakefile
167
210
  - alephant.gemspec
211
+ - bin/alephant
168
212
  - lib/alephant.rb
169
213
  - lib/alephant/errors.rb
170
214
  - lib/alephant/errors/invalid_view_path.rb
@@ -175,11 +219,16 @@ files:
175
219
  - lib/alephant/models/queue.rb
176
220
  - lib/alephant/models/renderer.rb
177
221
  - lib/alephant/models/sequencer.rb
222
+ - lib/alephant/preview/server.rb
223
+ - lib/alephant/preview/template.rb
224
+ - lib/alephant/tasks.rb
178
225
  - lib/alephant/util/string.rb
179
226
  - lib/alephant/version.rb
180
227
  - lib/alephant/views.rb
181
228
  - lib/alephant/views/base.rb
229
+ - lib/alephant/views/preview.rb
182
230
  - lib/env.rb
231
+ - lib/tasks/preview.rake
183
232
  - spec/alephant_spec.rb
184
233
  - spec/cache_spec.rb
185
234
  - spec/fixtures/views/models/example.rb
@@ -189,7 +238,7 @@ files:
189
238
  - spec/renderer_spec.rb
190
239
  - spec/sequencer_spec.rb
191
240
  - spec/spec_helper.rb
192
- homepage: http://rubygems.org/gems/alephant
241
+ homepage: https://github.com/BBC-News/alephant
193
242
  licenses:
194
243
  - GPLv3
195
244
  metadata: {}