<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cory O&#039;Daniel - These are just words &#187; java</title>
	<atom:link href="http://coryodaniel.com/index.php/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryodaniel.com</link>
	<description>Software development, thoughts, and randomness</description>
	<lastBuildDate>Thu, 17 Nov 2011 21:18:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cory&#8217;s Ruby YUI Compressor v 2.0 Simpler, shorter.</title>
		<link>http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 02:43:23 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=445</guid>
		<description><![CDATA[So, a while ago I write a Ruby YUI Compressor Wrapper which is still available and works great (although it could use a newer jar file) as far as I know from Mandula's Blog. Some other guy also wrote one that pretty much had the same functionality as my gem. He must have hated it [...]]]></description>
			<content:encoded><![CDATA[<p>So, a while ago I write a <a href="http://github.com/coryodaniel/ruby-yui">Ruby YUI Compressor Wrapper</a> which is still available and works great (although it could use a newer jar file) as far as I know from <a href="http://blog.mandula.co.uk/js-and-css-optimisation-with-ruby-yui-and-mer">Mandula's Blog.</a> Some <a href="http://github.com/sstephenson/ruby-yui-compressor">other guy</a> also wrote one that pretty much had the same functionality as my gem. He must have hated it or something, I dunno. <a href="http://github.com/documentcloud/closure-compiler"> Even another guy</a> wrote a wrapper for Closure Compiler if thats your bag.</p>
<p>Anywho, on a new project I'm working on *I just need a inline YUI compressor* and I dont give a damn about anything else like bundling, globbing or whatever. So I ended up throwing together this little (~50 lines less comments) Ruby YUI Compressor to do my dirty work. </p>
<p>I won't make it a gem probably, because if I do someone will just make a ruby-yui-simpler-also gem and put it on digg and get a bigger following <img src='http://coryodaniel.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  (&lt;/sarcasm&gt;)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'open3'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'stringio'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> YUI
  <span style="color:#9966CC; font-weight:bold;">include</span> Open3 
  JAR_PATH = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">dirname</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">__FILE__</span><span style="color:#006600; font-weight:bold;">&#41;</span>,<span style="color:#996600;">'yuicompressor-2.4.2.jar'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#0000FF; font-weight:bold;">self</span>
    <span style="color:#008000; font-style:italic;"># @param io [String|File]</span>
    <span style="color:#008000; font-style:italic;"># @param options [Hash] - See YUI.cli</span>
    <span style="color:#008000; font-style:italic;"># @returns String</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>io, options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:type</span><span style="color:#006600; font-weight:bold;">&#93;</span>    <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#ff3333; font-weight:bold;">:js</span>
      options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:charset</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#996600;">&quot;utf-8&quot;</span>
&nbsp;
      stdin, stdout, stderr = Open3.<span style="color:#9900CC;">popen3</span><span style="color:#006600; font-weight:bold;">&#40;</span> YUI.<span style="color:#9900CC;">cli</span><span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
      stdin.<span style="color:#CC0066; font-weight:bold;">puts</span> streamify<span style="color:#006600; font-weight:bold;">&#40;</span>io<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span>
      stdin.<span style="color:#9900CC;">close</span>
&nbsp;
      my_stderr = stderr.<span style="color:#9900CC;">read</span>
      my_stdout = stdout.<span style="color:#9900CC;">read</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># if compression failed, just output original IO</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> my_stderr.<span style="color:#9900CC;">empty</span>?
        <span style="color:#0000FF; font-weight:bold;">return</span> my_stdout
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, my_stderr
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># If any exceptions are raised, the original string will be</span>
    <span style="color:#008000; font-style:italic;">#   returned incase you are compressing stuff during a deploy</span>
    <span style="color:#008000; font-style:italic;">#   and _just_dont_care_</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> compress_safe<span style="color:#006600; font-weight:bold;">&#40;</span>io, options=<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      YUI.<span style="color:#9900CC;">compress</span><span style="color:#006600; font-weight:bold;">&#40;</span>io, options<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> ex
      <span style="color:#CC0066; font-weight:bold;">puts</span> ex.<span style="color:#9900CC;">message</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> streamify<span style="color:#006600; font-weight:bold;">&#40;</span>io<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span>      
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># Sets up the command line options</span>
    <span style="color:#008000; font-style:italic;"># @param options [Hash]</span>
    <span style="color:#008000; font-style:italic;">#   :jar_path         - [String] Path to the jar file, default &quot;./yuicompress-2.4.2.jar&quot;</span>
    <span style="color:#008000; font-style:italic;">#   :charset          - [String] default 'utf-8'</span>
    <span style="color:#008000; font-style:italic;">#   :type             - [Symbol] Options: js/css, default :js</span>
    <span style="color:#008000; font-style:italic;">#   :line_break       - [Fixnum] Column to add line break at</span>
    <span style="color:#008000; font-style:italic;">#   </span>
    <span style="color:#008000; font-style:italic;">#   if the type of compression is JS, then additional options:</span>
    <span style="color:#008000; font-style:italic;">#     :nomunge        - [Boolean] do not obfuscate</span>
    <span style="color:#008000; font-style:italic;">#     :preserve_semi  - [Boolean] preserve all semicolons</span>
    <span style="color:#008000; font-style:italic;">#     :disable_opt    - [Boolean] Disable all micro optimizations</span>
    <span style="color:#008000; font-style:italic;"># </span>
    <span style="color:#008000; font-style:italic;"># @returns String</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> cli<span style="color:#006600; font-weight:bold;">&#40;</span>options<span style="color:#006600; font-weight:bold;">&#41;</span>
      _cmd = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;java -jar #{options[:jar_path] || JAR_PATH}&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--type #{options[:type]}&quot;</span>
      _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--charset #{options[:charset]}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:charset</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--charset #{options[:line_break]}&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:line_break</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:type</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#ff3333; font-weight:bold;">:js</span>  
        _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--nomunge&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:nomunge</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--preserve-semi&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:preserve_semi</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        _cmd <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot;--disable-optimizations&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> options<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:disable_opt</span><span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      _cmd.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># If a file or a string, treat it like its a stream</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#008000; font-style:italic;"># @param string_or_stream [File|String]</span>
    <span style="color:#008000; font-style:italic;"># @return StringIO</span>
    <span style="color:#008000; font-style:italic;">#</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> streamify<span style="color:#006600; font-weight:bold;">&#40;</span>string_or_stream<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> string_or_stream.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:read</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        string_or_stream.<span style="color:#9900CC;">rewind</span> <span style="color:#9966CC; font-weight:bold;">if</span> string_or_stream.<span style="color:#9900CC;">eof</span>?
        string_or_stream
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC00FF; font-weight:bold;">StringIO</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>string_or_stream<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#008000; font-style:italic;">#end class &lt;&lt; self</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Wanna use it?<br />
1. Copy the code above, throw it in your lib folder or where ever<br />
2. Download the <a href="http://yuilibrary.com/downloads/yuicompressor/yuicompressor-2.4.2.zip">YUI compressor</a> JAR file and put it in the same directory.<br />
3. Do something like the following...</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">YUI.<span style="color:#9900CC;">compress</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;function weebleep(){var cool_variable= 3; return cool_variable;}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;function weebleep(){var a=3;return a};&quot;</span>
YUI.<span style="color:#9900CC;">compress</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;public/javascripts/application.js&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; A bunch of compressed javascript</span>
YUI.<span style="color:#9900CC;">compress</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;public/stylesheets/application.css&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#006600; font-weight:bold;">&#123;</span>:type <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:css</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; Compressed stylesheet</span></pre></td></tr></table></div>

<p>Other options include:<br />
:jar_path         - [String] Path to the jar file, default "./yuicompress-2.4.2.jar"<br />
:charset          - [String] default 'utf-8'<br />
:type             - [Symbol] Options: js/css, default :js<br />
:line_break       - [Fixnum] Column to add line break at</p>
<p>if the type of compression is JS, then additional options:<br />
:nomunge        - [Boolean] do not obfuscate<br />
:preserve_semi  - [Boolean] preserve all semicolons<br />
:disable_opt    - [Boolean] Disable all micro optimizations</p>
<p>Yay have a blast. Its another compression wrapper.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter.+http://coryodaniel.com/?p=445" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/&amp;title=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter." title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/&amp;t=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter." title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/&amp;title=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter." title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/&amp;title=Cory%E2%80%99s+Ruby+YUI+Compressor+v+2.0+Simpler%2C+shorter." title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

