triboelectric 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f76f32cb162ecb4e764aa864b26b8ad089fa54bc
4
- data.tar.gz: 1d55a2a9c4e57de5b5a83ec8d72db6df8a9eb7a5
3
+ metadata.gz: abbb6d9f0c464dedd612a38aa37b6203e58618eb
4
+ data.tar.gz: 898da423f28fcb0a6a1f87c77bc0365ddf17b4a2
5
5
  SHA512:
6
- metadata.gz: 7146c2136b11d81b102f51c7dad34ffbafc8a42e9530de821e67eb950afd4aaa30594c0e188885398ab2a6255c26891a697b571a8da0910aaab2e9f59d98c17b
7
- data.tar.gz: 92e00be5056105028fc40a9f2e8b026233b1be60dd073497d13e7a9b847777cc91ebcbf145ff795a8eac5b2272d032d8bad0010f42ebf1ce56643f2f465fa1fa
6
+ metadata.gz: bce3df593b9d24c5e17708d1c292d2a430a732c25470ec58298944be898d836a989432cc928be15eeda68d86b78861444fe2d030cf1551a8d1edee7a11b7aa7e
7
+ data.tar.gz: a9b26c00f3915151a4093810e1b78b9df611a9bd477d95d264eba4a532fef1df7fe71cd640f30b9925484aada2baa09c4694fe0c5bdbe764475e4a836e2be9a1
@@ -1,5 +1,6 @@
1
1
  require "triboelectric/version"
2
+ require "triboelectric/static"
3
+ require "triboelectric/uploader"
2
4
 
3
5
  module Triboelectric
4
- # Your code goes here...
5
6
  end
@@ -0,0 +1,40 @@
1
+ require "rack"
2
+
3
+ module Triboelectric
4
+ class Static
5
+ def initialize(app, options = {})
6
+ Uploader.upload(options) if options.fetch(:upload, true)
7
+ @app = app
8
+ @static = Rack::Static.new(@app, options)
9
+ @bucket = options[:bucket]
10
+ @root = options[:root]
11
+ end
12
+
13
+ def call(env)
14
+ return @app.call(env) unless @static.can_serve(env[Rack::PATH_INFO])
15
+
16
+ status, headers, body = @static.call(env)
17
+
18
+ if (object = get_object(env, status))
19
+ headers[Rack::CONTENT_TYPE] = object.content_type
20
+ headers[Rack::CONTENT_LENGTH] = object.content_length
21
+ status = 200
22
+ body = object.body
23
+ end
24
+
25
+ [status, headers, body]
26
+ end
27
+
28
+ private
29
+
30
+ def get_object(env, status)
31
+ return unless @bucket
32
+ return unless status == 404
33
+ path = env[Rack::PATH_INFO]
34
+ path = File.join(@root, path) if @root
35
+ object = @bucket.object(path)
36
+ return unless object.exists?
37
+ object.get
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,47 @@
1
+ require "rack/mime"
2
+
3
+ module Triboelectric
4
+ class Uploader
5
+ def initialize(options = {})
6
+ @bucket = options[:bucket]
7
+ @root = options[:root]
8
+ @urls = options[:urls]
9
+ end
10
+
11
+ def self.upload(*args)
12
+ new(*args).upload
13
+ end
14
+
15
+ def upload
16
+ return unless @bucket
17
+ return if %w(development test).include? ENV["RACK_ENV"]
18
+ files.each do |file|
19
+ next if @bucket.object(file).exists?
20
+ @bucket.put_object(
21
+ key: file,
22
+ body: File.open(file, "r"),
23
+ content_type: Rack::Mime.mime_type(File.extname(file), "text/plain"),
24
+ )
25
+ end
26
+ end
27
+
28
+ def files
29
+ urls = @urls if @urls.is_a?(Array)
30
+ urls = @urls.values if @urls.is_a?(Hash)
31
+
32
+ urls.map do |url|
33
+ if @root
34
+ url = File.join(@root, url)
35
+ else
36
+ url = url.sub(/^\//, "")
37
+ end
38
+
39
+ if File.file? url
40
+ url
41
+ else
42
+ Dir[File.join(url, "**/*")].select { |f| File.file? f }
43
+ end
44
+ end.flatten
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module Triboelectric
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: triboelectric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ed Robinson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-28 00:00:00.000000000 Z
11
+ date: 2017-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -66,6 +66,48 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler-audit
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.12'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.12'
97
+ - !ruby/object:Gem::Dependency
98
+ name: reevoocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
69
111
  description:
70
112
  email:
71
113
  - ed@reevoo.com
@@ -75,7 +117,8 @@ extra_rdoc_files: []
75
117
  files:
76
118
  - README.md
77
119
  - lib/triboelectric.rb
78
- - lib/triboelectric/assets.rb
120
+ - lib/triboelectric/static.rb
121
+ - lib/triboelectric/uploader.rb
79
122
  - lib/triboelectric/version.rb
80
123
  homepage: https://github.com/reevoo/triboelectric
81
124
  licenses:
@@ -1,43 +0,0 @@
1
- require "rack"
2
-
3
- module Triboelectric
4
- class Assets
5
- def initialize(app, options={})
6
- @app = app
7
- @urls = options[:urls] || []
8
- @bucket = options[:bucket]
9
- @root = options[:root]
10
- end
11
-
12
- def call(env)
13
- path = env[Rack::PATH_INFO]
14
- response = @app.call(env)
15
- return response unless active?(path, response)
16
-
17
- if (object = get_object(path))
18
- response[1][Rack::CONTENT_TYPE] = object.content_type
19
- response[1][Rack::CONTENT_LENGTH] = object.content_length
20
- response[2] = object.body
21
- return response
22
- end
23
-
24
- response
25
- end
26
-
27
- private
28
-
29
- def get_object(path)
30
- obj_path = path.dup
31
- obj_path = File.join(@root, obj_path) if @root
32
- @bucket.object(obj_path).get
33
- rescue Aws::S3::Errors::NoSuchKey
34
- nil
35
- end
36
-
37
- def active?(path, response)
38
- return unless @bucket
39
- return unless response.first == 404
40
- @urls.any? { |url| path.index(url) == 0 }
41
- end
42
- end
43
- end