Twitter Ruby Gem Experiments ..

Posted by dave
on April 02, 2007 @ 12:39 PM
Curious about the recent fuss about Twitter, I installed the Ruby Twitter gem over the weekend.


>gem install Hpricot
>gem install twitter

It has a dependency on the Ruby XML parsing API Hpricot. Its the first time I've played around with Hpricot, and the API is nice. (ActiveResource would do well to use it also as ActiveResources current XML to hash implementation is quite brittle).

In terms of configuration, the gem creates a /.twitter file in your home directory. On windows this is below %HOMEPATH%. Once you add your username and password to the config file, you can then post to twitter from a command shell anytime you like:


>twitter post "Post to my twitter account using ruby"

The only gotcha you have to look out for is that theres a bug in Hpricot when processing XML that contains <text> elements. I just commented out the offending line below:

<ruby>/lib/ruby/gems/1.8/gems/twitter-0.1.0/lib/twitter/status.rb


     def new_from_xml(xml)
        Status.new do |s|
          s.id         = (xml).at('id').innerHTML
          s.created_at = (xml).at('created_at').innerHTML
          #s.text       = (xml).at('text').innerHTML
          s.user       = User.new_from_xml(xml) if (xml).at('user')
        end
     end

I think you should be able to use the Twitter gem with the new direct messaging Twitter API to develop automated SMS response agents as described in TechCrunch's article "Web Services coming to twitter".

This would allow you to implement your own Twitter SMS agents (similar to the Google SMS API). I wonder will Google open up their SMS API on the backend?

Wire Level Debug for ActiveResource

Posted by dave
on February 13, 2007 @ 08:08 AM

ActiveResource provides the mechanism to implement RESTful Ruby clients in Rails 1.2. If you want to see wire level debug output from the Net::HTTP layer you can enable it by editing:


[activeresource]/lib/activeresource/connection.rb

Add the set_debug_output line to the (private) http method.

1
2
3
4
5
6
7
8
9
10
11
12

     def http
        unless @http
          @http             = Net::HTTP.new(@site.host, @site.port)
          @http.use_ssl     = @site.is_a?(URI::HTTPS)
          @http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @http.use_ssl
          # enable debug http wirelevel dumps
          @http.set_debug_output $stderr
        end

        @http
      end
Very handy for debugging ActiveResource with a Rails console.

Invoking backend SOAP/HTTP Web Services with Ruby

Posted by dave
on October 06, 2006 @ 10:54 AM
So ya wanna invoke on a backend SOAP/HTTP Web Service from Rails do ye? SOAP4R (SOAP 4 Ruby) gets the job done as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'soap/wsdlDriver'
URL = 'http://localhost/hello_world.wsdl'

factory = SOAP::WSDLDriverFactory.new(URL)
driver = factory.create_rpc_driver
driver.wiredump_dev =  STDERR

begin
 
 // Invoke on the webservice here
 result = driver.sayHi
 result = driver.greetMe("dave")
 puts result

rescue Exception => e

  #Handle SOAP Faults here

end
This code snippet uses SOAP4R 1.5.5. This is a somewhat simplified example as theres only two methods defined in the wsdl, sayHi takes 0 parameters and greetMe just takes a string. Needless to say, this can be placed inside your Ruby on Rails controller too. If you have complextypes it looks as though you need to use wsdl2ruby to generate helper classes for the complextypes. I might be doing something wrong, but this version of SOAP4R looks to have some problems handling one particular rpc/literal wsdl I have here, but a bit more digging is required.