<?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>SoftLayer Blog &#187; coding</title>
	<atom:link href="http://blog.softlayer.com/tag/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.softlayer.com</link>
	<description>A Behind the Scenes Look at the Best Hosting Provider in the World</description>
	<lastBuildDate>Wed, 15 May 2013 15:33:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Understanding and Implementing Coding Standards</title>
		<link>http://blog.softlayer.com/2013/understanding-and-implementing-coding-standards/</link>
		<comments>http://blog.softlayer.com/2013/understanding-and-implementing-coding-standards/#comments</comments>
		<pubDate>Fri, 10 May 2013 20:10:16 +0000</pubDate>
		<dc:creator>Phil Jackson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[code base]]></category>
		<category><![CDATA[coders]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[programmers]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[rules]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[team]]></category>
		<category><![CDATA[update]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=11314</guid>
		<description><![CDATA[Coding standards provide a consistent framework for development within a project and across projects in an organization. A dozen programmers can complete a simple project in a dozen different ways by using unique coding methodologies and styles, so I like to think of coding standards as the &#8220;rules of the road&#8221; for developers. When you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>Coding standards provide a consistent framework for development within a project and across projects in an organization. A dozen programmers can complete a simple project in a dozen different ways by using unique coding methodologies and styles, so I like to think of coding standards as the &#8220;rules of the road&#8221; for developers.</p>
<p>When you&#8217;re driving in a car, traffic is controlled by &#8220;standards&#8221; such as lanes, stoplights, yield signs and laws that set expectations around how you should drive. When you take a road trip to a different state, the stoplights might be hung horizontally instead of vertically or you&#8217;ll see subtle variations in signage, but because you&#8217;re familiar with the rules of the road, you&#8217;re comfortable with the mechanics of driving in this new place. Coding standards help control development traffic and provide the consistency programmers need to work comfortably with a team across projects. The problem with allowing developers to apply their own unique coding styles to a project is the same as allowing drivers to drive as they wish &#8230; Confusion about lane usage, safe passage through intersections and speed would result in collisions and bottlenecks.</p>
<p>Coding standards often seem restrictive or laborious when a development team starts considering their adoption, but they don&#8217;t have to be &#8230; They can be implemented methodically to improve the team&#8217;s efficiency and consistency over time, and they can be as simple as establishing that all instantiations of an object must be referenced with a variable name that begins with a capital letter:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$User</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>While that example may seem overly simplistic, it actually makes life a lot easier for all of the developers on a given project. Regardless of who created that variable, every other developer can see the difference between a variable that holds data and one that are instantiates an object. Think about the shapes of signs you encounter while driving &#8230; You know what a stop sign looks like without reading the word &#8220;STOP&#8221; on it, so when you see a red octagon (in the United States, at least), you know what to do when you approach it in your car. Seeing a capitalized variable name would tell us about its function.</p>
<p>The example I gave of capitalizing instantiated objects is just an example. When it comes to coding standards, the most effective rules your team can incorporate are the ones that make the most sense to you. While there are a few best practices in terms of formatting and commenting in code, the most important characteristics of coding standards for a given team is consistency and clarity.</p>
<p>So how do you go about creating a coding standard? Most developers dislike doing unnecessary work, so the easiest way to create a coding standard is to use an already-existing one. Take a look at any libraries or frameworks you are using in your current project. Do they use any coding standards? Are those coding standards something you can live with or use as a starting point? You are free to make any changes to it you wish in order to best facilitate your team&#8217;s needs, and you can even set how strict specific coding standards must be adhered to. Take for example left-hand comparisons:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> $a <span style="color: #002200;">==</span> <span style="color: #2400d9;">12</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> … <span style="color: #002200;">&#125;</span> <span style="color: #11740a; font-style: italic;">// right-hand comparison</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #2400d9;">12</span> <span style="color: #002200;">==</span> $a <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> … <span style="color: #002200;">&#125;</span> <span style="color: #11740a; font-style: italic;">// left-hand comparison</span></pre></div></div>

<p>Both of these statements are valid but one may be preferred over the other. Consider the following statements:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> $a <span style="color: #002200;">=</span> <span style="color: #2400d9;">12</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> … <span style="color: #002200;">&#125;</span> <span style="color: #11740a; font-style: italic;">// supposed to be a right-hand comparison but is now an assignment</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #2400d9;">12</span> <span style="color: #002200;">=</span> $a <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span> … <span style="color: #002200;">&#125;</span> <span style="color: #11740a; font-style: italic;">// supposed to be a left-hand comparison but is now an assignment</span></pre></div></div>

<p>The first statement will now evaluate to true due to <code>$a</code> being assigned the value of 12 which will then cause the code within the if-statement to execute (which is not the desired result). The second statement will cause an error, therefore making it obvious a mistake in the code has occurred. Because our team couldn&#8217;t come to a consensus, we decided to allow both of these standards &#8230; Either of these two formats are acceptable and they&#8217;ll both pass code review, but they are the only two acceptable variants. Code that deviates from those two formats would fail code review and would not be allowed in the code base.</p>
<p>Coding standards play an important role in efficient development of a project when you have several programmers working on the same code. By adopting coding standards and following them, you&#8217;ll avoid a free-for-all in your code base, and you&#8217;ll be able to look at every line of code and know more about what that line is telling you than what the literal code is telling you &#8230; just like seeing a red octagon posted on the side of the road at an intersection.</p>
<p>-<a href="https://twitter.com/SoftLayerDevs">@SoftLayerDevs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/understanding-and-implementing-coding-standards/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iptables Tips and Tricks &#8211; Track Bandwidth with iptables</title>
		<link>http://blog.softlayer.com/2013/iptables-tips-and-tricks-track-bandwidth-with-iptables/</link>
		<comments>http://blog.softlayer.com/2013/iptables-tips-and-tricks-track-bandwidth-with-iptables/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 21:30:29 +0000</pubDate>
		<dc:creator>Mark Ridlen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[bandwidth]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[cPanel]]></category>
		<category><![CDATA[CSF]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[tracking]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=11025</guid>
		<description><![CDATA[As I mentioned in my last post about CSF configuration in iptables, I&#8217;m working on a follow-up post about integrating CSF into cPanel, but I thought I&#8217;d inject a simple iptables use-case for bandwidth tracking. You probably think about iptables in terms of firewalls and security, but it also includes a great diagnostic tool for [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in my last post about <a href="http://blog.softlayer.com/2013/iptables-tips-and-tricks-csf-configuration/">CSF configuration in iptables</a>, I&#8217;m working on a follow-up post about integrating CSF into cPanel, but I thought I&#8217;d inject a simple iptables use-case for bandwidth tracking. You probably think about iptables in terms of firewalls and security, but it also includes a great diagnostic tool for counting bandwidth for individual rules or set of rules. If you can block it, you can track it! </p>
<p>The best part about using iptables to track bandwidth is that the tracking is enabled by default. To see this feature in action, add the &#8220;-v&#8221; into the command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-vnL</span>
Chain INPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT <span style="color: #000000;">2495</span> packets, 104K bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>The output includes counters for both the policies and the rules. To track the rules, you can create a new chain for <code>tracking</code> bandwidth:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-N</span> tracking
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-vnL</span>
...
Chain tracking <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0</span> references<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination</pre></div></div>

<p>Then you need to set up new rules to match the traffic that you wish to track. In this scenario, let&#8217;s look at inbound http traffic on port 80:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-I</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> tracking
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-vnL</span>
Chain INPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT <span style="color: #000000;">35111</span> packets, 1490K bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination
    <span style="color: #000000;">0</span> 	  <span style="color: #000000;">0</span> tracking    tcp  <span style="color: #660033;">--</span>  <span style="color: #000000; font-weight: bold;">*</span>  	<span style="color: #000000; font-weight: bold;">*</span>   	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>        	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>       	tcp dpt:<span style="color: #000000;">80</span></pre></div></div>

<p>Now let&#8217;s generate some traffic and check it again:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-vnL</span>
Chain INPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT <span style="color: #000000;">35216</span> packets, 1500K bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination
  <span style="color: #000000;">101</span>  <span style="color: #000000;">9013</span> tracking    tcp  <span style="color: #660033;">--</span>  <span style="color: #000000; font-weight: bold;">*</span>  	<span style="color: #000000; font-weight: bold;">*</span>   	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>        	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>       	tcp dpt:<span style="color: #000000;">80</span></pre></div></div>

<p>You can see the packet and byte transfer amounts to track the INPUT &mdash; traffic to a <strong>destination</strong> port on your server. If you want track the amount of data that the server is generating, you&#8217;d look for OUTPUT from the <strong>source</strong> port on your server:</p>
<p><span id="more-11025"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-I</span> OUTPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--sport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> tracking
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-vnL</span>
...
Chain OUTPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT <span style="color: #000000;">26149</span> packets, 174M bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination
  <span style="color: #000000;">488</span> 3367K tracking    tcp  <span style="color: #660033;">--</span>  <span style="color: #000000; font-weight: bold;">*</span>  	<span style="color: #000000; font-weight: bold;">*</span>   	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>        	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>       	tcp spt:<span style="color: #000000;">80</span></pre></div></div>

<p>Now that we know how the tracking chain works, we can add in a few different layers to get even more information. That way you can keep your INPUT and OUTPUT chains looking clean.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables –N tracking
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables –N tracking2
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables –I INPUT –j tracking
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables –I OUTPUT –j tracking
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables –A tracking –p tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> –j tracking2
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables –A tracking –p tcp <span style="color: #660033;">--sport</span> <span style="color: #000000;">80</span> –j tracking2
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-vnL</span>
&nbsp;
Chain INPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT <span style="color: #000000;">96265</span> packets, 4131K bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination
 <span style="color: #000000;">4002</span>  184K tracking    all  <span style="color: #660033;">--</span>  <span style="color: #000000; font-weight: bold;">*</span>  	<span style="color: #000000; font-weight: bold;">*</span>   	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>        	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>
&nbsp;
Chain FORWARD <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT <span style="color: #000000;">0</span> packets, <span style="color: #000000;">0</span> bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination
&nbsp;
Chain OUTPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT <span style="color: #000000;">33751</span> packets, 231M bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination
 <span style="color: #000000;">1399</span> 9068K tracking    all  <span style="color: #660033;">--</span>  <span style="color: #000000; font-weight: bold;">*</span>  	<span style="color: #000000; font-weight: bold;">*</span>   	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>        	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>
&nbsp;
Chain tracking <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span> references<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination
 <span style="color: #000000;">1208</span> <span style="color: #000000;">59626</span> tracking2   tcp  <span style="color: #660033;">--</span>  <span style="color: #000000; font-weight: bold;">*</span>  	<span style="color: #000000; font-weight: bold;">*</span>   	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>        	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>       	tcp dpt:<span style="color: #000000;">80</span>
  <span style="color: #000000;">224</span> 1643K tracking2   tcp  <span style="color: #660033;">--</span>  <span style="color: #000000; font-weight: bold;">*</span>  	<span style="color: #000000; font-weight: bold;">*</span>   	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>        	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>       	tcp spt:<span style="color: #000000;">80</span>
&nbsp;
Chain tracking2 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">2</span> references<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination</pre></div></div>

<p>Keep in mind that every time a packet passes through one of your rules, it will eat CPU cycles. Diverting all your traffic through 100 rules that track bandwidth may not be the best idea, so it&#8217;s important to have an efficient ruleset. If your server has eight processor cores and tons of overhead available, that concern might be inconsequential, but if you&#8217;re running lean, you could conceivably run into issues. </p>
<p>The easiest way to think about making efficient rulesets is to think about eating the largest slice of pie first. Understand <a href="http://blog.softlayer.com/2012/iptables-tips-and-tricks-rule-processing/">iptables rule processing</a> and put the rules that get more traffic higher in your list. Conversely, save the tiniest pieces of your pie for last. If you run all of your traffic by a rule that only applies to a tiny segment before you screen out larger segments, you&#8217;re wasting processing power.</p>
<p>Another thing to keep in mind is that you do not need to specify a target (in our examples above, we established <code>tracking</code> and <code>tracking2</code> as our targets). If you&#8217;re used to each rule having a specific purpose of either blocking, allowing, or diverting traffic, this simple tidbit might seem revolutionary. For example, we could use this rule:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-A</span> INPUT</pre></div></div>

<p>If that seems a little bare to you, don&#8217;t worry &#8230; It is! The output will show that it is a rule that tracks all traffic in the chain at that point. We&#8217;re appending the data to the end of the chain in this example (&#8220;<code>-A</code>&#8220;) but we could also insert it (&#8220;<code>-I</code>&#8220;) at the top of the chain instead. This command could be helpful if you are using a number of different chains and you want to see the exact volume of packets that are filtered at any given point. Additionally, this strategy could show how much traffic a potential rule would filter before you run it on your production system. Because having several of these kinds of commands can get a little messy, it&#8217;s also helpful to add comments to help sort things out:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-m</span> comment <span style="color: #660033;">--comment</span> <span style="color: #ff0000;">&quot;track all data&quot;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server ~<span style="color: #7a0874; font-weight: bold;">&#93;</span>$ iptables <span style="color: #660033;">-vnL</span>
Chain INPUT <span style="color: #7a0874; font-weight: bold;">&#40;</span>policy ACCEPT 11M packets, 5280M bytes<span style="color: #7a0874; font-weight: bold;">&#41;</span>
 pkts bytes target 	prot opt <span style="color: #000000; font-weight: bold;">in</span> 	out 	<span style="color: #7a0874; font-weight: bold;">source</span>           	destination
   <span style="color: #000000;">98</span>  <span style="color: #000000;">9352</span>        	all  <span style="color: #660033;">--</span>  <span style="color: #000000; font-weight: bold;">*</span>  	<span style="color: #000000; font-weight: bold;">*</span>   	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>        	0.0.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>       	<span style="color: #000000; font-weight: bold;">/*</span> track all data <span style="color: #000000; font-weight: bold;">*/</span></pre></div></div>

<p>Nothing terribly complicated about using iptables to count bandwidth, right? If you have iptables rulesets and you want to get a glimpse at how your traffic is being affected, this little trick could be useful. You can rely on the information iptables gives you about your bandwidth usage, and you won&#8217;t be the only one &#8230; cPanel actually uses iptables to track bandwidth.</p>
<p>-Mark</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/iptables-tips-and-tricks-track-bandwidth-with-iptables/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Plivo: Tech Partner Spotlight</title>
		<link>http://blog.softlayer.com/2013/plivo-tech-partner-spotlight/</link>
		<comments>http://blog.softlayer.com/2013/plivo-tech-partner-spotlight/#comments</comments>
		<pubDate>Wed, 10 Apr 2013 16:00:05 +0000</pubDate>
		<dc:creator>Guest Blog</dc:creator>
				<category><![CDATA[Partner Marketplace]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[functionality]]></category>
		<category><![CDATA[marketplace]]></category>
		<category><![CDATA[partner]]></category>
		<category><![CDATA[SMS]]></category>
		<category><![CDATA[tech partners]]></category>
		<category><![CDATA[telecom]]></category>
		<category><![CDATA[telephony]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[voice]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=11042</guid>
		<description><![CDATA[We invite each of our featured SoftLayer Tech Marketplace Partners to contribute a guest post to the SoftLayer Blog, and this week, we&#8217;re happy to welcome Mike Lauricella from Plivo. Plivo is an open communications and messaging platform with advanced features, simple APIs, easy management and volume pricing. Company Website: http://www.plivo.com/ Tech Partners Marketplace: http://www.softlayer.com/marketplace/plivo [...]]]></description>
			<content:encoded><![CDATA[<p class="attribution">We invite each of our featured SoftLayer Tech Marketplace Partners to contribute a guest post to the SoftLayer Blog, and this week, we&#8217;re happy to welcome Mike Lauricella from <a href="http://www.plivo.com/">Plivo</a>. Plivo is an open communications and messaging platform with advanced features, simple APIs, easy management and volume pricing.</p>
<div class="yt560"><iframe width="560" height="315" src="http://www.youtube.com/embed/8PAkqRov9mk" frameborder="0" allowfullscreen></iframe></div>
<div class="more-info"><strong>Company Website:</strong> <a href="http://www.plivo.com/">http://www.plivo.com/</a><br />
<strong>Tech Partners Marketplace:</strong> <a href="http://www.softlayer.com/partners/marketplace/plivo">http://www.softlayer.com/marketplace/plivo</a></div>
<h3>Bridging the Gap Between the Web and Telephony</h3>
<p>Businesses face a fundamental challenge in the worlds of telephony and messaging: Those worlds move too slowly, require too much telecom knowledge and take too long to adopt. As a result, developers often forgo phone and SMS functionality in their applications because the learning curves are so steep, and the dated architecture seems like a foreign language. Over the last twenty years, the web has evolved a lot faster than telephony, and that momentum only widens the gap between the &#8220;old&#8221; telecom world and the &#8220;new&#8221; Internet world. <a href="http://www.plivo.com">Plivo</a> was created to bridge that gap and make telephony easy for developers to understand and incorporate into their applications with simple tools and APIs.</p>
<p>I could bore you to tears by describing the ins and outs of what we&#8217;ve learned about telephony and telecom since Plivo was founded, but I&#8217;d rather show off some of the noteworthy ways our customers have incorporated Plivo in their own businesses. After all, seeing those real-world applications is much more revealing about what Plivo does than any description of the nuts and bolts of our platform, right? </p>
<p><strong>Conferencing Solution</strong><br />
The purest use-cases for Plivo are when our customers can simply leverage powerful telephony functionality. A perfect example is a conferencing solution one of our customers created to host large-scale conferences with up to 200 participants. The company integrated the solution into their product and CRM so that sales reps and customers could jump on conference calls quickly. With that integration, the executive management team can keep track of all kinds of information about the calls &#8230; whether they&#8217;re looking to find which calls resulted in closed sales or they just want to see the average duration of a conference call for a given time frame.</p>
<p><strong>Call Tracking</strong><br />
Beyond facilitate conference calls quickly and seamlessly, many businesses have started using Plivo&#8217;s integration to incorporate call tracking statistics in their environments. Call tracking is big business because information about who called what number, when they called, how long they talked and the result of the call (sale, no sale, follow up) can determine whether the appropriate interaction has taken place with prospects or customers.</p>
<p><strong>Two Factor Authentication</strong><br />
With ever-increasing concerns about security online, we&#8217;ve seen a huge uptick in developers that come to Plivo for help with two factor authentication for web services. To ensure that a new site registrant is a real person who has provided a valid phone number (to help cut down on potential fraud), they use Plivo to send text messages with verification codes to those new registrant. </p>
<p><strong>Mass Alert Messaging</strong><br />
Because emergencies can happen at any time, our customers have enlisted Plivo&#8217;s functionality to send out mass alerts via phone calls and SMS messages when their customers are affected by an issue and need to be contacted. These voice and text messages can be sent quickly and easily with our automated tools, and while no one ever wants to deal with an emergency, having a solid communication lifeline provides some peace of mind.  </p>
<p><strong>WebRTC</strong><br />
An emerging new standard for communications is <a href="http://www.webrtc.org/">WebRTC</a> &mdash; open project that enables web browsers with Real-Time Communications (RTC) capabilities. WebRTC make communications a feature of the Web without plugins or complex SIP clients. Plivo already supports WebRTC, and even though the project is relatively young, it&#8217;s already being used in some amazing applications. </p>
<p>These use-cases are only the tip of the iceberg when it comes to how our customers are innovating on our platform, but I hope it helps paint a picture of the kinds of functionality Plivo enables simply and quickly. If you&#8217;ve been itching to incorporate telephony into your application, before you spending hours of your life poring over complex telecom architecture requirements, head over to <a href="http://www.plivo.com">plivo.com</a> to see how easy we can make your life. We offer free developer accounts where you can start to make calls to other Plivo users and other SIP endpoints immediately, and we&#8217;d love to chat with you about how you can leverage Plivo to make your applications communicate.</p>
<p>If you have any questions, feel free to drop us a note at <a href="mailto:hello@plivo.com">hello@plivo.com</a>, and we&#8217;ll get back to you with answers.</p>
<p>-Mike Lauricella, <a href="http://www.plivo.com/">Plivo</a></p>
<div class="tpm-note">This guest blog series highlights companies in SoftLayer&#8217;s <a href="http://www.softlayer.com/partners/marketplace/index">Technology Partners Marketplace</a>. <br/>These <a href="http://blog.softlayer.com/partner-marketplace/">Partners</a> have built their businesses on the SoftLayer Platform, and we&#8217;re excited for them to tell their stories. New Partners will be added to the Marketplace each month, so stay tuned for many more come.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/plivo-tech-partner-spotlight/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SoftLayer Mobile: Now a Universal iOS Application</title>
		<link>http://blog.softlayer.com/2013/softlayer-mobile-now-a-universal-ios-application/</link>
		<comments>http://blog.softlayer.com/2013/softlayer-mobile-now-a-universal-ios-application/#comments</comments>
		<pubDate>Mon, 01 Apr 2013 20:45:56 +0000</pubDate>
		<dc:creator>Pawel Kijowski</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[mobile applications]]></category>
		<category><![CDATA[SoftLayer Mobile]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10714</guid>
		<description><![CDATA[Last month, we put SoftLayer Mobile HD out to pasture. That iPad-specific application performed amazingly, and we got a lot of great feedback from our customers, so we doubled-down on our efforts to support iPad users by merging SoftLayer Mobile HD functionality with our standard SoftLayer Mobile app to provide a singular, universal application for [...]]]></description>
			<content:encoded><![CDATA[<p>Last month, we put <a href="https://itunes.apple.com/us/app/softlayer-mobile-hd/id460377057?mt=8">SoftLayer Mobile HD</a> out to pasture. That iPad-specific application performed amazingly, and we got a lot of great feedback from our customers, so we doubled-down on our efforts to support iPad users by merging SoftLayer Mobile HD functionality with our standard <a href="https://itunes.apple.com/us/app/softlayer-mobile/id373786244?mt=8">SoftLayer Mobile</a> app to provide a singular, universal application for all iOS devices.</p>
<p>By merging our two iOS applications into a single, universal app, we can provide better feature parity, maintain coherent architecture and increase code reuse and maintainability because we&#8217;re only working with a single feature-rich binary app that provides a consistent user experience on the iPhone and the iPad at the same. Obviously, this meant we had to retool much of the legacy iPhone-specific SoftLayer Mobile app in order to provide the same device-specific functionality we had for the iPad in SoftLayer Mobile HD, but I was surprised at how straightforward that process ended up being. I thought I&#8217;d share a few of the resources iOS includes that simplify the process of creating a universal iOS application.</p>
<p>iOS <a href="http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/AdvancedAppTricks/AdvancedAppTricks.html">supports</a> development of universal applications via device-specific resource loading and device-specific runtime checks, and we leveraged those tools based on particular situations in our code base. </p>
<p><a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html">Device-specific resource loading</a> allows iOS to choose the appropriate resource for the device being used. For example, if we have two different versions of an image called <code>SoftLayerOnBlack.png</code> to fit either an iPhone or an iPad, we simply call one <code>SoftLayerOnBlack~iphone.png</code> and call the other one <code>SoftLayerOnBlack~ipad.png</code>. With those two images in our application bundle, we let the system choose which image to use with a simple line of code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImage<span style="color: #002200;">*</span> image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;SoftLayerOnBlack.png&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>In addition to device-specific resource loading, iOS also included device-specific runtime checks. With these runtime checks, we&#8217;re able to create conditional code paths depending on the underlying device type:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>UI_USER_INTERFACE_IDIOM<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> UIUserInterfaceIdiomPad<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// The device is an iPad running iOS 3.2 or later.</span>
<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// The device is an iPhone or iPod touch.</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>These building blocks allow for a great deal of flexibility when it comes to creating a universal iOS application. Both techniques enable simple support based on what device is running the application, but they&#8217;re used in subtly different ways. With those device-specific tools, developers are able to approach their universal applications in a couple of distinct ways: </p>
<p><strong>Device-Dependent View Controller:</strong><br />
If we want users on the iPhone and iPad applications to have the same functionality but have the presentation tailored to their specific devices, we would create separate iPhone and iPad view controllers. For example, let&#8217;s look at how our <a href="http://www.softlayer.com/cloudlayer/storage/">Object Storage</a> browser appears on the iPhone and the iPad in SoftLayer Mobile: </p>
<div class="centered" style="width:620px;"><img style="float:left; display:block;" src="http://cdn.softlayer.com/innerlayer/objstoiphone.png" alt="Object Storage - iPhone"/><img style="display:block;" src="http://cdn.softlayer.com/innerlayer/objstoipad.png" alt="Object Storage - iPad"/></div>
<p>We want to take advantage of the additional real estate the iPad provides, so at runtime, the appropriate view controller is be selected based on the devices&#8217; UI context. The technique would look a little like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> SLMenuController
...
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> navigateToStorageModule<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
	UIViewController&lt;SLApplicationModule&gt; <span style="color: #002200;">*</span>storageModule <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>UI_USER_INTERFACE_IDIOM<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> UIUserInterfaceIdiomPad<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        storageModule <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>SLStorageModule_iPad storageModule<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        storageModule <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>SLStorageModule storageModule<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>self navigateToModule<span style="color: #002200;">:</span> storageModule<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>	
...
<span style="color: #a61390;">@end</span></pre></div></div>

<p><strong>&#8220;Universal&#8221; View Controller</strong><br />
In other situations, we didn&#8217;t need for the viewing experience to differ between the iPhone and the iPad, so we used a single view controller for all devices. We don&#8217;t compromise the user experience or presentation of data because the view controller either re-scales or reconfigures the layout at runtime based on screen size. Take a look at the &#8220;About&#8221; module on the iPhone and iPad:</p>
<div class="centered" style="width:620px;"><img style="float:left; display:block;" src="http://cdn.softlayer.com/innerlayer/aboutiphone.png" alt="About Module - iPhone"/><img style="display:block;" src="http://cdn.softlayer.com/innerlayer/aboutipad.png" alt="About Module - iPad"/></div>
<p>The code for the universal view controller of the &#8220;About&#8221; module looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> SLAboutModuleNavigationViewController
…
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init <span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      _navigationHidden <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
		_navigationWidth <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span>.size.width <span style="color: #002200;">*</span> <span style="color: #2400d9;">0.5</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
…
<span style="color: #a61390;">@end</span></pre></div></div>

<p>There are plenty of other iOS features and tricks in the universal <a href="https://itunes.apple.com/us/app/softlayer-mobile/id373786244?mt=8">SoftLayer Mobile</a> app. If you&#8217;ve got a SoftLayer account and an iOS devices, download the app to try it out and let us know what you think. If you were a SoftLayer Mobile HD user, do you notice any significant changes in the new app from the legacy app? </p>
<p>-Pawel</p>
<p>P.S. If you&#8217;re not on iOS but you still want some SoftLayer love on your mobile device, check out the other <a href="http://www.softlayer.com/about/automation/mobile-apps">SoftLayer Mobile Apps</a> on Android and Windows Phone.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/softlayer-mobile-now-a-universal-ios-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script Clip: HTML5 Audio Player with jQuery Controls</title>
		<link>http://blog.softlayer.com/2013/script-clip-html5-audio-player-with-jquery-controls/</link>
		<comments>http://blog.softlayer.com/2013/script-clip-html5-audio-player-with-jquery-controls/#comments</comments>
		<pubDate>Thu, 07 Mar 2013 21:05:19 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[audio player]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[creating]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[music]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10633</guid>
		<description><![CDATA[HTML5 and jQuery provide mind-blowing functionality. Projects that would have taken hours of development and hundreds of lines of code a few years ago can now be completed in about the time it&#8217;ll take you to read this paragraph. If you wanted to add your own audio player on a web page in the past, [...]]]></description>
			<content:encoded><![CDATA[<p>HTML5 and jQuery provide mind-blowing functionality. Projects that would have taken hours of development and hundreds of lines of code a few years ago can now be completed in about the time it&#8217;ll take you to read this paragraph. If you wanted to add your own audio player on a web page in the past, what would it have involved? Complicated elements? Flash (*shudders*)? It was so complicated that most developers just linked to the audio file, and the user just downloaded the file to play it locally. With HTML5, an embedded, cross-browser audio player can be added to a page with five lines of code, and if you want to get really fancy, you can easily use jQuery to add some custom controls.</p>
<p>If you&#8217;ve read any of my <a href="http://blog.softlayer.com/author/cwolff/">previous blogs</a>, you know that I love when I find little code snippets that make life as a web developer easier. My go-to tools in that pursuit are HTML5 and jQuery, so when I came across this audio player, I knew I had to share. There are some great jQuery plugins to play music files on a web page, but they can be major overkill for a simple application if you have to include comprehensive controls and themes. Sometimes you just want something simple without all of that overhead:  </p>
<p><audio style="width:550px; margin: 0 auto; display:block;" controls><br />
  <source src="http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.ogg" type="audio/ogg"><br />
  <source src="http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.mp3" type="audio/mpeg"><br />
Your browser does not support the audio element.<br />
</audio></p>
<p>Oooh&#8230; Ahhh&#8230;</p>
<p>That song &mdash; Pop Bounce by SoftLayer&#8217;s very own <a href="http://www.reverbnation.com/chrisinterrante">Chris Interrante</a> &mdash; is written in five simple lines of HTML5 code:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;audio <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:550px; margin: 0 auto; display:block;&quot;</span> controls&gt;</span>
  <span style="color: #009900;">&lt;source <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.ogg&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audio/ogg&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;source <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.mp3&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audio/mpeg&quot;</span>&gt;</span>
Your browser does not support the audio element.
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>audio&gt;</span></pre></div></div>

<p>If IE 9+, Chrome 6+, Firefox 3.6+, Safari 5+ and Opera 10+ would all agree on supported file formats for the <code>&lt;audio&gt;</code> tag, the code snippet would be even smaller. I completely geek out over it every time I look at it and remember the days of yore. As you can see, the HTML5 application has some simple default controls: Play, Pause, Scan to Time, etc. As a developers, I couldn&#8217;t help but look for a to spice it up a little &#8230; What if we want to fire an event when the user plays, pauses, stops or takes any other action with the audio file? jQuery!</p>
<p>Make sure your jQuery include is in the <code>&lt;head&gt;</code> of your page:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>Now let&#8217;s use jQuery to script separate &#8220;Play&#8221; and &#8220;Pause&#8221; links &#8230; And let&#8217;s have those links fire off an alert when they are pressed:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#play-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You have played the audio file!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>    
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#pause-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'You have paused the audio file!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>    
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>With that script in the <code>&lt;head&gt;</code> as well, the HTML on our page will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span>:<span style="color: #ff0000;">&quot;audioplayer&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;audio <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audioplayer&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audioplayer&quot;</span> controls loop&gt;</span>
    <span style="color: #009900;">&lt;source <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.ogg&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audio/ogg&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;source <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.mp3&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;audio/mpeg&quot;</span>&gt;</span>
  Your browser does not support the audio element.
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>audio&gt;</span>
&nbsp;
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;play-button&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Play!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pause-button&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>&gt;</span>Pause!<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></div></div>

<p>Want proof that it works that simply? <a href="http://cdn.softlayer.com/innerlayer/audioSL.html">Boom.</a></p>
<p>You can theme it any way you like; you can add icons instead of the text &#8230; The world is your oyster. The bonus is that you&#8217;re using one of the lightest media players on the Internet! If you decide to get brave (or just <em>more awesome</em>), you can explore additional features. You&#8217;re using jQuery, so your possibilities are nearly limitless. If you want to implement a &#8220;Stop&#8221; feature (which returns the audio back to the beginning when &#8220;Stop&#8221; is pressed), you can get creative:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#stop-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">currentTime</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// return the audio file back to the beginning</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you want to include some volume controls, those can be added in a snap as well:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#volumeUp&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">volume</span> <span style="color: #339933;">+=</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#volumeDown&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#audioplayer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">volume</span> <span style="color: #339933;">-=</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Try it out and let me know what you think. Your homework is to come up with some unique audio player functionality and share it here!</p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/script-clip-html5-audio-player-with-jquery-controls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.ogg" length="4222877" type="audio/ogg" />
<enclosure url="http://cdn.softlayer.com/innerlayer/Interrante-PopBounce.mp3" length="2680757" type="audio/mpeg" />
		</item>
		<item>
		<title>Tips and Tricks – Building a jQuery Plugin (Part 2)</title>
		<link>http://blog.softlayer.com/2013/tips-and-tricks-building-a-jquery-plugin-part-2/</link>
		<comments>http://blog.softlayer.com/2013/tips-and-tricks-building-a-jquery-plugin-part-2/#comments</comments>
		<pubDate>Thu, 14 Feb 2013 15:25:43 +0000</pubDate>
		<dc:creator>Cassandra Wolff</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[exercise]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10619</guid>
		<description><![CDATA[jQuery plugins don&#8217;t have to be complicated to create. If you&#8217;ve stumbled upon this blog in pursuit of a guide to show you how to make a jQuery plugin, you might not believe me &#8230; It seems like there&#8217;s a chasm between the &#8220;haves&#8221; of jQuery plugin developers and the &#8220;have nots&#8221; of future jQuery [...]]]></description>
			<content:encoded><![CDATA[<p>jQuery plugins don&#8217;t have to be complicated to create. If you&#8217;ve stumbled upon this blog in pursuit of a guide to show you how to make a jQuery plugin, you might not believe me &#8230; It seems like there&#8217;s a chasm between the &#8220;haves&#8221; of jQuery plugin developers and the &#8220;have nots&#8221; of future jQuery developers, and there aren&#8217;t very many bridges to get from one side to the other. In <a href="http://blog.softlayer.com/2012/tips-and-tricks-building-a-jquery-plugin-part-1/">Part 1</a> of our &#8220;Building a jQuery Plugin&#8221; series, we broke down how to build the basic structure of a plugin, and in this installment, we&#8217;ll be adding some usable functionality to our plugin. </p>
<p>Let&#8217;s start with the jQuery code block we created in Part 1:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">slPlugin</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                myVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;This is&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// this will be the default value of this var</span>
                anotherVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;our awesome&quot;</span><span style="color: #339933;">,</span>
                coolVar<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;plugin!&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                ourString <span style="color: #339933;">=</span> myVar <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> anotherVar <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; &quot;</span> <span style="color: #339933;">+</span> coolVar<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span> ourString<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We want our plugin to do a little more than return, &#8220;This is our awesome plugin!&#8221; so let&#8217;s come up with some functionality to build. For this exercise, let&#8217;s create a simple plugin that allows truncates a blob of text to a specified length while providing the user an option show/hide the rest of the text. Since the most common character length limitation on the Internet these days is Twitter&#8217;s 140 characters, we&#8217;ll use that mark in our example.</p>
<p>Taking what we know about the basic jQuery plugin structure, let&#8217;s create the foundation for our new plugin &mdash; <code>slPlugin2</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">slPlugin2</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            length<span style="color: #339933;">:</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span>
            moreLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read more&quot;</span><span style="color: #339933;">,</span>
            lessLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;collapse&quot;</span><span style="color: #339933;">,</span>
            trailingText<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, we&#8217;ve established four default variables:</p>
<ul>
<li><code>length</code>: The length of the paragraph we want before we truncate the rest.</li>
<li><code>moreLength</code>: What we append to the paragraph when it is truncated. This will be the link the user clicks to expand the rest of the text.</li>
<li><code>lessLink</code>: What we append to the paragraph when it is expanded. This will be the link the user clicks to collapse the rest of the text.</li>
<li><code>trailingText</code>: The typical ellipses to append to the truncation.</li>
</ul>
<p>In our jQuery plugin example from Part 1, we started our function with <code>this.each(function() {</code>, and for this example, we&#8217;re going to add a return for <code>this</code> to maintain chainability. By doing so, we&#8217;re able to manipulate the segment with methods. For example, if we started our function with <code>this.each(function() {</code>, we&#8217;d call it with this line:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#ourParagraph'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slPlugin2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If we start the function with <code>return this.each(function() {</code>, we have the freedom to add further manipulation:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#ourParagraph'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slPlugin2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>With such a simple change, we&#8217;re able to add method calls to make one massive dynamic function. </p>
<p>Let&#8217;s flesh out the actual function a little more. We&#8217;ll add a substantial bit of code in this step, but you should be able to follow along with the changes via the comments:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">slPlugin2</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            length<span style="color: #339933;">:</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span> 
            moreLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read more&quot;</span><span style="color: #339933;">,</span>
            lessLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;collapse&quot;</span><span style="color: #339933;">,</span>
            trailingText<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// return this keyword for chainability</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> ourText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// the element we want to manipulate</span>
            <span style="color: #003366; font-weight: bold;">var</span> ourHtml <span style="color: #339933;">=</span> ourText.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//get the contents of ourText!</span>
            <span style="color: #006600; font-style: italic;">// let's check if the contents are longer than we want</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ourHtml.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> options.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> truncSpot <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #339933;">,</span> options.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// the location of the first space (so we don't truncate mid-word) where we will end our truncation.</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// make sure to ignore the first space IF the text starts with a space</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>truncSpot <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #006600; font-style: italic;">// the part of the text that will not be truncated, starting from the beginning</span>
       <span style="color: #003366; font-weight: bold;">var</span> firstText <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> truncSpot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">// the part of the text that will be truncated, minus the trailing space</span>
       <span style="color: #003366; font-weight: bold;">var</span> secondText <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>truncSpot<span style="color: #339933;">,</span> ourHtml.<span style="color: #660066;">legnth</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Are you still with us? I know it seems like a lot to take in, but each piece is very straightforward. The <code>firstText</code> is the chunk of text that will be shown: The first 140 characters (or whatever <code>length</code> you define). The <code>secondText</code> is what <em>will</em> be truncated. We have two blobs of text, and now we need to make them work together:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $.<span style="color: #660066;">fn</span>.<span style="color: #660066;">slPlugin2</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> defaults <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            length<span style="color: #339933;">:</span> <span style="color: #CC0000;">140</span><span style="color: #339933;">,</span> 
            moreLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read more&quot;</span><span style="color: #339933;">,</span>
            lessLink<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read less&quot;</span><span style="color: #339933;">,</span>
            trailingText<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;...&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> $.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>defaults<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// return this keyword for chainability</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> ourText <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// the element we want to manipulate</span>
            <span style="color: #003366; font-weight: bold;">var</span> ourHtml <span style="color: #339933;">=</span> ourText.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//get the contents of ourText!</span>
            <span style="color: #006600; font-style: italic;">// let's check if the contents are longer than we want</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ourHtml.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> options.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> truncSpot <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">' '</span><span style="color: #339933;">,</span> options.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// the location of the first space (so we don't truncate mid-word) where we will end our truncation.</span>
&nbsp;
   <span style="color: #006600; font-style: italic;">// make sure to ignore the first space IF the text starts with a space</span>
   <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>truncSpot <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
       <span style="color: #006600; font-style: italic;">// the part of the text that will not be truncated, starting from the beginning</span>
       <span style="color: #003366; font-weight: bold;">var</span> firstText <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> truncSpot<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">// the part of the text that will be truncated, minus the trailing space</span>
       <span style="color: #003366; font-weight: bold;">var</span> secondText <span style="color: #339933;">=</span> ourHtml.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>truncSpot<span style="color: #339933;">,</span> ourHtml.<span style="color: #660066;">legnth</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">// perform our truncation on our container ourText, which is technically more of a &quot;rewrite&quot; of our paragraph, to our liking so we can modify how we please. It's basically saying: display the first blob then add our trailing text, then add our truncated part wrapped in span tags (to further modify)</span>
       ourText.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>firstText <span style="color: #339933;">+</span> options.<span style="color: #660066;">trailingText</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;span class=&quot;slPlugin2&quot;&gt;'</span> <span style="color: #339933;">+</span> secondText <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/span&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
       <span style="color: #006600; font-style: italic;">// but wait! The secondText isn't supposed to show until the user clicks &quot;read more&quot;, right? Right! Hide it using the span tags we wrapped it in above.</span>
       ourText.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.slPlugin2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our function now truncates text to the specified length, and we can call it from our page simply:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.slPlugin2.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
$(document).ready(function() {  
    $('#slText').slPlugin2();  
});
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>Out of all the ways to truncate text via jQuery, this has to be my favorite. It&#8217;s feature-rich while still being fairly easy to understand. As you might have noticed, we haven&#8217;t touched on the &#8220;read more&#8221; and &#8220;read less&#8221; links or the expanding/collapsing animations yet, but we&#8217;ll be covering those in Part 3 of this series. Between now and when Part 3 is published, I challenge you to think up how you&#8217;d add those features to this plugin as homework. </p>
<p>-Cassandra</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/tips-and-tricks-building-a-jquery-plugin-part-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>From the Startup Trenches to the Catalyst War Room</title>
		<link>http://blog.softlayer.com/2013/from-the-startup-trenches-to-the-catalyst-war-room/</link>
		<comments>http://blog.softlayer.com/2013/from-the-startup-trenches-to-the-catalyst-war-room/#comments</comments>
		<pubDate>Tue, 12 Feb 2013 16:45:20 +0000</pubDate>
		<dc:creator>Andy Mui</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Introductions]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[bootstrapping]]></category>
		<category><![CDATA[Catalyst]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[development team]]></category>
		<category><![CDATA[entrepreneurs]]></category>
		<category><![CDATA[incubator]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[mentor]]></category>
		<category><![CDATA[PIE]]></category>
		<category><![CDATA[Portland]]></category>
		<category><![CDATA[resource]]></category>
		<category><![CDATA[Seattle]]></category>
		<category><![CDATA[startups]]></category>
		<category><![CDATA[SURF]]></category>
		<category><![CDATA[team]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10487</guid>
		<description><![CDATA[Before joining SoftLayer, I was locked in a dark, cold room for two years. Sustained by a diet of sugar and caffeine and basking in the glow of a 27&#8243; iMac, I was tasked with making servers dance to the tune of Ruby. The first few months were the toughest. The hours were long, and [...]]]></description>
			<content:encoded><![CDATA[<p>Before joining SoftLayer, I was locked in a dark, cold room for two years. Sustained by a diet of sugar and caffeine and basking in the glow of a 27&#8243; iMac, I was tasked with making servers dance to the tune of Ruby. The first few months were the toughest. The hours were long, and we worked through holidays. And I loved it.</p>
<p>If that work environment seems like torture, you probably haven&#8217;t been on the front lines of a development team. I was a member of a band of brothers at war with poorly documented vendor APIs, trying to emerge victorious from the Battle of Version 1.0. We operated (and suffered) like a startup in its early stages, so I&#8217;ve had firsthand experience with the ups and downs of creating and innovating in technology. Little did I know that those long hours and challenges were actually preparing me to help hundreds of other developers facing similar circumstances &#8230; I was training to be a <a href="http://www.softlayer.com/partners/catalyst">Catalyst</a> SLayer:</p>
<p><a href="http://www.softlayer.com/partners/catalyst/team" target="_blank"><img class="centered" style="border:1px solid black;" src="http://cdn.softlayer.com/innerlayer/catalyst_andy.jpg" alt="Catalyst Team"/></a></p>
<p>You probably know a lot about Catalyst by now, but one of the perks of the program that often gets overshadowed by &#8220;free hosting&#8221; is the mentorship and feedback the SoftLayer team provides every Catalyst participant. Entrepreneurs bounce ideas off of guys like Paul Ford and George Karidis to benefit from the years of experience and success we&#8217;ve experienced, and the more technical folks can enlist our help in figuring out more efficient ways to tie their platforms to their infrastructure.</p>
<p>When I was forging through the startup waters, I was fortunate to have been supported by financially reinforced walls and the skilled engineers of a well-established hosting company in Tokyo. Unfortunately, that kind of support is relatively uncommon. That&#8217;s where Catalyst swoops in. SoftLayer&#8217;s roots were planted in the founders&#8217; living rooms and garages, so we&#8217;re particularly fond of other companies who are bootstrapping, learning from failure and doing whatever it takes to succeed. In my role with Catalyst, I&#8217;ve effectively become a resource for hundreds of startups around the world &#8230; and that feels good.</p>
<p>Five days before my official start date, I receive a call from <a href="http://blog.softlayer.com/author/jkrammes/">Josh</a> telling me that we&#8217;d be spending my first official week on the job in Seattle with <a href="http://www.surfincubator.com/">Surf Incubator</a> and Portland with <a href="http://www.piepdx.com/">Portland Incubator Experiment</a> (PIE). While the trip did not involve carving waves or stuffing our faces with baked goods (bummer), we did get to hear passionate people explain what keeps them up at night. We got to share a little bit about SoftLayer and how we can help them sleep better (or fuel them with more energy when they&#8217;re up at night &#8230; depending on which they preferred), and as I headed back to Los Angeles, I knew I made the right choice to become a SLayer. I&#8217;m surrounded by energy, creativity, passion, innovation and collaboration on a daily basis. It&#8217;s intoxicating.</p>
<p><strong>TL;DR</strong>: I love my job.    </p>
<p>-<a href="https://twitter.com/andy_mui">@andy_mui</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/from-the-startup-trenches-to-the-catalyst-war-room/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web Development &#8211; JavaScript Packaging</title>
		<link>http://blog.softlayer.com/2013/web-development-javascript-packaging/</link>
		<comments>http://blog.softlayer.com/2013/web-development-javascript-packaging/#comments</comments>
		<pubDate>Thu, 10 Jan 2013 20:15:05 +0000</pubDate>
		<dc:creator>Philip Thompson</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[dependencies]]></category>
		<category><![CDATA[efficient]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Mootools]]></category>
		<category><![CDATA[object oriented]]></category>
		<category><![CDATA[organization]]></category>
		<category><![CDATA[packaging]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[version control]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10401</guid>
		<description><![CDATA[If you think of JavaScript as the ugly duckling of programming languages, think again! It got a bad rap in the earlier days of the web because developers knew enough just to get by but didn&#8217;t really respect it like they did Java, PHP or .Net. Like other well-known and heavily used languages, JavaScript contains [...]]]></description>
			<content:encoded><![CDATA[<p>If you think of JavaScript as the ugly duckling of programming languages, think again! It got a bad rap in the earlier days of the web because developers knew enough just to get by but didn&#8217;t really respect it like they did Java, PHP or .Net. Like other well-known and heavily used languages, JavaScript contains various data types (String, Boolean, Number, etc.), objects and functions, and it is even capable of inheritance. Unfortunately, that functionality is often overlooked, and many developers seem to implement it as an afterthought: &#8220;Oh, we need to add some neat jQuery effects over there? I&#8217;ll just throw some inline JavaScript here.&#8221; That kind of implementation perpetuates a stereotype that JavaScript code is unorganized and difficult to maintain, but it doesn&#8217;t have to be! I&#8217;m going to show you how easy it is to maintain and organize your code base by packaging your JavaScript classes into a single file to be included with your website.</p>
<p>There are a few things to cover before we jump into code: </p>
<ol>
<li><strong>JavaScript Framework</strong> &#8211; <a href="http://mootools.net/">Mootools</a> is my framework of choice, but you can use whatever <a href="http://en.wikipedia.org/wiki/Comparison_of_JavaScript_frameworks">JavaScript framework</a> you&#8217;d like.</li>
<li><strong>Classes</strong> &#8211; Because I see JavaScript as another programming language that I respect (and is capable of object-oriented-like design), I write classes for EVERYTHING. Don&#8217;t think of your JavaScript code as something you use once and throw away. Write your code to be generic enough to be reused wherever it&#8217;s placed. Object-oriented design is great for this! Mootools makes object-oriented design easy to do, so this point reinforces the point above.</li>
<li><strong>Class Files</strong> &#8211; Just like you&#8217;d organize your PHP to contain one class per file, I do the exact same thing with JavaScript. <em>Note: Each of the class files in the example below uses the class name appended with <code>.js</code>.</em></li>
<li><strong>Namespacing</strong> &#8211; I will be organizing my classes in a way that will only add a single property &mdash; PT &mdash; to the global namespace. I won&#8217;t get into the details of namespacing in this blog because I&#8217;m sure you&#8217;re already thinking, &#8220;The code! The code! Get on with it!&#8221; You can namespace whatever is right for your situation.</li>
</ol>
<p>For this example, our classes will be food-themed because &#8230; well &#8230; I enjoy food. Let&#8217;s get started by creating our base object:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
---
name: PT
description: The base class for all the custom classes
authors: [Philip Thompson]
provides: [PT]
...
*/</span>
<span style="color: #003366; font-weight: bold;">var</span> PT <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We now have an empty object from which we&#8217;ll build all of our classes. We&#8217;ll go I will go into more details later about the comment section, but let&#8217;s build our first class: <strong>PT.Ham</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
---
name: PT.Ham
description: The ham class
authors: [Philip Thompson]
requires: [/PT]
provides: [PT.Ham]
...
*/</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    PT.<span style="color: #660066;">Ham</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// Custom code here...</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As I mentioned in point three (above), PT.Ham should be saved in the file named PT.Ham.js. When we create second class, <strong>PT.Pineapple</strong>, we&#8217;ll store it in PT.Pineapple.js:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
---
name: PT.Pineapple
description: The pineapple class
authors: [Philip Thompson]
requires: [/PT]
provides: [PT.Pineapple]
...
*/</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    PT.<span style="color: #660066;">Pineapple</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// Custom code here...</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our final class for this example will be <strong>PT.Pizza</strong> (I&#8217;ll let you guess the name of the file where PT.Pizza lives). Our PT.Pizza class will require that PT, PT.Ham and PT.Pineapple be present.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
---
name: PT.Pizza
description: The pizza class
authors: [Philip Thompson]
requires: [/PT, /PT.Ham, /PT.Pineapple]
provides: [PT.Pizza]
...
*/</span>
&nbsp;
<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    PT.<span style="color: #660066;">Pizza</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// Custom code here that uses PT.Ham and PT.Pineapple...</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Before we go any further, let&#8217;s check out the comments we include above each of the classes. The comments are formatted for <a href="http://www.yaml.org/">YAML</a> &mdash; YAML Ain&#8217;t Markup Language (you gotta love recursive acronyms). These comments allow our parser to determine how our classes are related, and they help resolve dependencies. YAML&#8217;s pretty easy to learn and you only need to know a few basic features to use it. The YAML comments in this example are essential for our JavaScript package-manager &mdash; <a href="https://github.com/anutron/packager">Packager</a>. I won&#8217;t go into all the details about Packager, but simply mention a few commands that we&#8217;ll need to build our single JavaScript file.</p>
<p>In addition to the YAML comments in each of the class files, we also need to create a YAML file that will organize our code. This file &mdash; <strong>package.yml</strong> for this example &mdash; is used to load our separate JavaScript classes:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;PT&quot;</span>
description<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Provides our fancy PT classes&quot;</span>
authors<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;[Philip Thompson]&quot;</span>
version<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;1.0.0&quot;</span>
sources<span style="color: #339933;">:</span>
    <span style="color: #339933;">-</span> js<span style="color: #339933;">/</span>PT.<span style="color: #660066;">js</span>
    <span style="color: #339933;">-</span> js<span style="color: #339933;">/</span>PT.<span style="color: #660066;">Ham</span>.<span style="color: #660066;">js</span>
    <span style="color: #339933;">-</span> js<span style="color: #339933;">/</span>PT.<span style="color: #660066;">Pineapple</span>.<span style="color: #660066;">js</span>
    <span style="color: #339933;">-</span> js<span style="color: #339933;">/</span>PT.<span style="color: #660066;">Pizza</span>.<span style="color: #660066;">js</span></pre></div></div>

<p>package.yml shows that all of our PT* files are located in the js directory, one directory up from the package.yml file. Some of the properties in the YAML file are optional, and you can add much more detail if you&#8217;d like, but this will get the job done for our purposes. </p>
<p>Now we&#8217;re ready to turn back to Packager to build our packaged file. Packager includes an option to use PHP, but we&#8217;re just going to do it command-line. First, we need to register the new package (package.yml) we created for PT. If our JavaScript files are located in <code>/path/to/web/directory/js</code>, the package.yml file is in <code>/path/to/web/directory</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>packager register <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>web<span style="color: #000000; font-weight: bold;">/</span>directory</pre></div></div>

<p>This finds our package.yml file and registers our PT package. Now that we have our package registered, we can build it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>packager build <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>web<span style="color: #000000; font-weight: bold;">/</span>directory<span style="color: #000000; font-weight: bold;">/</span>js<span style="color: #000000; font-weight: bold;">/</span>PT.all.js</pre></div></div>

<p>The Packager sees that our PT package is registered, so it looks at each of the individual class files to build a single large file. In the comments of each of the class files, it determines if there are dependencies and warns you if any are not found. </p>
<p>It might seem like a lot of work when it&#8217;s written out like this, but I can assure you that when you go through the process, it takes no time at all. The huge benefit of packaging our JavaScript is evident as soon as you start incorporating those JavaScript classes into your website &#8230; Because we have built all of our class files into a single file, we don&#8217;t need to include each of the individual JavaScript files into our website (much less include the inline JavaScript declarations that make you cringe). To streamline your implementation even further if you&#8217;re using your JavaScript package in a production deployment, I recommend that you <a href="http://blog.softlayer.com/2011/html5-javascript-web-development-part-i/">&#8220;minify&#8221;</a> your code as well.</p>
<p>See &#8230; Organized code is no longer just for server-side only languages. Treat your JavaScript kindly, and it will be your friend!</p>
<p>Happy coding!</p>
<p>-Philip</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2013/web-development-javascript-packaging/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using SoftLayer Object Storage to Back Up Your Server</title>
		<link>http://blog.softlayer.com/2012/using-softlayer-object-storage-to-back-up-your-server/</link>
		<comments>http://blog.softlayer.com/2012/using-softlayer-object-storage-to-back-up-your-server/#comments</comments>
		<pubDate>Thu, 27 Dec 2012 21:40:01 +0000</pubDate>
		<dc:creator>Ronald Steelman</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Backups]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[object storage]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[SolusVM]]></category>
		<category><![CDATA[storage]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=9795</guid>
		<description><![CDATA[Before I came to my senses and moved my personal servers to SoftLayer, I was one of many victims of a SolusVM exploit that resulted in the wide-scale attack of many nodes in my previous host&#8217;s Chicago data center. While I&#8217;m a firm believer in backing up my data, I could not have foreseen the [...]]]></description>
			<content:encoded><![CDATA[<p>Before I came to my senses and moved my personal servers to SoftLayer, I was one of many victims of a SolusVM exploit that resulted in the wide-scale attack of many nodes in my previous host&#8217;s Chicago data center. While I&#8217;m a firm believer in backing up my data, I could not have foreseen the situation I was faced with: Not only was my server in one data center compromised with all of its data deleted, but my backup server in one of the host&#8217;s other data centers was also attacked &#8230; This left me with old, stale backups on my local computer and not much else. I quickly relocated my data and decided that I should use <a href="http://www.softlayer.com/cloudlayer/storage/">SoftLayer Object Storage</a> to supplement and improve upon my backup and disaster recovery plans.</p>
<p>With <a href="https://github.com/softlayer/softlayer-object-storage-python">SoftLayer Object Storage Python Client</a> set up and the SoftLayer Object Storage Backup script &mdash; <a href="http://sldn.softlayer.com/blog/klandreth/Deglazing-slbackuppy-Usage-Object-Storage-Kitchen">slbackup.py</a> &mdash; in hand, I had the tools I needed to build a solid backup infrastructure easily. On Linux.org, I contributed an article about how to <a href="http://www.linux.org/article/view/mysql-backups-using-softlayer-object-storage">perform MySQL backups</a> with those resources, so the database piece is handled, but I also need to back up my web files, so I whipped up another quick <code>bash</code> script to run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The path the backups will be dumped to</span>
<span style="color: #007800;">DUMP_DIR</span>=<span style="color: #ff0000;">&quot;/home/backups/&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Path to the web files to be backed up</span>
<span style="color: #007800;">BACKUP_PATH</span>=<span style="color: #ff0000;">&quot;/var/www/sites /&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Back up folder name (mmddyyyy)</span>
<span style="color: #007800;">BACKUP_DIR</span>=<span style="color: #ff0000;">&quot;<span style="color: #780078;">`date +%m%d%Y`</span>&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Backup File Name</span>
<span style="color: #007800;">DUMP_FILE</span>=<span style="color: #ff0000;">&quot;<span style="color: #780078;">`date +%m_%d_%Y_%H_%M_%S`</span>_site_files&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># SL container name</span>
<span style="color: #007800;">CONTAINER</span>=<span style="color: #ff0000;">&quot;site_backups&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Create backup dir if doesn't exist</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$DUMP_DIR</span><span style="color: #007800;">$BACKUP_DIR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #007800;">$DUMP_DIR</span><span style="color: #007800;">$BACKUP_DIR</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-zcvpf</span> <span style="color: #007800;">$DUMP_DIR</span><span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DUMP_FILE</span>.tar.gz <span style="color: #007800;">$BACKUP_PATH</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Make sure the archive exists</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$DUMP_DIR</span><span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$DUMP_FILE</span>.tar.gz <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>slbackup.py <span style="color: #660033;">-s</span> <span style="color: #007800;">$DUMP_DIR</span><span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CONTAINER</span>&quot;</span> <span style="color: #660033;">-r</span> <span style="color: #000000;">30</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Remove the backup stored locally</span>
        <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #007800;">$DUMP_DIR</span><span style="color: #007800;">$BACKUP_DIR</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Success</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$DUMP_DIR</span><span style="color: #007800;">$BACKUP_DIR</span>/<span style="color: #007800;">$DUMP_FILE</span>.tar.gz does not exist.&quot;</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>It&#8217;s not the prettiest <code>bash</code> script, but it gets the job done. By tweaking a few variables, you can easily generate backups for any important directory of files and push them to your SoftLayer Object Storage account. If you want to change the retention time of your backups to be longer or shorter, you can change the <code>30</code> after the <code>–r</code> in the line below to the number of days you want to keep each backup:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>slbackup.py <span style="color: #660033;">-s</span> <span style="color: #007800;">$DUMP_DIR</span><span style="color: #007800;">$BACKUP_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-o</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CONTAINER</span>&quot;</span> <span style="color: #660033;">-r</span> <span style="color: #000000;">30</span></pre></div></div>

<p>I created a script for each website on my server, and I set a CRON (crontab –e) entry to run each one on Sundays staggered by 5 minutes:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">5</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">0</span>  <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>cron<span style="color: #000000; font-weight: bold;">/</span>CRON-site1.com_web_files <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
<span style="color: #000000;">10</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">0</span>  <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>cron<span style="color: #000000; font-weight: bold;">/</span>CRON-site2.com_web_files <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null
<span style="color: #000000;">15</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">0</span>  <span style="color: #000000; font-weight: bold;">/</span>root<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>cron<span style="color: #000000; font-weight: bold;">/</span>CRON-site3.com_web_files <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null</pre></div></div>

<p>If you&#8217;re looking for an easy way to automate and solidify your backups, this little bit of code could make life easier on you. Had I taken the few minutes to put this script together prior to the attack I experienced at my previous host, I wouldn&#8217;t have lost any of my data. It&#8217;s easy to get lulled into &#8220;backup apathy&#8221; when you don&#8217;t <em>need</em> your backups, but just because nothing *has* happened to your data doesn&#8217;t mean nothing *can* happen to your data.</p>
<p>Take it from me &#8230; Be over-prepared and save yourself a lot of trouble.</p>
<p>-Ronald</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/using-softlayer-object-storage-to-back-up-your-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SoftLayer API: Streamline. Simplify.</title>
		<link>http://blog.softlayer.com/2012/softlayer-api-streamline-simplify/</link>
		<comments>http://blog.softlayer.com/2012/softlayer-api-streamline-simplify/#comments</comments>
		<pubDate>Wed, 19 Dec 2012 19:00:34 +0000</pubDate>
		<dc:creator>Kevin Hazard</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SoftLayer]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[coders]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[developers]]></category>
		<category><![CDATA[efficient]]></category>
		<category><![CDATA[improvement]]></category>
		<category><![CDATA[method]]></category>
		<category><![CDATA[order]]></category>
		<category><![CDATA[provision]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[SLAPI]]></category>
		<category><![CDATA[streamline]]></category>

		<guid isPermaLink="false">http://blog.softlayer.com/?p=10205</guid>
		<description><![CDATA[Building an API is a bit of a balancing act. You want your API to be simple and easy to use, and you want it to be feature-rich and completely customizable. Because those two desires happen to live on opposite ends of the spectrum, every API finds a different stasis in terms of how complex [...]]]></description>
			<content:encoded><![CDATA[<p>Building an API is a bit of a balancing act. You want your API to be simple and easy to use, <em>and</em> you want it to be feature-rich and completely customizable. Because those two desires happen to live on opposite ends of the spectrum, every API finds a different stasis in terms of how complex and customizable they are. The <a href="https://sldn.softlayer.com/reference/softlayerapi">SoftLayer API</a> was designed to provide customers with granular control of every action associated with any product or service on our platform; anything you can do in our customer portal can be done via our API. That depth of functionality might be intimidating to developers looking to dive in quickly and incorporate the SoftLayer platform into their applications, so our development team has been working to streamline and simplify some of the most common API services to make them even more accessible.</p>
<p><a href="https://sldn.softlayer.com/reference/softlayerapi"><img class="centered" src="http://cdn.softlayer.com/innerlayer/slapi.jpg" alt="SoftLayer API"/></a></p>
<p>To get an idea of what their efforts look like in practice, Phil posted an SLDN blog with a perfect example of how they <a href="https://sldn.softlayer.com/blog/phil/Simplified-CCI-Creation">simplified cloud computing instance (CCI) creation</a> via the API. The traditional CCI ordering process required developers to define nineteen data points: </p>
<div style="margin-left:20px;"><code>Hostname<br />
Domain name<br />
complexType<br />
Package Id<br />
Location Id<br />
Quantity to order<br />
Number of cores<br />
Amount of RAM<br />
Remote management options<br />
Port speeds<br />
Public bandwidth allotment<br />
Primary subnet size<br />
Disk size<br />
Operating system<br />
Monitoring<br />
Notification<br />
Response<br />
VPN Management - Private Network<br />
Vulnerability Assessments &#038; Management</code></div>
<p>While each of those data points is straightforward, you still have to define <em>nineteen</em> of them. You have all of those options when you check out through our shopping cart, so it makes sense that you&#8217;d have them in the API, but when it comes to ordering through the API, you don&#8217;t necessarily <strong>need</strong> all of those options. Our development team observed our customers&#8217; API usage patterns, and they created the slimmed-down and efficient <a href="https://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/createObject">SoftLayer_Virtual_Guest::createObject</a> &mdash; a method that only requires seven data points:</p>
<div style="margin-left:20px;"><code>Hostname<br />
Domain name<br />
Number of cores<br />
Amount of RAM<br />
Hourly/monthly billing<br />
Local vs SAN disk<br />
Operating System</code></div>
<p>Without showing you a single line of code, you see the improvement. Default values were established for options like <code>Port speeds</code> and <code>Monitoring</code> based on customer usage patterns, and as a result, developers only have to provide half the data to place a new CCI order. Because each data point might require multiple lines of code, the volume of API code required to place an order is slimmed down even more. The best part is that if you find yourself needing to modify one of the now-default options like <code>Port speeds</code> or <code>Monitoring</code>, you still can!</p>
<p>As the development team finds other API services and methods that can be streamlined and simplified like this one, they&#8217;ll ninja new solutions to make the API even more accessible. Have you tried coding to the SoftLayer API yet? If not, what&#8217;s the biggest roadblock for you? If you&#8217;re already a SLAPI coder, what other methods do you use often that could be streamlined?</p>
<p>-<a href="http://twitter.com/khazard">@khazard</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.softlayer.com/2012/softlayer-api-streamline-simplify/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
