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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 418d71f5a4fd6cb910fb0546346079aad4110015
4
- data.tar.gz: 4a3fb9c10d3413aa36a0fd80f169e16f94edffa8
3
+ metadata.gz: 7f79aa0602f1011cba9b97d95accfe277a4dd490
4
+ data.tar.gz: 5876ee8664790e44ddf2ad1625d1f23a70d5ca2b
5
5
  SHA512:
6
- metadata.gz: 04bda5e541bd93a198d6c5feead5a6f42ed23b1c7c565dda9b3ea2c22d8d38cb10aac5bfcefb8e0e176378f24bd9201984577609f2be590bf3cdd47479d56bf9
7
- data.tar.gz: baf88f0d196b92e054cd1db2b743a85f0ab6c27e0b4c633c3a0782d0d693deaee5dd03b1ba4b71e7eb413fad6a8b8184e9f0a6cf46172f84aac1f9788946574f
6
+ metadata.gz: 882e8b92884111e731584518f83e267205f5ebc04dc7553b78edbd777c3a0a7380fb02723bb3fe91d0ad4bd6475feaf476c9c093a357ba310c76f209b79d08ca
7
+ data.tar.gz: 5e8c522c3d51700244ab570c351f2ff45c16eef471f19571ace0c738703d7729eb299630597d66da740f2e033d3ac8e732263a572bbfbbd98a64b7cbdcaffea1
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in alephant-preview.gemspec
4
4
  gemspec
data/Guardfile CHANGED
@@ -1,10 +1,10 @@
1
- guard "rake", :task => "spec" do
1
+ guard 'rake', task: 'spec' do
2
2
  watch(%r{^spec/.+_spec\.rb$})
3
3
  watch(%r{^lib/.+\.rb$})
4
- watch("spec/spec_helper.rb")
5
- watch("spec/fixtures/.+$")
4
+ watch('spec/spec_helper.rb')
5
+ watch('spec/fixtures/.+$')
6
6
  end
7
7
 
8
- guard "rake", :task => "integration" do
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__), "lib")
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
2
2
 
3
- require "rspec/core/rake_task"
4
- require "rake/rspec"
5
- require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+ require 'rake/rspec'
5
+ require 'bundler/gem_tasks'
6
6
 
7
- task :default => :all
7
+ task default: :all
@@ -1,17 +1,17 @@
1
1
  #!/usr/bin/env RUBYOPT=-Ku ruby
2
2
 
3
- require "pathname"
4
- require "trollop"
3
+ require 'pathname'
4
+ require 'trollop'
5
5
 
6
6
  root = Pathname.new(__FILE__).dirname.parent
7
- lib_path = (root + "lib").realdirpath
8
- current_dir = File.join(Dir.pwd, "lib")
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 "alephant/preview"
14
- require "alephant/preview/tasks"
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["alephant:preview:go"].invoke
34
- when "update"
35
- Rake::Task["alephant:preview:update"].invoke
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
@@ -1,6 +1,6 @@
1
- lib = File.expand_path("../..", __FILE__)
1
+ lib = File.expand_path('../..', __FILE__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
- require "alephant/preview/version"
5
- require "alephant/preview/server"
6
- require "alephant/preview/template"
4
+ require 'alephant/preview/version'
5
+ require 'alephant/preview/server'
6
+ require 'alephant/preview/template'
@@ -10,8 +10,8 @@ module Alephant
10
10
 
11
11
  def get(_uri)
12
12
  OpenStruct.new(
13
- :status => 200,
14
- :body => fixture
13
+ status: 200,
14
+ body: fixture
15
15
  )
16
16
  end
17
17
 
@@ -1,96 +1,98 @@
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"
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, "0.0.0.0"
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 "components/*/models/*.rb"
25
- also_reload "components/*/mapper.rb"
26
- also_reload "components/shared/mappers/*.rb"
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["Access-Control-Allow-Origin"] = "*"
33
+ response['Access-Control-Allow-Origin'] = '*'
32
34
  end
33
35
 
34
- get "/preview/:id/:template/:region/?:fixture?" do
35
- response["X-Sequence"] = sequence_id
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 "/component/:template/?:fixture?" do
41
- params["id"] = find_id_from_template params["template"]
42
- params["fixture"] = "responsive" unless params["fixture"]
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["X-Sequence"] = sequence_id
46
+ response['X-Sequence'] = sequence_id
45
47
 
46
48
  render_component
47
49
  end
48
50
 
49
- get "/component/:id/:template/?:fixture?" do
50
- response["X-Sequence"] = sequence_id
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 "/components/batch" do
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("options", {})
61
- params["template"] = component.fetch("component")
62
- params["id"] = find_id_from_template params["template"]
63
- params["fixture"] = options.fetch("fixture", "responsive") || "responsive"
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
- { :components => batch_components }.to_json
69
+ { components: batch_components }.to_json
68
70
  end
69
71
 
70
- post "/components/batch" do
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["template"] = component.fetch(:component)
76
- params["id"] = find_id_from_template params["template"]
77
- params["fixture"] = options.fetch(:fixture, "responsive") || "responsive"
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
- { :components => batch_components }.to_json
83
+ { components: batch_components }.to_json
82
84
  end
83
85
 
84
- get "/status" do
85
- "ok"
86
+ get '/status' do
87
+ 'ok'
86
88
  end
87
89
 
88
90
  not_found do
89
- "Not found"
91
+ 'Not found'
90
92
  end
91
93
 
92
94
  def find_id_from_template(template)
93
- files = Dir.glob(BASE_LOCATION + "/**/models/*")
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["Content-Type"] = get_content_type(content)
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
- :component => template,
120
- :options => {},
121
- :status => 200,
122
- :body => content,
123
- :content_type => get_content_type(content),
124
- :sequence_id => sequence_id
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 "application/json" if is_json?(content)
136
- "text/html"
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, :symbolize_names => true) || {}
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("components", [])
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, "models", "#{template}.rb")
173
+ File.join(base_path, 'models', "#{template}.rb")
172
174
  end
