alephant 0.0.9.9.1-java → 0.1.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/alephant.gemspec +8 -2
  4. data/lib/alephant.rb +40 -56
  5. data/lib/alephant/models/jsonpath_lookup.rb +16 -0
  6. data/lib/alephant/models/parser.rb +20 -2
  7. data/lib/alephant/models/queue.rb +6 -5
  8. data/lib/alephant/models/render_mapper.rb +56 -0
  9. data/lib/alephant/models/writer.rb +44 -0
  10. data/lib/alephant/version.rb +1 -1
  11. data/lib/env.rb +0 -2
  12. data/spec/alephant_spec.rb +51 -197
  13. data/spec/fixtures/components/foo/models/bar.rb +7 -0
  14. data/spec/fixtures/components/foo/models/foo.rb +1 -1
  15. data/spec/parser_spec.rb +17 -9
  16. data/spec/render_mapper_spec.rb +63 -0
  17. data/spec/spec_helper.rb +1 -0
  18. data/spec/writer_spec.rb +52 -0
  19. metadata +97 -34
  20. data/bin/alephant +0 -37
  21. data/lib/alephant/errors.rb +0 -6
  22. data/lib/alephant/errors/invalid_view_path.rb +0 -6
  23. data/lib/alephant/errors/view_model_not_found.rb +0 -6
  24. data/lib/alephant/errors/view_template_not_found.rb +0 -6
  25. data/lib/alephant/models/cache.rb +0 -28
  26. data/lib/alephant/models/logger.rb +0 -22
  27. data/lib/alephant/models/multi_renderer.rb +0 -84
  28. data/lib/alephant/models/renderer.rb +0 -34
  29. data/lib/alephant/models/sequence_table.rb +0 -99
  30. data/lib/alephant/models/sequencer.rb +0 -61
  31. data/lib/alephant/preview/server.rb +0 -75
  32. data/lib/alephant/preview/template.rb +0 -63
  33. data/lib/alephant/tasks.rb +0 -8
  34. data/lib/alephant/util/string.rb +0 -9
  35. data/lib/alephant/views.rb +0 -15
  36. data/lib/alephant/views/base.rb +0 -16
  37. data/lib/alephant/views/preview.rb +0 -44
  38. data/lib/tasks/preview.rake +0 -16
  39. data/spec/cache_spec.rb +0 -63
  40. data/spec/logger_spec.rb +0 -40
  41. data/spec/multi_renderer_spec.rb +0 -92
  42. data/spec/renderer_spec.rb +0 -62
  43. data/spec/sequencer_spec.rb +0 -107
