Loving Android

The Android development environment is pretty darn slick…if you already haven’t or are stuck on the iPhone bandwagon, by all means take a look:
Android

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Blog Relevancy

Is everyone on Twitter now?

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Accessing FedEx Web Services From Ruby/Rails

I recently played around with adding FedEx shipping services to the active_shipping plugin and found a bit of confusion online in regards to how to hit FedEx depending upon the style of service being exposed. Somewhat confusingly, FedEx has non-SOAP services as well as SOAP services that you can hit restfully in an HTTPS Post. For the SOAP service, FedEx has very specific requirements about how to formulate the payload and HTTP headers that took a bit of tinkering to get working. Non-SOAP FedEx support was recently added to active_shipping prior to the completion of my work, but I figured I would post if for those who want to use what I think are newer SOAP services.

Without much further ado, here are the basics for creating a tracking request:


TEST_DOMAIN = 'gatewaybeta.fedex.com'

def find_tracking_info(tracking_number, options={})
        options = @options.update(options)
        tracking_request = build_tracking_request(tracking_number, options)
        response = commit(:track, save_request(tracking_request),
                          (options[:test] || false))
        parse_tracking_response(response, options)
  end

protected
   def build_access_request
        xml_request = XmlNode.new('ns:WebAuthenticationDetail') do |access|
          access << XmlNode.new('ns:UserCredential') do |user|
             user << XmlNode.new('ns:Key', @options[:key])
             user << XmlNode.new('ns:Password', @options[:password])
          end
        end
        xml_request
      end

      def build_client_detail
        xml_request = XmlNode.new('ns:ClientDetail') do |client_detail|
          client_detail << XmlNode.new('ns:AccountNumber', @options[:account])
          client_detail << XmlNode.new('ns:MeterNumber', @options[:meter])
        end
        xml_request
      end

     def build_tracking_request(tracking_number, options={})
        xml_request = XmlNode.new('ns:TrackRequest',
                         :'xmlns:ns'=>"http://fedex.com/ws/track/v2",
                         :'xmlns:xsi'=>"http://www.w3.org/2001/XMLSchema-instance",
                         :'xsi:schemaLocation'=>"http://fedex.com/ws/track/v2") do |root_node|
          root_node << build_access_request
          root_node << build_client_detail
          root_node << XmlNode.new('ns:TransactionDetail') do |detail|
            detail << XmlNode.new('ns:CustomerTransactionId', 'Ground Track')
          end
          root_node <<  XmlNode.new('ns:Version') do |version|
            version << XmlNode.new('ns:ServiceId', 'trck')
            version << XmlNode.new('ns:Major', '2')
            version << XmlNode.new('ns:Intermediate', '0')
            version << XmlNode.new('ns:Minor', '0')
          end
           root_node <<  XmlNode.new('ns:PackageIdentifier') do |package|
              package << XmlNode.new('ns:Value', tracking_number.to_s)
              package << XmlNode.new('ns:Type', "TRACKING_NUMBER_OR_DOORTAG")
          end
        end
        xml_request.to_xml
      end

     def commit(action, request, test = false)
        http = Net::HTTP.new((test ? TEST_DOMAIN : LIVE_DOMAIN),
                                (USE_SSL[action] ? 443 : 80 ))
        http.use_ssl = USE_SSL[action]

        headers = {
          'Referer' => 'me',
          'Port' => '443',
          'Accept' => "image/gif, image/jpeg, image/pjpeg, text/plain, text/html, */*",
          'Content-Type' => 'image/gif'
        }

        http.verify_mode = OpenSSL::SSL::VERIFY_NONE if USE_SSL[action]
        response = http.start do |http|
          http.request_post ('/xml', request, headers)
        end
        response.body
      end

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Lightweight Ruby Web Services

The recent Oreilly book Enterprise Rails has a few chapters on SOA and the entire time I was reading those chapters I kept thinking to myself that Rails is sorta overkill for most services implementations, unless you plan on bundling APIs and applications together. I was going to write something about translating the services discussed in that book to Merb micro apps or something similar, but a few recent posts beat me to the punch. Enjoy:

Web Services With Sinatra.

SOA with Merb?.

Throw in a little Happy Mapper and you sort of have the equivalent of Restlet/Jersey and Castor/Jibx in the Java world. Enterprise indeed.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Rails MailChimp Plugin Updates

While I’ve been tinkering with fleshing out my custom DataMapper adapter for MailChimp, I decided to start adding more functionality to my Rails/ActiveRecord plugin. I’ve upgraded to the most recent MailChimp APIs and am in the process of adding campaign management functionality. Drop me a line if you want to fork and patch what I have, as a few redundant plugins have started popping up and it seems like some consolidation would make sense.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Netflix API Authentication with the Ruby Oauth Gem

For any folks out there struggling to get the Ruby OAuth gem working with Netflix authentication, here’s a quick run down on how to get started.

The 0.2.7 version of Ruby OAuth isn’t totally compatible with the Netflix authentication APIs. However, thanks to the grooviness that it is GitHub, Rob Ares graciously forked the OAuth gem in order to get it working. So clone his repository and get cracking!

