swiss_knife 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swiss_knife (0.1.9)
4
+ swiss_knife (0.2.0)
5
5
  rails (>= 3.0.0)
6
6
 
7
7
  GEM
@@ -34,7 +34,7 @@ GEM
34
34
  activemodel (= 3.0.6)
35
35
  activesupport (= 3.0.6)
36
36
  activesupport (3.0.6)
37
- arel (2.0.9)
37
+ arel (2.0.10)
38
38
  builder (2.1.2)
39
39
  diff-lcs (1.1.2)
40
40
  erubis (2.6.6)
data/lib/swiss_knife.rb CHANGED
@@ -10,3 +10,4 @@ require "swiss_knife/jquery"
10
10
  require "swiss_knife/jquery_ujs"
11
11
  require "swiss_knife/modernizr"
12
12
  require "swiss_knife/railtie"
13
+ require "swiss_knife/smusher_it"
@@ -1,4 +1,24 @@
1
1
  namespace :swiss_knife do
2
+ namespace :assets do
3
+ desc "Pack everything (images, CSS & JavaScript)"
4
+ task :all => %w[swiss_knife:assets:js swiss_knife:assets:css]
5
+
6
+ desc "Pack images"
7
+ task :images do
8
+ SwissKnife::SmusherIt.convert_directory Rails.root.join("public/images")
9
+ end
10
+
11
+ desc "Pack CSS"
12
+ task :css do
13
+ SwissKnife::Assets.export(:stylesheets)
14
+ end
15
+
16
+ desc "Pack JavaScript"
17
+ task :js do
18
+ SwissKnife::Assets.export(:javascripts)
19
+ end
20
+ end
21
+
2
22
  namespace :javascripts do
3
23
  desc "Update all JavaScripts"
4
24
  task :update => %w[ i18njs jquery jquery_ujs modernizr dispatcher ]
@@ -0,0 +1,59 @@
1
+ require "net/http"
2
+ require "uri"
3
+ require "json"
4
+ require "open-uri"
5
+ require "digest/sha1"
6
+
7
+ module SwissKnife
8
+ class SmushIt
9
+ ENDPOINT = URI.parse("http://ws1.adq.ac4.yahoo.com/ysmush.it/ws.php")
10
+ CONTENT_TYPE = {
11
+ ".png" => "image/png",
12
+ ".gif" => "image/gif",
13
+ ".jpg" => "image/jpeg",
14
+ ".jpeg" => "image/jpeg"
15
+ }
16
+
17
+ HOOKS = {}
18
+
19
+ def self.on(hook, &block)
20
+ HOOKS[hook] ||= []
21
+ HOOKS[hook] << block
22
+ end
23
+
24
+ def self.compress(filepath)
25
+ http = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port)
26
+ request = Net::HTTP::Post.new(ENDPOINT.request_uri)
27
+ file_contents = File.read(filepath)
28
+ boundary = Digest::SHA1.hexdigest(file_contents)
29
+ request["Content-Type"] = "multipart/form-data, boundary=#{boundary}"
30
+ request.body = [].tap do |body|
31
+ body << "--#{boundary}"
32
+ body << %[Content-Disposition: form-data; name="files[]"; filename="#{File.basename(filepath)}"]
33
+ body << %[Content-Type: #{CONTENT_TYPE[File.extname(filepath).downcase]}\r\n]
34
+ body << file_contents
35
+ body << "--#{boundary}--\r\n"
36
+ end.join("\r\n")
37
+
38
+ response = http.request(request)
39
+ return nil unless response.code == "200"
40
+
41
+ JSON.load(response.body).tap do |info|
42
+ HOOKS[:complete].each {|block| block[filepath, info]}
43
+ end
44
+ end
45
+
46
+ def self.convert(from, to)
47
+ info = compress(from)
48
+ return if info["error"]
49
+ File.open(to, "wb+") {|file| file << open(info["dest"]).read }
50
+ end
51
+
52
+ def self.convert_directory(source)
53
+ Dir["#{source}/**/*.{png,gif,jpg,jpeg}"].each do |filepath|
54
+ p "converting #{filepath}"
55
+ convert filepath, filepath
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,8 +1,8 @@
1
1
  module SwissKnife
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 1
5
- PATCH = 9
4
+ MINOR = 2
5
+ PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: swiss_knife
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.9
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nando Vieira
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-01 00:00:00 Z
13
+ date: 2011-05-29 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -99,6 +99,7 @@ files:
99
99
  - lib/swiss_knife/rspec/allow.rb
100
100
  - lib/swiss_knife/rspec/have_tag.rb
101
101
  - lib/swiss_knife/rspec/have_text.rb
102
+ - lib/swiss_knife/smusher_it.rb
102
103
  - lib/swiss_knife/support/remote_file.rb
103
104
  - lib/swiss_knife/version.rb
104
105
  - locales/en.yml
@@ -159,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
160
  requirements: []
160
161
 
161
162
  rubyforge_project:
162
- rubygems_version: 1.7.2
163
+ rubygems_version: 1.8.3
163
164
  signing_key:
164
165
  specification_version: 3
165
166
  summary: Several helpers for Rails 3