Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Ruby DBF Library Fixes

A handful of fixes/enhancements for the "dbf" Rubygem.

# example.rb
require 'rubygems'
require 'dbf'
require 'dbf_fixes'

table = DBF::Table.new('/path/to/table.dbf', :in_memory => false)
table.each_record do |record|
  # do something here
  # with in_memory=>false tables, each_record is much more efficient
  # because it reads directly from disk 
end



# dbf_fixes.rb
module DBF
  class Record
    private

    # fix bug in DBF code (or workaround bug in FoxPro dbf files; I don't know :-)
    def initialize_values(columns)
      columns.each do |column|
        case column.type
        when 'I' # added by Tim - I don't understand this much, but it seems to work
          @attributes[column.name] = @data.read(column.length).unpack("I").first
        when 'N' # number
          @attributes[column.name] = column.decimal.zero? ? unpack_string(column).to_i : unpack_string(column).to_f
        when 'D' # date
          raw = unpack_string(column).strip
          unless raw.empty?
            begin
              parts = raw.match(DATE_REGEXP).to_a.slice(1,3).map {|n| n.to_i}
              @attributes[column.name] = Time.gm(*parts)
            rescue
              parts = raw.match(DATE_REGEXP).to_a.slice(1,3).map {|n| n.to_i}
              @attributes[column.name] = Date.new(*parts)
            end
          end
        when 'M' # memo
          starting_block = unpack_string(column).to_i
          @attributes[column.name] = read_memo(starting_block)
        when 'L' # logical
          @attributes[column.name] = unpack_string(column) =~ /^(y|t)$/i ? true : false
        else
          @attributes[column.name] = unpack_string(column).strip
        end
      end
    end

    # don't know why, but accessors stopped working for me.
    def define_accessors
      @table.columns.each do |column|
        underscored_column_name = underscore(column.name)
        if @table.options[:accessors]
          self.class.send :define_method, underscored_column_name do
            @attributes[column.name]
          end
          @@accessors_defined = true
        end
      end
    end
  end

  class Table
    # more efficient iterator (so we don't load everything)
    def each_record
      if options[:in_memory] and @records
        @records.each { |r| yield(r) }
      else
        0.upto(@record_count - 1) do |n|
          seek_to_record(n)
          yield(DBF::Record.new(self)) unless deleted_record?
        end
      end
    end
  end
end

gemdocs

Put this snippet into your ~/.bash_login (or alternatives) to browse your gem docs from the command line.


function gemdocs() { 

  if [[ -n "$(whereis gem_server)" ]]; then
     gem_server >/dev/null 2>&1 &
     sleep 3
     open http://127.0.0.1:8808/
  fi

}

Twittering around in Ruby

This code uses the Twitter4R v0.2.0 which is a complete Ruby library that provides access to all documented Twitter REST APIs (and some experimental features).

Read more at:
http://twitter4r.rubyforge.org
http://snakesgemscoffee.blogspot.com/2007/07/twitter4r-020-release.html

You will first need to install the Twitter4R Ruby Gem: <tt>sudo gem install twitter4r</tt>.
require('rubygems')
gem('twitter4r', '>=0.2.0')
require('twitter')

login = 'mylogin' # change this
password = 'mypass' # change this
friend = 'myfriend' # change this

client = Twitter::Client.new(:login => login, :password => password)
public_timeline = client.timeline_for(:public) do |status|
  # do something here with each individual status in timeline that is also returned
  puts status.text
end

# can also pass a block like above to process each individual status object returned in timeline
friends_timeline = client.timeline_for(:friends) 

# same as above with block if you want
friend_timeline = client.timeline_for(:friend, friend)

# Retrieve the user object (with all public profile information) for my friend
user = Twitter::User.find(friend)

# If I don't want to be friends any more I "defriend" the user...
user.defriend

# Now I realize that was a terrible mistake and "befriend" them...
user.befriend

# At this point I want to send them a private message to let them know I made a mistake
# You can do this in two ways.
message = Twitter::Message.create(:client => client, :user => user, 
  :text => "I didn't really mean to defriend you.  Sorry!")
# OR...
message = client.message(:post, "I didn't really mean to defriend you.  Sorry!", user)

# Now I want to post a new status to my own timeline.
# This can be done one of two ways...
status = Twitter::Status.create(:client => client, :text => 'Sleeping off the beer from last night')
# OR...
status = client.status(:post, 'Sleeping off the beer from last night')

# Now I realize my mother is on twitter and wouldn't like to see me talking about beer,
# so assuming she doesn't have IM or SMS alerts we can delete the evidence....
client.status(:delete, status)

rubygems update

sudo gem updatesystem

Mofo - Parse Microformats with Ruby

$ sudo gem install mofo 
Successfully installed mofo-0.2.1
$ irb -rubygems
>> require 'mofo'
=> true
>> HResume.find("http://resume.jnewland.com").tags.uniq.sort
=> ["AJAX", "Apache", "CSS", "Capistrano", "DNS", "GNU/Linux", "HTML", "Javascript", "LAMP", "Mongrel", "Movable Type", "MySQL", "PHP", "Perl", "REST", "RSS", "Ruby", "Ruby on Rails", "SEO", "XHTML", "XML", "XSLT", "high availability", "lighttpd", "load-balanced"]

Install fcgi gem after installing FastCGI with darwinports

sudo gem install fcgi -- --with-fcgi-dir=/opt/local
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS