js-fixtures 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+
2
+ module JsFixtures
3
+ PATH = File.join(File.dirname(__FILE__), "js_fixtures")
4
+ require "#{PATH}/s3_upload.rb"
5
+ require "#{PATH}/base.rb"
6
+ require "#{PATH}/html.rb"
7
+ require "#{PATH}/html_s3.rb"
8
+
9
+ class << self
10
+ def create(name, config, &block)
11
+ Base.create(name, config, &block)
12
+ end
13
+
14
+ def get_location(name)
15
+ Base.get(name)
16
+ end
17
+
18
+ def generate_all
19
+ Base.generate_all
20
+ end
21
+
22
+ def config(type, &block)
23
+ Base.config(type, &block)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,66 @@
1
+ module JsFixtures
2
+ class Base
3
+ @@fixtures = []
4
+ @@fixture_types = []
5
+
6
+ class << self
7
+ def config(type, &block)
8
+ block.call(get_by_type(type))
9
+ end
10
+
11
+ def create(name, config, &block)
12
+ if config[:type]
13
+ @@fixtures << create_p(config[:type], name, config)
14
+ elsif config[:parent]
15
+ fixture = get_by_name(config[:parent]).clone(name)
16
+ @@fixtures << fixture
17
+ else
18
+ raise "need either :parent or :type to create fixture"
19
+ end
20
+
21
+ block.call(@@fixtures.last)
22
+ end
23
+
24
+ def generate_all
25
+ workers = []
26
+ @@fixtures.flatten.each do |f|
27
+ workers << Thread.new { f.generate() }
28
+ end
29
+
30
+ workers.each { |w| w.join }
31
+ end
32
+
33
+ def get(name)
34
+ get_by_name(name).location
35
+ end
36
+
37
+ private
38
+ def inherited(subclass)
39
+ @@fixture_types << subclass
40
+ end
41
+
42
+ def create_p(type, *args)
43
+ get_by_type(type).new(*args)
44
+ end
45
+
46
+ def get_by_name(name)
47
+ @@fixtures.each do |f|
48
+ return f if f.name == name
49
+ end
50
+
51
+ raise "No Fixture with Name: #{name} found"
52
+ end
53
+
54
+ def get_by_type(type)
55
+ @@fixture_types.each do |fixture|
56
+ return fixture if fixture.type == type
57
+ end
58
+
59
+ raise "No fixture of type: #{type} found"
60
+ end
61
+ end
62
+
63
+ attr_reader :location
64
+ attr_accessor :name
65
+ end
66
+ end
@@ -0,0 +1,65 @@
1
+ require 'erb'
2
+ require 'tmpdir'
3
+
4
+ module JsFixtures
5
+ class HTML < Base
6
+ @type = :html
7
+ @template = File.join File.dirname(__FILE__), "../../template/html_template.html.erb"
8
+
9
+ class << self
10
+ attr_accessor :local_fixture_path
11
+ attr_reader :type, :template
12
+
13
+ def type
14
+ @type.to_sym
15
+ end
16
+
17
+ def inherited(subclass)
18
+ super
19
+ end
20
+
21
+ def template=(template)
22
+ @template = template
23
+ end
24
+ end
25
+
26
+ attr_accessor :scripts, :pre_scripts, :post_scripts
27
+
28
+ def initialize(name, config)
29
+ @name = name
30
+ @scripts = config[:scripts] ||= []
31
+ @pre_scripts = config[:settings] ||= ""
32
+ @post_scripts = config[:post_scripts] ||= ""
33
+
34
+ self.class.local_fixture_path ||= Dir.mktmpdir
35
+ @location = "#{self.class.local_fixture_path}/#{@name}.html"
36
+ @local_file = File.expand_path(@location)
37
+ end
38
+
39
+ def generate
40
+ file = File.open(@local_file, 'w')
41
+ file.syswrite(render_template())
42
+ file.close
43
+ end
44
+
45
+
46
+ def clone(new_name)
47
+ self.class.new(new_name,
48
+ :scripts => @scripts.dup,
49
+ :settings => @pre_scripts.dup,
50
+ :post_scripts => @post_scripts.dup)
51
+ end
52
+
53
+ private
54
+
55
+ def render_template
56
+ # setup binding
57
+ name = @name
58
+ pre_scripts = @pre_scripts
59
+ scripts = @scripts
60
+ post_scripts = @post_scripts
61
+ ERB.new(File.read(self.class.template)).result(binding)
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,34 @@
1
+ module JsFixtures
2
+ class HTMLS3 < HTML
3
+ include S3Upload
4
+
5
+ @type = :html_s3
6
+ @template = File.join File.dirname(__FILE__), "../../template/html_template.html.erb"
7
+
8
+ class << self
9
+ attr_reader :s3_upload_url, :s3_access_url
10
+
11
+ def s3_path=(s3_path)
12
+ @s3_upload_url = s3_path
13
+ @s3_access_url = "http://#{s3_path.gsub(':', '/')}"
14
+ end
15
+ end
16
+
17
+
18
+ def initialize(name, config)
19
+ super
20
+
21
+ # Number of seconds since 1970-01-01 00:00:00 UTC
22
+ # to avoid test collisions from multiple machines
23
+ suffix = Time.now.strftime("%s")
24
+ @filename = "#{@name}-#{suffix}.html"
25
+ @location = "#{self.class.s3_access_url}/#{@filename}"
26
+ end
27
+
28
+ def generate
29
+ puts "Generating Fixture: #{@name}"
30
+ super
31
+ upload_html_file(@local_file, "#{self.class.s3_upload_url}/#{@filename}")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+
2
+ require 'right_aws'
3
+ module S3Upload
4
+ AWS_KEY = ENV['AWS_ACCESS_KEY_ID'] || raise("Need to have AWS_ACCESS_KEY set in your env")
5
+ AWS_SECRET_KEY = ENV['AWS_SECRET_ACCESS_KEY'] || raise("Need to have AWS_SECRET_ACCESS_KEY set in your env")
6
+
7
+ HTML_HEADERS = {"Content-Type" => "text/html" }
8
+
9
+ def upload_html_file(file_path, s3_path)
10
+ puts "Uploading from #{file_path} to #{s3_path}"
11
+ uploader = RightAws::S3.new(AWS_KEY, AWS_SECRET_KEY)
12
+ bucket, key = s3_path.split(":")
13
+ bucket = uploader.bucket(bucket)
14
+ bucket.put(key, File.read(file_path), {}, "public-read", HTML_HEADERS)
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE HTML>
2
+ <html>
3
+ <head>
4
+ <script type="text/javascript">
5
+ <%= pre_scripts %>
6
+ </script>
7
+
8
+ <% scripts.each do |script| %>
9
+ <script type="text/javascript" src="<%=script %>"></script>
10
+ <% end %>
11
+
12
+ <script type="text/javascript">
13
+ <%= post_scripts %>
14
+ </script>
15
+ </head>
16
+ <body>
17
+ </body>
18
+ </html>
19
+
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js-fixtures
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joshua Carver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-04 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: &2161994340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2161994340
25
+ - !ruby/object:Gem::Dependency
26
+ name: right_aws
27
+ requirement: &2161993900 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2161993900
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2161993480 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2161993480
47
+ description:
48
+ email: jcarver989@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/js_fixtures/base.rb
54
+ - lib/js_fixtures/html.rb
55
+ - lib/js_fixtures/html_s3.rb
56
+ - lib/js_fixtures/s3_upload.rb
57
+ - lib/js_fixtures.rb
58
+ - template/html_template.html.erb
59
+ homepage:
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.6
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: An easy way to handle your javascript/html fixtures
83
+ test_files: []