173
175
 
174
176
  def template
175
- params["template"]
177
+ params['template']
176
178
  end
177
179
 
178
180
  def region
179
- params["region"]
181
+ params['region']
180
182
  end
181
183
 
182
184
  def id
183
- params["id"]
185
+ params['id']
184
186
  end
185
187
 
186
188
  def fixture
187
- params["fixture"] || id
189
+ params['fixture'] || id
188
190
  end
189
191
 
190
192
  def fixture_data
@@ -1,7 +1,7 @@
1
- require "rake"
2
- require "pathname"
1
+ require 'rake'
2
+ require 'pathname'
3
3
 
4
4
  root = Pathname.new(__FILE__).dirname
5
- task_path = (root + "tasks").realdirpath
5
+ task_path = (root + 'tasks').realdirpath
6
6
 
7
7
  Dir["#{task_path}/**/*.rake"].each { |ext| load ext } if defined?(Rake)
@@ -1,5 +1,5 @@
1
- require "alephant/preview/server"
2
- require "alephant/preview/template"
1
+ require 'alephant/preview/server'
2
+ require 'alephant/preview/template'
3
3
 
4
4
  namespace :alephant do
5
5
  namespace :preview do
@@ -1,11 +1,11 @@
1
- require "alephant/preview/template/base"
2
- require "alephant/preview/template/updater"
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["PREVIEW_TEMPLATE_PATH"] || "#{Dir.pwd}/components/lib"
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 "mustache"
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["STATIC_HOST"] || "localhost:8000"
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 "faraday"
2
- require "uri"
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(:url => host).get(path)
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, "w") do |file|
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["PREVIEW_TEMPLATE_URL"]
33
- raise Exception.new("PREVIEW_TEMPLATE_URL is unset!") if uri_from_env.nil?
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, "{{{static_host}}}")
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["STATIC_HOST_REGEX"]
46
- raise Exception.new("STATIC_HOST_REGEX is unset!") if static_host_regex_from_env.nil?
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
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Preview
3
- VERSION = "0.5.3".freeze
3
+ VERSION = '0.6.0'.freeze
4
4
  end
5
5
  end
@@ -1,43 +1,43 @@
1
- require "spec_helper"
1
+ require 'spec_helper'
2
2
 
3
3
  describe Alephant::Preview::FixtureLoader do
4
- let (:fixtures_base) { File.join(File.dirname(__FILE__), "fixtures") }
5
- let (:base_path) { File.join(fixtures_base, "components", "bar") }
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 ".new" do
9
- context "using valid parameters" do
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 "#get" do
17
- let (:uri) { "/test/uri" }
16
+ describe '#get' do
17
+ let (:uri) { '/test/uri' }
18
18
 
19
- context "with a single fixture" do
20
- let (:fixture_data) { File.open(File.join(fixtures_base, "components", "bar", "fixtures", "bar.json")).read }
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 "with multiple fixtures" do
25
- let (:base_path) { File.join(fixtures_base, "components", "baz") }
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, "components", "baz", "fixtures", "*"))
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 "using a valid amount of fixtures" do
32
- it "should return each fixture on subsequent calls" do
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 "using incorrect amount of fixtures" do
40
- it "should raise an exception" do
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