Read my latest article: 8 things I look for in a Ruby on Rails app (posted Thu, 06 Jul 2017 16:59:00 GMT)

The HTTParty has just begun

Posted by Thu, 27 Nov 2008 00:54:00 GMT

After releasing the new RubyURL API, I decided that it was time to look around at libraries to interact with it. I came across a new Ruby gem from John Nunemaker named, HTTParty, which aims to make it easy to talk to XML and JSON-based web services. Be sure to read John’s announcement of HTTParty.

So, I decided it might be fun to introduce more people to the gem by showing you all how to use it to talk to the new RubyURL API.

Install HTTParty

Before we get started, you’ll need to install the HTTParty gem with the following command:


   ~ : sudo gem install httparty
  Password:
  When you HTTParty, you must party hard!
  Successfully installed httparty-0.1.6
  1 gem installed
  Installing ri documentation for httparty-0.1.6...
  Installing RDoc documentation for httparty-0.1.6...

Great! Now that we’re ready to party hard, let’s build something.

Talking to the RubyURL API

The RubyURL API currently supports both XML and JSON, which are each supported by HTTParty. The great thing about HTTParty is that all you need to do is include it in a class and you’re able to quickly talk to remote services.

In this following example, we’re going to create a new class called Rubyurl.

class Rubyurl
end

What we’ll want to do now is include the HTTParty library. (note: you’ll need to require both rubygems and httparty gems and I’ll skip those lines in following code samples)

class Rubyurl
  include HTTParty
end

The HTTParty provides a few class methods, which we can use to configure our library. We’ll go ahead and specify the base_uri, which we’ll set to rubyurl.com.

class Rubyurl
  include HTTParty
  base_uri 'rubyurl.com'
end

Now that our class is setup to talk to the Rubyurl.com site, we’ll want to add a new method which we can use to communicate with the RubyURL API. We’ll call this shorten as we’re using RubyURL to shorten long URLs… right?

class Rubyurl
  include HTTParty
  base_uri 'localhost:3000'

  def self.shorten( website_url )
  end
end

Our new shorten method will expect us to provide it with a website url, which we’ll want RubyURL to return a shortened URL for. The PATH for the API that we’ll want to talk to is: /api/links, which we’re expected to pass XML or JSON to.

Here are two examples of using the RubyURL API with HTTParty.

RubyURL via JSON w/HTTParty

We’re going to use the post method that is provided with HTTParty to send a request to /api/links.json. As you can see, we’re providing the original website url to the web service.

class Rubyurl
  include HTTParty
  base_uri 'rubyurl.com'

  def self.shorten( website_url )
    post( '/api/links.json', :query => { :link => { :website_url => website_url } } )
  end
end

When ran, it’ll produce the following:

  >> Rubyurl.shorten( 'http://github.com/jnunemaker/httparty/tree/master/lib/httparty.rb' ).inspect
  => {"link"=>{"permalink"=>"http://rubyurl.com/uJVu", "website_url"=>"http://github.com/jnunemaker/httparty/tree/master/lib/httparty.rb"}}

Pretty simple, eh?

RubyURL via XML w/HTTParty

The great thing about HTTParty is that you can use XML without changing much.

class Rubyurl
  include HTTParty
  base_uri 'rubyurl.com'

  def self.shorten( website_url )
    post( '/api/links.xml', :query => { :link => { :website_url => website_url } } )
  end
end

Produces the following

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<link>
  <website_url>http://github.com/jnunemaker/httparty/tree/master/lib/httparty.rb</website_url>
  <permalink>http://rubyurl.com/uJVu</permalink>
</link>

Closing thoughts

So… there you have it. HTTParty makes it extremely easy to interact with various web services that work over HTTP. I’d encourage you all to take a few minutes to experiment with it and see what crazy ideas that come to mind during the process. :-)

The new RubyURL API

Posted by Sun, 31 Aug 2008 17:55:00 GMT

We’ve just deployed the initial version of an API for RubyURL. It makes it really easy to create RubyURLs and is now open to the public. Should it end up being abused, we’ll consider introducing an API KEY for authenticating and tracking abuse.

In the meantime, you can now start to use the RubyURL API.

For example, the following…


$ curl  -i \ 
        -X POST \
        -H 'Content-Type: application/xml' \
        -d '<link><website_url>http://github.com/robbyrussell</website_url></link>' \
        http://rubyurl.com/api/links  


	

...would return the following response.

I’ll be updating the ShortURL gem in the coming days (unless someone else wants to patch it first wink) to take advantage of new API, versus how it’s currently creating RubyURLs.

You can see the code & changes for this new API on the RubyURL github site.

Update with JSON

I took a little time today to update the API and extend it to support JSON. So… you can now use the RubyURL API to generate RubyURLs via JSON. (see commits)

Enjoy! If you’re using RubyURL via the new API, I’d love to hear about it. :-)

Putting Tumblr to work for you

Posted by Wed, 19 Dec 2007 19:27:00 GMT

I’ve been using Tumblr off and on since early April. I tend to neglect it because I’ve found the interface a bit clumsy. The recent redesign hasn’t improved on the things that I consider obstacles in getting things quickly added to my tumblr. Since the concept behind the tumblr is to quickly share things with people, the interface doesn’t facilitate this workflow as quickly as I think it could.

In any event, I tend to not login to my dashboard very often (few times a week?), which means that I don’t post as often as I’d like.

A few months ago, I finally started to use the Feeds feature in tumblr, which will automatically add things to your tumblr from an RSS feed.

The first feed that I started to use was my Flickr account.

Flickr to Tumblr

This solution for this was to tag photos that I want Tumblr to automatically with ‘to:tumblr’.

Next, you just need to grab the URL for the Flickr RSS feed for photos tagged with to:tumblr.

Next, you’ll want to add this feed to Tumblr.

Voila… in a little while, your photos tagged with to:tumblr will start to show up on your Tumblr.

Del.icio.us to Tumblr

Links to blog articles, web sites, etc… are things that I generally use Deli.cio.us for… so doing it once for each was taking too much. So, I’m now using the same tagging formula with Del.icio.us to get Tumblr to automatically add links to my Tumblr.

Now, I can use the RSS feed for items tagged with to:tumblr on my del.icio.us account with Tumblr.

Pretty simple. :-)

Google Reader to Tumblr

Another place that I find myself wanting to post to Tumblr is from within Google Reader. Well, it’s actually really easy to do this by taking advantage of the RSS feed that Google Reader provides for your Shared Items.

google reader shared

Just grab the RSS feed from here…

Then add this RSS feed to Tumblr like the examples above.

Putting Tumblr to work for You!

I’m hoping to continue using this pattern with other sites as well. I’d be interested in hearing how other people are using Tumblr to aggregate focused content in an easy to browse way.

Update

It appears that Tumblr currently only allows you to use five feeds to import. Until they upgrade this limit, you can use Yahoo! Pipes to do some of the heavy lifting.