@@ -1,22 +0,0 @@
1
- require 'logger'
2
-
3
- module Alephant
4
- def self.logger
5
- ::Alephant::LogSystem.logger
6
- end
7
-
8
- def self.logger=(value)
9
- ::Alephant::LogSystem.logger = value
10
- end
11
-
12
- class LogSystem
13
- def self.logger
14
- @logger ||= Logger.new(STDOUT)
15
- end
16
-
17
- def self.logger=(value)
18
- @logger = value
19
- end
20
- end
21
- end
22
-
@@ -1,84 +0,0 @@
1
- module Alephant
2
- class MultiRenderer
3
- DEFAULT_LOCATION = 'components'
4
-
5
- def initialize(component_id, view_base_path=nil)
6
- self.base_path = "#{view_base_path}/#{component_id}" unless view_base_path.nil?
7
- @component_id = component_id
8
- @logger = ::Alephant.logger
9
- end
10
-
11
- def base_path
12
- @base_path || DEFAULT_LOCATION
13
- end
14
-
15
- def base_path=(path)
16
- @base_path = File.directory?(path) ? path : (raise Errors::InvalidViewPath)
17
- end
18
-
19
- def render(data)
20
- template_locations.reduce({}) do |obj, file|
21
- template_id = template_id_for file
22
- @logger.info("MultiRenderer.render: attempting to render #{template_id}")
23
-
24
- obj.tap do |o|
25
- o[template_id.to_sym] = render_template(
26
- template_id,
27
- data
28
- )
29
- end
30
- end
31
- end
32
-
33
- def render_template(template_file, data)
34
- @logger.info("MultiRenderer.render_template: rendering template #{template_file} (base_path: #{base_path})")
35
- renderer(
36
- template_file,
37
- base_path,
38
- data
39
- ).render
40
- end
41
-
42
- def renderer(template_file, base_path, data)
43
- Renderer.new(template_file, base_path, create_instance(template_file, data))
44
- end
45
-
46
- def create_instance(template_file, data)
47
- begin
48
- create_model(template_file, data)
49
- rescue Exception => e
50
- @logger.error("MultiRenderer.create_instance: exeception #{e.message}")
51
- raise Errors::ViewModelNotFound
52
- end
53
- end
54
-
55
- private
56
- def create_model(template_file, data)
57
- require model_location_for template_file
58
- klass = Views.get_registered_class("#{template_file}")
59
-
60
- @logger.info("MultiRenderer.create_model: creating new klass #{klass}")
61
- klass.new(data)
62
- end
63
-
64
- def model_location_for(template_file)
65
- @logger.info("MultiRenderer.model_location_for(#{template_file})")
66
- File.join(base_path, 'models', "#{template_file}.rb")
67
- end
68
-
69
- def template_locations
70
- @logger.info("MultiRenderer.template_locations: locating templates in #{template_base_path}")
71
- Dir[template_base_path]
72
- end
73
-
74
- def template_base_path
75
- "#{base_path}/templates/*"
76
- end
77
-
78
- def template_id_for(template_location)
79
- @logger.info("MultiRenderer.template_id_for(#{template_location})")
80
- template_location.split('/').last.sub(/\.mustache/, '')
81
- end
82
-
83
- end
84
- end
@@ -1,34 +0,0 @@
1
- require 'aws-sdk'
2
- require 'mustache'
3
-
4
- module Alephant
5
- class Renderer
6
- attr_reader :template_file
7
-
8
- def initialize(template_file, base_path, model)
9
- @logger = ::Alephant.logger
10
- @template_file = template_file
11
- @base_path = base_path
12
- @model = model
13
- @logger.info("Renderer.initialize: end with @base_path set to #{@base_path}")
14
- end
15
-
16
- def render
17
- @logger.info("Renderer.render: rendered template #{template_file}")
18
-
19
- Mustache.render(template, @model)
20
- end
21
-
22
- def template
23
- template_location = File.join(@base_path, 'templates', "#{template_file}.mustache")
24
-
25
- begin
26
- @logger.info("Renderer.template: #{template_location}")
27
- File.open(template_location).read
28
- rescue Exception => e
29
- @logger.error("Renderer.template: view template #{template_file} not found")
30
- raise Errors::ViewTemplateNotFound
31
- end
32
- end
33
- end
34
- end
@@ -1,99 +0,0 @@
1
- require 'aws-sdk'
2
- require 'thread'
3
- require 'timeout'
4
-
5
- module Alephant
6
- module Sequencer
7
- class SequenceTable
8
- attr_reader :table_name
9
-
10
- TIMEOUT = 120
11
- DEFAULT_CONFIG = {
12
- :write_units => 5,
13
- :read_units => 10,
14
- }
15
- SCHEMA = {
16
- :hash_key => {
17
- :key => :string,
18
- :value => :string
19
- }
20
- }
21
-
22
- def initialize(table_name, config = DEFAULT_CONFIG)
23
- @mutex = Mutex.new
24
- @dynamo_db = AWS::DynamoDB.new
25
- @table_name = table_name
26
- @config = config
27
- end
28
-
29
- def create
30
- @mutex.synchronize do
31
- ensure_table_exists
32
- ensure_table_active
33
- end
34
- end
35
-
36
- def table
37
- @table ||= @dynamo_db.tables[@table_name]
38
- end
39
-
40
- def sequence_for(ident)
41
- rows = batch_get_value_for(ident)
42
- rows.count >= 1 ? rows.first['value'].to_i : 0
43
- end
44
-
45
- def set_sequence_for(ident,value)
46
- @mutex.synchronize do
47
- AWS::DynamoDB::BatchWrite.new.tap { |batch|
48
- batch.put(
49
- table_name,
50
- [:key => ident,:value => value]
51
- )
52
- }.process!
53
- end
54
- end
55
-
56
- def delete_item!(ident)
57
- table.items[ident].delete
58
- end
59
-
60
- private
61
- def batch_get_value_for(ident)
62
- table.batch_get(['value'],[ident],batch_get_opts)
63
- end
64
-
65
- def batch_get_opts
66
- { :consistent_read => true }
67
- end
68
-
69
- def ensure_table_exists
70
- create_dynamodb_table unless table.exists?
71
- end
72
-
73
- def ensure_table_active
74
- sleep_until_table_active unless table_active?
75
- end
76
-
77
- def create_dynamodb_table
78
- @table = @dynamo_db.tables.create(
79
- @table_name,
80
- @config[:read_units],
81
- @config[:write_units],
82
- SCHEMA
83
- )
84
- end
85
-
86
- def table_active?
87
- table.status == :active
88
- end
89
-
90
- def sleep_until_table_active
91
- begin
92
- Timeout::timeout(TIMEOUT) do
93
- sleep 1 until table_active?
94
- end
95
- end
96
- end
97
- end
98
- end
99
- end
@@ -1,61 +0,0 @@
1
- require 'jsonpath'
2
-
3
- module Alephant
4
- module Sequencer
5
- @@sequence_tables = {}
6
-
7
- def self.create(table_name, ident, jsonpath = nil)
8
- @@sequence_tables[table_name] ||= SequenceTable.new(table_name)
9
- Sequencer.new(@@sequence_tables[table_name], ident, jsonpath)
10
- end
11
-
12
- class Sequencer
13
- attr_reader :ident, :jsonpath
14
-
15
- def initialize(sequence_table, id, sequence_path = nil)
16
- @mutex = Mutex.new
17
- @sequence_table = sequence_table
18
- @jsonpath = sequence_path
19
- @ident = id
20
-
21
- ::Alephant.logger.info("Sequencer.initialize: with id #{@ident}")
22
- @sequence_table.create
23
- end
24
-
25
- def sequential?(data)
26
- get_last_seen < sequence_id_from(data)
27
- end
28
-
29
- def delete!
30
- @sequence_table.delete_item!(ident)
31
- end
32
-
33
- def set_last_seen(data)
34
- last_seen_id = sequence_id_from(data)
35
-
36
- @sequence_table.set_sequence_for(ident, last_seen_id)
37
- ::Alephant.logger.info("Sequencer.set_last_seen: #{ident}:#{last_seen_id}")
38
- end
39
-
40
- def get_last_seen
41
- @sequence_table.sequence_for(ident)
42
- end
43
-
44
- private
45
- def sequence_id_from(data)
46
- jsonpath.nil? ?
47
- default_sequence_id_for(data) :
48
- sequence_from_jsonpath_for(data)
49
- end
50
-
51
- def sequence_from_jsonpath_for(data)
52
- JsonPath.on(data.body, jsonpath).first
53
- end
54
-
55
- def default_sequence_id_for(data)
56
- data.body['sequence_id'].to_i
57
- end
58
-
59
- end
60
- end
61
- end
@@ -1,75 +0,0 @@
1
- require 'sinatra/base'
2
- require 'alephant/models/parser'
3
- require 'alephant/models/multi_renderer'
4
- require 'alephant/views/preview'
5
- require 'faraday'
6
- require 'json'
7
- require 'uri'
8
-
9
- module Alephant
10
- module Preview
11
- class Server < Sinatra::Base
12
-
13
- get '/preview/:id/:template/:region/?:fixture?' do
14
- render_preview
15
- end
16
-
17
- get '/component/:id/:template/?:fixture?' do
18
- render_component
19
- end
20
-
21
- def render_preview
22
- ::Alephant::Views::Preview.new(
23
- { region => render_component },
24
- preview_template_location
25
- ).render
26
- end
27
-
28
- def render_component
29
- MultiRenderer.new(id, base_path).render_template(template, fixture_data)
30
- end
31
-
32
- private
33
-
34
- def template
35
- params['template']
36
- end
37
-
38
- def region
39
- params['region']
40
- end
41
-
42
- def id
43
- params['id']
44
- end
45
-
46
- def fixture
47
- params['fixture'] || id
48
- end
49
-
50
- def fixture_data
51
- parser.parse raw_fixture_data
52
- end
53
-
54
- def raw_fixture_data
55
- File.open(fixture_location).read
56
- end
57
-
58
- def parser
59
- @parser ||= Parser.new
60
- end
61
-
62
- def base_path
63
- "#{Dir.pwd}/components"
64
- end
65
-
66
- def fixture_location
67
- "#{base_path}/#{id}/fixtures/#{fixture}.json"
68
- end
69
-
70
- def preview_template_location
71
- "#{::Alephant::Preview.path}/templates/preview.mustache"
72
- end
73
- end
74
- end
75
- end
@@ -1,63 +0,0 @@
1
- require 'faraday'
2
- require 'uri'
3
-
4
- module Alephant
5
- module Preview
6
-
7
- def self.path
8
- "#{Dir.pwd}/components/lib"
9
- end
10
-
11
- class Template
12
-
13
- def self.update(template_location)
14
- self.new.update(template_location)
15
- end
16
-
17
- def template
18
- response = Faraday.new(:url => host).get(path)
19
- raise "Can't get template" if response.status != 200
20
-
21
- apply_static_host_regex_to response.body
22
- end
23
-
24
- def update(template_location)
25
- File.open(template_location, 'w') { |file|
26
- file.write(template)
27
- }
28
- end
29
-
30
- def host
31
- "#{uri.scheme}://#{uri.host}"
32
- end
33
-
34
- def path
35
- uri.path
36
- end
37
-
38
- def uri
39
- return @uri if not @uri.nil?
40
-
41
- uri_from_env = ENV['PREVIEW_TEMPLATE_URL']
42
- raise Exception.new("PREVIEW_TEMPLATE_URL is unset!") if uri_from_env.nil?
43
-
44
- @uri = URI(uri_from_env)
45
- end
46
-
47
- def apply_static_host_regex_to(string)
48
- string.gsub(static_host_regex, '{{{static_host}}}')
49
- end
50
-
51
- def static_host_regex
52
- return @static_host_regex if not @static_host_regex.nil?
53
-
54
- static_host_regex_from_env = ENV['STATIC_HOST_REGEX']
55
- raise Exception.new("STATIC_HOST_REGEX is unset!") if static_host_regex_from_env.nil?
56
-
57
- @static_host_regex = Regexp.new(static_host_regex_from_env)
58
- end
59
- end
60
- end
61
- end
62
-
63
-