Here’s a start:


require "oauth/consumer"
consumer = OAuth::Consumer.new(
      "developer api key",
      "developer api secret",
      {
        :site => "http://api.netflix.com",
        :request_token_url => "https://api-user.netflix.com/oauth/request_token",
        :access_token_url => "http://api.netflix.com/oauth/access_token",
        :authorize_url => "https://api-user.netflix.com/oauth/login"
      })

    request_token = consumer.get_request_token  

 request_token.authorize_url({
      :o auth_consumer_key => "you developer api key",
      :application_name => "application name",
      :o auth_callback => "optional"
    })

    access_token = request_token.get_access_token

    response = consumer.request(
      :get,
      "/users/#{access_token.response[:user_id]}",
      access_token,
      {:scheme => :query_string})

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

DataMapper Adapter for Netflix

I’ve apparently caught the DM Adapter bug, as I’ve decided to write one to access Netflix while I finish up my MailChimp adapter.

I’ll be posting as I go along, as I’ve been learning quite a bit about the inner workings of DataMapper while going through this process.

Cheers.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Writing a DataMapper Adapter to Access MailChimp Web Services From Merb

As I mentioned in my last post, I’ve started looking into writing DataMapper Adapters, using my MailChimp wrapper as an example. Thus far, I’ve been pretty impressed with the Adapter layer built into DataMapper and the possibilities this abstraction layer provides. A quick search over at GitHub will show you all the interesting things folks are doing with adapters.

Anyway, here’s a synopsis of what I’ve come across thus far. As pointed out in this post by The Merbist, a custom adapter simply inherits from Abstract Adapter and as such, can define a number of default Adapter methods such as create, update, delete, read_one and read_many. You also have access to an initialization method that supplies a hash with important environment information, including any parameters relevant to your adapter that a user may have setup in a database.yml file. This allowed me to create a MailChimp client and login once during class initialization rather than at every request, so I got that going for me.

Once I had a MailChimp client setup with login information, I started getting create working in my adapter. The create method takes a collection of resources as an option which I simply loop through and call the MailChimp listSubscribe API. So far, so good. I now wanted to test whether any of this would work with Merb.

I created a merb app:


 merb-gen app campaign
 cd campaign

and set the :use_orm parameter to datamapper in the Merb init.rb.

Next, I ran rake dm:db:database_yaml in order to generate a database.yml file to specify relevant properties for my adapter. In my case, I needed to create an entry for my mailchimp adapter:


  adapter:  mailchimp
  database:
  username: user
  password: password
  mailing_list_id: list_id
  host: localhost

.

I next created a resource to test the adapter with by running merb-gen resource mailings. My thinking was that one could have a User stored in a relational (or other) datastore, and that User can have many mailings or some such thing, bad naming aside. Anyway, a cool feature of DataMapper is that you can specify any number of repositories in a database.yml with different adapters, and then specify on a resource by resource basis which repository a resource belongs to like so:


 def self.default_repository_name
      :mysql
 end

After generating my resource, I went in to add the properties MailChimp would require for the create method.


class Mailings
  include DataMapper::Resource

  property :id, String, :serial => true, :key => true, :field => :_id
  property :first_name, String
  property :last_name, String
  property :email, String
  property :mailing_list_id, String

   def build_mail_merge()
      {"EMAIL" => self.email, "FNAME" => self.first_name, "LNAME" => self.last_name }
   end
end

.

I also added a callback method that is used by the adapter in order to pass mail merge arguments to MailChimp. This seemed like a sane approach given that the MailChimp mail_merge arguments are quite dynamic.

With a resource firmly in place, I fired up the merb console and tested to see whether I could connect to MailChimp with it.


 m = Mailing.new
 m.first_name = 'mandarin'
 m.last_name = 'soda'
 m.email = 'msoda@disney.com'
 m.mailing_list_id = '9'
 m.create

I checked my disney email, and sure enough, I had an opt-in response from MailChimp. Victory Adapter! I could sign users up to MailChimp via the DataMapper APIs quite quickly….not too shabby.

That’s where I’m at for now…still have some things to iron out, one being that DataMapper is returning false after a successful create. Others, of course being that I need to figure out the rest of the default Adapter methods. Feel free to follow on GitHub if you feel so inclined.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

DataMapper Adapter for MailChimp

Inspired by this post by The Merbist, I’ve decided to begin writing a Datamapper Adapter for MailChimp as a follow up to my post about creating a Merb web service. Look here for updates. Just an empty shell at the moment, but I’ll remedy that shortly.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Zeep SMS Gem Configuration for Rails

For whatever reason, I can rarely remember the myriad syntax for managing gem dependencies in Rails. I’ve visited this page quite a bit during various projects.

When trying to install the zeep messaging gem recently, sure enough I struggled a bit with getting the syntax correct.

Anyway, without further ado, here is what worked:
config.gem "zeep-messaging", :lib => "zeep/messaging", :version => "0.1.5"

Kinda nice sending SMS messages from the console, I have to admit.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]