alephant-preview 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Guardfile +4 -4
- data/Rakefile +5 -5
- data/bin/alephant-preview +9 -9
- data/lib/alephant/preview.rb +4 -4
- data/lib/alephant/preview/fixture_loader.rb +2 -2
- data/lib/alephant/preview/server.rb +62 -60
- data/lib/alephant/preview/tasks.rb +3 -3
- data/lib/alephant/preview/tasks/preview.rake +2 -2
- data/lib/alephant/preview/template.rb +3 -3
- data/lib/alephant/preview/template/base.rb +2 -2
- data/lib/alephant/preview/template/updater.rb +9 -9
- data/lib/alephant/preview/version.rb +1 -1
- data/spec/fixture_loader_spec.rb +16 -16
- data/spec/fixtures/components/bar/mapper.rb +1 -1
- data/spec/fixtures/components/bar/models/bar.rb +1 -1
- data/spec/fixtures/components/baz/mapper.rb +2 -2
- data/spec/fixtures/components/baz/models/baz.rb +1 -1
- data/spec/fixtures/components/foo/models/foo.rb +2 -2
- data/spec/integration/preview_spec.rb +121 -121
- data/spec/spec_helper.rb +8 -8
- metadata +42 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f79aa0602f1011cba9b97d95accfe277a4dd490
|
4
|
+
data.tar.gz: 5876ee8664790e44ddf2ad1625d1f23a70d5ca2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 882e8b92884111e731584518f83e267205f5ebc04dc7553b78edbd777c3a0a7380fb02723bb3fe91d0ad4bd6475feaf476c9c093a357ba310c76f209b79d08ca
|
7
|
+
data.tar.gz: 5e8c522c3d51700244ab570c351f2ff45c16eef471f19571ace0c738703d7729eb299630597d66da740f2e033d3ac8e732263a572bbfbbd98a64b7cbdcaffea1
|
data/Gemfile
CHANGED
data/Guardfile
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
guard
|
1
|
+
guard 'rake', task: 'spec' do
|
2
2
|
watch(%r{^spec/.+_spec\.rb$})
|
3
3
|
watch(%r{^lib/.+\.rb$})
|
4
|
-
watch(
|
5
|
-
watch(
|
4
|
+
watch('spec/spec_helper.rb')
|
5
|
+
watch('spec/fixtures/.+$')
|
6
6
|
end
|
7
7
|
|
8
|
-
guard
|
8
|
+
guard 'rake', task: 'integration' do
|
9
9
|
watch(%r{^spec/integration/.+_spec\.rb$})
|
10
10
|
end
|
data/Rakefile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
$LOAD_PATH.unshift File.join(File.dirname(__FILE__),
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
require 'rake/rspec'
|
5
|
+
require 'bundler/gem_tasks'
|
6
6
|
|
7
|
-
task :
|
7
|
+
task default: :all
|
data/bin/alephant-preview
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
#!/usr/bin/env RUBYOPT=-Ku ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'pathname'
|
4
|
+
require 'trollop'
|
5
5
|
|
6
6
|
root = Pathname.new(__FILE__).dirname.parent
|
7
|
-
lib_path = (root +
|
8
|
-
current_dir = File.join(Dir.pwd,
|
7
|
+
lib_path = (root + 'lib').realdirpath
|
8
|
+
current_dir = File.join(Dir.pwd, 'lib')
|
9
9
|
|
10
10
|
$LOAD_PATH.unshift(lib_path)
|
11
11
|
$LOAD_PATH.unshift(current_dir)
|
12
12
|
|
13
|
-
require
|
14
|
-
require
|
13
|
+
require 'alephant/preview'
|
14
|
+
require 'alephant/preview/tasks'
|
15
15
|
|
16
16
|
SUB_COMMANDS = %w(preview).freeze
|
17
17
|
global_opts = Trollop.options do
|
@@ -30,9 +30,9 @@ end
|
|
30
30
|
cmd = ARGV.shift # get the subcommand
|
31
31
|
case cmd
|
32
32
|
when nil
|
33
|
-
Rake::Task[
|
34
|
-
when
|
35
|
-
Rake::Task[
|
33
|
+
Rake::Task['alephant:preview:go'].invoke
|
34
|
+
when 'update'
|
35
|
+
Rake::Task['alephant:preview:update'].invoke
|
36
36
|
else
|
37
37
|
Trollop.die "unknown subcommand #{cmd.inspect}"
|
38
38
|
end
|
data/lib/alephant/preview.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
lib = File.expand_path(
|
1
|
+
lib = File.expand_path('../..', __FILE__)
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
4
|
+
require 'alephant/preview/version'
|
5
|
+
require 'alephant/preview/server'
|
6
|
+
require 'alephant/preview/template'
|
@@ -1,96 +1,98 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
1
|
+
require 'alephant/renderer/views/html'
|
2
|
+
require 'alephant/renderer/views/json'
|
3
|
+
require 'alephant/renderer/view_mapper'
|
4
|
+
require 'alephant/publisher/request/data_mapper_factory'
|
5
|
+
require 'alephant/publisher/request/data_mapper'
|
6
|
+
require 'alephant/publisher/request/error'
|
7
|
+
|
8
|
+
require 'alephant/support/parser'
|
9
|
+
require 'alephant/preview/fixture_loader'
|
10
|
+
require 'alephant/preview/template/base'
|
11
|
+
|
12
|
+
require 'sinatra/base'
|
13
|
+
require 'sinatra/reloader'
|
14
|
+
require 'faraday'
|
15
|
+
require 'json'
|
16
|
+
require 'uri'
|
17
17
|
|
18
18
|
module Alephant
|
19
19
|
module Preview
|
20
20
|
class Server < Sinatra::Base
|
21
|
-
set :bind,
|
21
|
+
set :bind, '0.0.0.0'
|
22
|
+
port = ENV['PORT'] || 4567
|
23
|
+
set :port, port
|
22
24
|
|
23
25
|
register Sinatra::Reloader
|
24
|
-
also_reload
|
25
|
-
also_reload
|
26
|
-
also_reload
|
26
|
+
also_reload 'components/*/models/*.rb'
|
27
|
+
also_reload 'components/*/mapper.rb'
|
28
|
+
also_reload 'components/shared/mappers/*.rb'
|
27
29
|
|
28
30
|
BASE_LOCATION = "#{(ENV['BASE_LOCATION'] || Dir.pwd)}/components".freeze
|
29
31
|
|
30
32
|
before do
|
31
|
-
response[
|
33
|
+
response['Access-Control-Allow-Origin'] = '*'
|
32
34
|
end
|
33
35
|
|
34
|
-
get
|
35
|
-
response[
|
36
|
+
get '/preview/:id/:template/:region/?:fixture?' do
|
37
|
+
response['X-Sequence'] = sequence_id
|
36
38
|
|
37
39
|
render_preview
|
38
40
|
end
|
39
41
|
|
40
|
-
get
|
41
|
-
params[
|
42
|
-
params[
|
42
|
+
get '/component/:template/?:fixture?' do
|
43
|
+
params['id'] = find_id_from_template params['template']
|
44
|
+
params['fixture'] = 'responsive' unless params['fixture']
|
43
45
|
|
44
|
-
response[
|
46
|
+
response['X-Sequence'] = sequence_id
|
45
47
|
|
46
48
|
render_component
|
47
49
|
end
|
48
50
|
|
49
|
-
get
|
50
|
-
response[
|
51
|
+
get '/component/:id/:template/?:fixture?' do
|
52
|
+
response['X-Sequence'] = sequence_id
|
51
53
|
|
52
54
|
render_component
|
53
55
|
end
|
54
56
|
|
55
|
-
get
|
57
|
+
get '/components/batch' do
|
56
58
|
batch_components = []
|
57
59
|
|
58
60
|
get_batched_components.each do |component|
|
59
61
|
component = component[1]
|
60
|
-
options = component.fetch(
|
61
|
-
params[
|
62
|
-
params[
|
63
|
-
params[
|
62
|
+
options = component.fetch('options', {})
|
63
|
+
params['template'] = component.fetch('component')
|
64
|
+
params['id'] = find_id_from_template params['template']
|
65
|
+
params['fixture'] = options.fetch('fixture', 'responsive') || 'responsive'
|
64
66
|
batch_components << render_batch_component
|
65
67
|
end
|
66
68
|
|
67
|
-
{ :
|
69
|
+
{ components: batch_components }.to_json
|
68
70
|
end
|
69
71
|
|
70
|
-
post
|
72
|
+
post '/components/batch' do
|
71
73
|
batch_components = []
|
72
74
|
|
73
75
|
post_batched_components.each do |component|
|
74
76
|
options = symbolize component.fetch(:options, {})
|
75
|
-
params[
|
76
|
-
params[
|
77
|
-
params[
|
77
|
+
params['template'] = component.fetch(:component)
|
78
|
+
params['id'] = find_id_from_template params['template']
|
79
|
+
params['fixture'] = options.fetch(:fixture, 'responsive') || 'responsive'
|
78
80
|
batch_components << render_batch_component
|
79
81
|
end
|
80
82
|
|
81
|
-
{ :
|
83
|
+
{ components: batch_components }.to_json
|
82
84
|
end
|
83
85
|
|
84
|
-
get
|
85
|
-
|
86
|
+
get '/status' do
|
87
|
+
'ok'
|
86
88
|
end
|
87
89
|
|
88
90
|
not_found do
|
89
|
-
|
91
|
+
'Not found'
|
90
92
|
end
|
91
93
|
|
92
94
|
def find_id_from_template(template)
|
93
|
-
files = Dir.glob(BASE_LOCATION +
|
95
|
+
files = Dir.glob(BASE_LOCATION + '/**/models/*')
|
94
96
|
file = files.select { |file| file.include? "/#{template}.rb" }.pop
|
95
97
|
|
96
98
|
halt(404) if file.nil?
|
@@ -108,7 +110,7 @@ module Alephant
|
|
108
110
|
|
109
111
|
def render_component
|
110
112
|
view_mapper.generate(fixture_data)[template].render.tap do |content|
|
111
|
-
response[
|
113
|
+
response['Content-Type'] = get_content_type(content)
|
112
114
|
end
|
113
115
|
end
|
114
116
|
|
@@ -116,12 +118,12 @@ module Alephant
|
|
116
118
|
content = render_component
|
117
119
|
|
118
120
|
{
|
119
|
-
:
|
120
|
-
:
|
121
|
-
:
|
122
|
-
:
|
123
|
-
:
|
124
|
-
:
|
121
|
+
component: template,
|
122
|
+
options: {},
|
123
|
+
status: 200,
|
124
|
+
body: content,
|
125
|
+
content_type: get_content_type(content),
|
126
|
+
sequence_id: sequence_id
|
125
127
|
}
|
126
128
|
end
|
127
129
|
|
@@ -132,8 +134,8 @@ module Alephant
|
|
132
134
|
end
|
133
135
|
|
134
136
|
def get_content_type(content)
|
135
|
-
return
|
136
|
-
|
137
|
+
return 'application/json' if is_json?(content)
|
138
|
+
'text/html'
|
137
139
|
end
|
138
140
|
|
139
141
|
def is_json?(content)
|
@@ -143,7 +145,7 @@ module Alephant
|
|
143
145
|
end
|
144
146
|
|
145
147
|
def request_body
|
146
|
-
JSON.parse(request.body.read, :
|
148
|
+
JSON.parse(request.body.read, symbolize_names: true) || {}
|
147
149
|
end
|
148
150
|
|
149
151
|
def query_string
|
@@ -155,7 +157,7 @@ module Alephant
|
|
155
157
|
end
|
156
158
|
|
157
159
|
def get_batched_components
|
158
|
-
query_string.fetch(
|
160
|
+
query_string.fetch('components', [])
|
159
161
|
end
|
160
162
|
|
161
163
|
def model
|
@@ -168,23 +170,23 @@ module Alephant
|
|
168
170
|
end
|
169
171
|
|
170
172
|
def model_location
|
171
|
-
File.join(base_path,
|
173
|
+
File.join(base_path, 'models', "#{template}.rb")
|
172
174
|
end
|
173
175
|
|
174
176
|
def template
|
175
|
-
params[
|
177
|
+
params['template']
|
176
178
|
end
|
177
179
|
|
178
180
|
def region
|
179
|
-
params[
|
181
|
+
params['region']
|
180
182
|
end
|
181
183
|
|
182
184
|
def id
|
183
|
-
params[
|
185
|
+
params['id']
|
184
186
|
end
|
185
187
|
|
186
188
|
def fixture
|
187
|
-
params[
|
189
|
+
params['fixture'] || id
|
188
190
|
end
|
189
191
|
|
190
192
|
def fixture_data
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'rake'
|
2
|
+
require 'pathname'
|
3
3
|
|
4
4
|
root = Pathname.new(__FILE__).dirname
|
5
|
-
task_path = (root +
|
5
|
+
task_path = (root + 'tasks').realdirpath
|
6
6
|
|
7
7
|
Dir["#{task_path}/**/*.rake"].each { |ext| load ext } if defined?(Rake)
|
@@ -1,11 +1,11 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'alephant/preview/template/base'
|
2
|
+
require 'alephant/preview/template/updater'
|
3
3
|
|
4
4
|
module Alephant
|
5
5
|
module Preview
|
6
6
|
module Template
|
7
7
|
def self.path
|
8
|
-
ENV[
|
8
|
+
ENV['PREVIEW_TEMPLATE_PATH'] || "#{Dir.pwd}/components/lib"
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.update(template_location)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'mustache'
|
2
2
|
|
3
3
|
module Alephant
|
4
4
|
module Preview
|
@@ -12,7 +12,7 @@ module Alephant
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def static_host
|
15
|
-
ENV[
|
15
|
+
ENV['STATIC_HOST'] || 'localhost:8000'
|
16
16
|
end
|
17
17
|
|
18
18
|
def method_missing(name, *args, &block)
|
@@ -1,19 +1,19 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'faraday'
|
2
|
+
require 'uri'
|
3
3
|
|
4
4
|
module Alephant
|
5
5
|
module Preview
|
6
6
|
module Template
|
7
7
|
class Updater
|
8
8
|
def template
|
9
|
-
response = Faraday.new(:
|
9
|
+
response = Faraday.new(url: host).get(path)
|
10
10
|
raise "Can't get template" if response.status != 200
|
11
11
|
|
12
12
|
apply_static_host_regex_to response.body
|
13
13
|
end
|
14
14
|
|
15
15
|
def update(template_location)
|
16
|
-
File.open(template_location,
|
16
|
+
File.open(template_location, 'w') do |file|
|
17
17
|
file.write(template)
|
18
18
|
end
|
19
19
|
end
|
@@ -29,21 +29,21 @@ module Alephant
|
|
29
29
|
def uri
|
30
30
|
return @uri unless @uri.nil?
|
31
31
|
|
32
|
-
uri_from_env = ENV[
|
33
|
-
raise Exception.new(
|
32
|
+
uri_from_env = ENV['PREVIEW_TEMPLATE_URL']
|
33
|
+
raise Exception.new('PREVIEW_TEMPLATE_URL is unset!') if uri_from_env.nil?
|
34
34
|
|
35
35
|
@uri = URI(uri_from_env)
|
36
36
|
end
|
37
37
|
|
38
38
|
def apply_static_host_regex_to(string)
|
39
|
-
string.gsub(static_host_regex,
|
39
|
+
string.gsub(static_host_regex, '{{{static_host}}}')
|
40
40
|
end
|
41
41
|
|
42
42
|
def static_host_regex
|
43
43
|
return @static_host_regex unless @static_host_regex.nil?
|
44
44
|
|
45
|
-
static_host_regex_from_env = ENV[
|
46
|
-
raise Exception.new(
|
45
|
+
static_host_regex_from_env = ENV['STATIC_HOST_REGEX']
|
46
|
+
raise Exception.new('STATIC_HOST_REGEX is unset!') if static_host_regex_from_env.nil?
|
47
47
|
|
48
48
|
@static_host_regex = Regexp.new(static_host_regex_from_env)
|
49
49
|
end
|
data/spec/fixture_loader_spec.rb
CHANGED
@@ -1,43 +1,43 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Alephant::Preview::FixtureLoader do
|
4
|
-
let (:fixtures_base) { File.join(File.dirname(__FILE__),
|
5
|
-
let (:base_path) { File.join(fixtures_base,
|
4
|
+
let (:fixtures_base) { File.join(File.dirname(__FILE__), 'fixtures') }
|
5
|
+
let (:base_path) { File.join(fixtures_base, 'components', 'bar') }
|
6
6
|
subject { described_class.new(base_path) }
|
7
7
|
|
8
|
-
describe
|
9
|
-
context
|
8
|
+
describe '.new' do
|
9
|
+
context 'using valid parameters' do
|
10
10
|
let (:expected) { described_class }
|
11
11
|
|
12
12
|
specify { expect(subject).to be_a expected }
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
describe
|
17
|
-
let (:uri) {
|
16
|
+
describe '#get' do
|
17
|
+
let (:uri) { '/test/uri' }
|
18
18
|
|
19
|
-
context
|
20
|
-
let (:fixture_data) { File.open(File.join(fixtures_base,
|
19
|
+
context 'with a single fixture' do
|
20
|
+
let (:fixture_data) { File.open(File.join(fixtures_base, 'components', 'bar', 'fixtures', 'bar.json')).read }
|
21
21
|
specify { expect(subject.get(uri).body).to eq fixture_data }
|
22
22
|
end
|
23
23
|
|
24
|
-
context
|
25
|
-
let (:base_path) { File.join(fixtures_base,
|
24
|
+
context 'with multiple fixtures' do
|
25
|
+
let (:base_path) { File.join(fixtures_base, 'components', 'baz') }
|
26
26
|
let (:fixture_data) do
|
27
|
-
fixtures = Dir.glob(File.join(fixtures_base,
|
27
|
+
fixtures = Dir.glob(File.join(fixtures_base, 'components', 'baz', 'fixtures', '*'))
|
28
28
|
fixtures.map { |fixture| File.open(fixture).read }
|
29
29
|
end
|
30
30
|
|
31
|
-
context
|
32
|
-
it
|
31
|
+
context 'using a valid amount of fixtures' do
|
32
|
+
it 'should return each fixture on subsequent calls' do
|
33
33
|
(0..2).each do |index|
|
34
34
|
expect(subject.get(uri).body).to eq fixture_data[index]
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
context
|
40
|
-
it
|
39
|
+
context 'using incorrect amount of fixtures' do
|
40
|
+
it 'should raise an exception' do
|
41
41
|
(0..2).each { |_index| subject.get(uri).body }
|
42
42
|
expect do
|
43
43
|
subject.get(uri).body
|