<?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>Blog</title>
	<atom:link href="http://guillermocifuentes.com/en/feed/" rel="self" type="application/rss+xml" />
	<link>http://guillermocifuentes.com/en/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 04 Jul 2009 17:30:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Serializing JavaScriptObject objects in GWT</title>
		<link>http://guillermocifuentes.com/en/2009/03/16/serializing-javascriptobject-objects-in-gwt/</link>
		<comments>http://guillermocifuentes.com/en/2009/03/16/serializing-javascriptobject-objects-in-gwt/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 10:55:43 +0000</pubDate>
		<dc:creator>guillermo.cifuentes</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[gwt]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://guillermocifuentes.com/?p=61</guid>
		<description><![CDATA[While writing a REST service (in JAX-RS implementation), I needed to create and modify resources using JSON as the exchange format. The client-side is written in GWT. In GWT when you want to convert a string to a Javascript object, you can use eval and it&#8217;s done. But what about serializing objects into strings? Now [...]]]></description>
			<content:encoded><![CDATA[<p>While writing a REST service (in JAX-RS implementation), I needed to create and modify resources using JSON as the exchange format. The client-side is written in GWT. In GWT when you want to convert a string to a Javascript object, you can use <em>eval</em> and it&#8217;s done.</p>
<p>But what about serializing objects into strings? Now supposing you&#8217;re sending data from a <em>FormHandler</em>, in the <em>onSubmit</em> method, you may want to set the values in the Javascript object and later send it as a string with <em>RequestBuilder.sendRequest</em>. GWT-Ext provides tools for the task, but maybe you&#8217;re not using them, so your question at this point would be: must I code a JSON tokenizer in Java for this? (yeah, you&#8217;re org.json library won&#8217;t work)</p>
<p>No, not at all, since you can use any external JS library from GWT. So go ahead and grab a normal Javascript to do the job, in example, <a href="http://www.json.org/json2.js">Json2.js</a>.  Put the file in a directory reachable by the webrowser and load it with a <em>script</em> tag. The last step is writing an interface for your java code:</p>
<pre class="brush: java;">
import com.google.gwt.core.client.JavaScriptObject;

public class JSON {

    protected JSON() {}

    public static final native String encode(JavaScriptObject object) /*-{
        /** if you use a different  library, check the method beneath **/
        return $wnd.JSON.stringify(object);
    }-*/;
    public static final native &lt;T&gt; T decode(String json) /*-{
        return eval('('+json+')');
    }-*/;

}
</pre>
<p>When you want to refer to an external Javascript object, you must prefix the access to the object by <strong>$wnd</strong>. This way you tell GWT that your object is located in <em>Window</em>. Anyway, if you forgot to add it you&#8217;d have a compilation error, so you&#8217;d spotted it quickly. You have more information about the JNSI interface in <a href="http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-gwt-part-1-jsni.html">this page</a>.</p>
<blockquote><p><strong>Note:</strong> In the <em>decode</em> method, there&#8217;s a generic been used so the compiler can guess the type of the left side when you do an assignation. So take care to which type you&#8217;re doing the assignation before you get a conflict type error.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://guillermocifuentes.com/en/2009/03/16/serializing-javascriptobject-objects-in-gwt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Typo plugin for GitHub</title>
		<link>http://guillermocifuentes.com/en/2009/03/09/typo-plugin-for-github/</link>
		<comments>http://guillermocifuentes.com/en/2009/03/09/typo-plugin-for-github/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 19:15:11 +0000</pubDate>
		<dc:creator>guillermo.cifuentes</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[typo]]></category>

		<guid isPermaLink="false">http://guillermocifuentes.com/?p=70</guid>
		<description><![CDATA[When I was starting up this blog, I missed a plugin which would work in the server-side to show the public repositories I had in GitHub, as I didn&#8217;t find anything, I wrote my own. The steps I followed: As Typo is a Rails&#8217; application, the process to create a plugin is well-known, move to [...]]]></description>
			<content:encoded><![CDATA[<p>When I was starting up this blog, I missed a plugin which would work in the server-side to show the public repositories I had in GitHub, as I didn&#8217;t find anything, I wrote my own.</p>
<p>The steps I followed:</p>
<ol>
<li>As Typo is a Rails&#8217; application, the process to create a plugin is well-known, move to your Typo root directory and type in the shell to create the plugin structure:
<pre class="brush: bash; light: true;">
                guillermo:typo$ script/generate plugin github_sidebar
            </pre>
</li>
<li>So let&#8217;s start editing the file which will hold most of the functionality:
<pre class="brush: bash; light: true;">
                guillermo:typo$ vim github_sidebar.rb
            </pre>
<p>Typo has a nice class to inherit by sidebars, so you&#8217;ve little work to do. By the way, if you want to see how other plugins implement sidebars, like the tags sidebar, or the amazon&#8217;s one, you have them in <em>RAILS_ROOT/vendor/plugins</em>. Let&#8217;s code!</p>
<pre class="brush: ruby;">
require 'net/http'
require 'uri'

class GithubSidebar &lt; Sidebar
  display_name &quot;Github&quot; #the name of the plugin in the plugins' list
  description &quot;Shows all the public projects in GitHub for a user&quot;
  # next are the preferences, they're saved in the database, but you don't have to mess around with ActiveRecord
  # the first symbol, will create an attribute accessor with such name
  setting :title, 'Github projects', :label =&gt; &quot;Sidebar title&quot;
  setting :user, 'githubuser', :label =&gt; &quot;Github username&quot;
  setting :refresh, '300', :label =&gt; &quot;Time to purge the cache&quot;

  USER_INFO_BASE = &quot;http://github.com/api/v1/json/&quot;

  def repositories
    data = retrieve_reposdata
    return [] if data.length &lt; 2
    user = JSON.parse data
    user[&quot;user&quot;][&quot;repositories&quot;]
  end

  private

  def retrieve_reposdata
    now = Time.now.to_i
    timestamp = Rails.cache.read('GithubSidebar_repos_stamp').to_i || refresh.to_i
    data = Rails.cache.read('GithubSidebar_repos')

    unless data and (now - timestamp) &lt; refresh.to_i
      final_url = USER_INFO_BASE + user
      response = Net::HTTP.get_response(URI.parse(final_url))
      data = response.body

      Rails.cache.write 'GithubSidebar_repos', data
      Rails.cache.write 'GithubSidebar_repos_stamp', now.to_s
    end
    data
  end
end
</pre>
<p>If you take a look to <em>retrieve_reposdata</em> method, instead of saving the ruby object in the cache, it&#8217;s saved the string sent by GitHub (<a href="http://github.com/guides/the-github-api">more information about the GitHub Api</a>). This is because most backends just store strings but not objects.</p>
<p>The timeout is saved in a different key because I couldn&#8217;t find a way to specify the timeout for an item.
        </li>
<li>
            We&#8217;re done with the model, so let&#8217;s write the view. First we need to tell Typo where it&#8217;s our view, so in <em>init.rb</em> add:</p>
<pre class="brush: ruby;">
require 'github_sidebar'
GithubSidebar.view_root = File.dirname(__FILE__) + &quot;/views&quot;
           </pre>
<p>Two things to take care about: our model will be in scope as <strong>sidebar</strong> independently how we name our model&#8217;s class name<strong>sidebar</strong> and we must call our view <strong>content.rhtml</strong>.</p>
<p>So we create the <em>views</em> folder in our plugin&#8217;s directory and edit <em>views/content.rhtml</em>:</p>
<pre class="brush: ruby; html-script: true;">
&lt;h3&gt;&lt;%= sidebar.title %&gt;&lt;/h3&gt;
&lt;ul id=&quot;github_repos&quot;&gt;
&lt;%- sidebar.repositories.each do |repo| %&gt;
&lt;li&gt;&lt;a href=&quot;&lt;%= repo[&quot;url&quot;] %&gt;&quot; title=&quot;&lt;%= repo[&quot;description&quot;] %&gt;&quot;&gt;&lt;%= repo[&quot;name&quot;] %&gt;&lt;/a&gt;&lt;/li&gt;
&lt;%- end %&gt;
&lt;/ul&gt;
</pre>
</li>
</ol>
<p>You&#8217;re done. Now just restart your application server.</p>
<p>If you&#8217;re too lazy to follow the steps above and you just want to use it, you can grab it from GitHub:</p>
<pre class="brush: bash; light: true;">
guillermo:typo$ script/plugin install git://github.com/guicifuentes/typo-github-sidebar.git
</pre>
]]></content:encoded>
			<wfw:commentRss>http://guillermocifuentes.com/en/2009/03/09/typo-plugin-for-github/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
