Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Sunday, July 31, 2011

Reuse gettext gem to parse .po file

I am working on a translation service which requires translating .po file (ruby gettext gem). Spent hours to understand the rake tasks it provides and looked for methods to extract all String inside .po file. Finally found the parser and the method to do my job:


require 'gettext/tools/poparser'
require 'gettext/tools/rmsgmerge'

parser = GetText::PoParser.new
result = parser.parse_file("...../my_rails_project/po/en/text_domain.po", GetText::RMsgMerge::PoData.new, false)

the object representing the data structure is assigned to result.


In case you are also extending gettext library, check the gettext/tools directory. Most of the useful methods are located there.

Monday, May 9, 2011

use adjacent sibling selectors for Web Scraping

I was using one CSS selector which is less commonly used in my last scraping task. It is called Adjacent Sibling Selector.

The information I needed was hotel links inside the page hotel.hk.

There was no id or class specified for that link and there are tons of link inside the same row, and I did not want to scrape the whole page and then take out that single html. The only specify information I know is that the link is always the next tag element immediately after the span having CSS class "title".

The usage of the selector is like this:

span.title + a

My program is something like this to extract the url in the href attribute:

detail_page_urls = []

page.search("span.title + a") do |title_link|
  detail_page_urls += title_link.attribute("href").value
end

I am using Ruby and Mechanize gem.

Tuesday, April 26, 2011

Another idea prototyping for comic fans: Comics Crowd

I created an application to share fav comic book list: http://comics-crowd.heroku.com/

it is great if you can recommend good comic books to me (and to other people)

need your feedback to improve the website too ~

^___^

Cheers

Written in Ruby On Rails

Open Source at github: http://github.com/3dd13/comics-crowd

Deployed to heroku

Saturday, April 23, 2011

Red Dot Ruby Conf 2011

Few things I think of after RedDotRubyConf day 1 event:

  • Rubyist =~ Agilist
  • I wish that people having 3G tethering, could form a Wifi access point cluster and share to attendee
  • Singapore's food and drinks are more expensive than 4 years ago
  • Today's talk keep remind me not to be lazy developer
  • Looking forward to RiteVM, to write mobile application in ruby
  • Pivotal Labs has plenty of talented developers

Monday, March 14, 2011

uninitialized constant ActiveRecord::TestFixtures

I saw this uninitialized constant ActiveRecord::TestFixtures error when i run the rake test. I have checked all the environment configuration. I am sure i am using Mongoid without ActiveRecord.

after reading the fixture_support.rb source code, I found out that the error is thrown only if ActiveRecord was called somewhere in my code. and it turned out to be caused by the Transition suggested by krzysiek: http://dev.netizer.pl/transitions-state-machine-for-rails-3.html

So, I have to switch the state machine to another gem which is not using ActiveRecord stuff. that's why i found stateflow: https://github.com/ryanza/stateflow

My test can run happily ever after.

Monday, February 28, 2011

Show seconds in datetime_select

By default the datetime_select only shows date, hour and minutes. But I do want to show the seconds for user to change. the default way of calling is like this:
<%= f.datetime_select :start_time %>
After I keep bundle open several gems, I found the option in active_pack, date_helper.rb:
@options[:discard_second] ||= true unless @options[:include_seconds] && !@options[:discard_minute]
OK, so :include_seconds can control this behavior, easy job then:
<%= f.datetime_select :start_time, :include_seconds => true %>

Sunday, February 27, 2011

String interpolation in en.yml

suppose we need to create a copyright statement which shows current year dynamically.

in the language text file (en.yml), we expect a variable current_year:

in the layout view, we provide this current_year as hash:

Saturday, February 26, 2011

Using Jekyll, Hyde and Heroku for presentation slides

I was sharing architecture of a RU05 program and performance tuning we did on with Mongo.
Codeaholics Feb Meetup


The slides i showed was actually a web page moving like a powerpoint slides.
The source code to generate the slides is available in github.


here I draft out high level steps giving you some rough ideas of the work required to create such webpage. you can find all the detail steps in jekyll, jekyll_and_hyde and rack-jekyll github page.


install required gems and create project:

gem install jekyll
gem install jekyll_and_hyde
gem install rack-jekyll
jh new codeaholics_hk_feb_2011
cd codeaholics_hk_feb_2011

To prepare deployment for the future, generate the static pages into another folder instead of default _site directory. i used "pages" folder in my case:

in the .gems file, include rack-jekyll
follow the github pages to setup _config.yml and config.ru

You can start creating your powerpoint, and read it at localhost:4000

jekyll --server pages
jh generate frontpage
... // generate your static pages
... // keep your jekyll server running until you finish. i explain why below

When you want to deploy to heroku:

git init
git add .
git commit -m ""
heroku create codeaholics_hk_feb_2011
git push heroku master

One issue i saw when generating post was that. I edited my posts markdown files after I stopped the jekyll server, and later when i started it again, no more static pages generated automatically. I guess the service could no longer recognizes which files was edited. I could not resolve this problem. So, i have to create another project and copy over my markdown files to continue.

Tuesday, February 22, 2011

Hong Kong Ruby Developer Job Market demands greater than supply

Recently, many teams in Hong Kong are recruiting Ruby On Rails developers. However, due to the nature of human laziness, not much people are willing to spare their own time learning Ruby On Rails.


Even though companies are willing to train up new Rubyist, people still do not think being a fulltime Ruby programmer is a good career. All the big players in Hong Kong are using Java, .Net and iOS, it makes career development path for starting as a Ruby developer harder.


The conventional development path for IT worker is promoting to project management instead of development. Salary and job promotion will stop soon if you stay as a coder even though how great program you can write. That's how pathetic it is being a normal developer in Hong Kong. Being a Ruby developer is considered risky and has no future.


here are some of the local Hong Kong companies recruiting RoR developers:
http://thought-sauce.com/jobs.html
http://hk.jobsdb.com/HK/en/Search/JobAdSingleDetail?jobsIdList=100003000539835
http://groups.google.com/group/hkror/browse_thread/thread/b463024bfed4da2f?pli=1
http://groups.google.com/group/hkror/browse_thread/thread/e18d3d3e5338f76f
http://hk.jobsdb.com/HK/EN/Search/JobAdSingleDetail?jobsIdList=100003000529158&sr=1


Regularly, Rubyist are coming from all over the world. Very interesting though. They just give a shout at google group, talk to several people, then they will get the job, fly over and then work here. The supply here internally in Hong Kong is really far far under the demand.


Very often, they compare the advantages of working in Hong Kong with Singapore. Singapore's ruby community and business usage looks much better than that in Hong Kong. Hong Kong is totally in a bad place booting up the culture of applying the edge tools instead of using the popular tools. But that's would be another long story about geographical competitiveness.

Friday, February 18, 2011

Overflow / page break examples in Prawn PDF generation

I have been using Prawn for a while. Here I gather several overflow / page break examples.


1. Text paragraph overflow

Having several lines of text and I want whole block of text stick together


Solution is using group to hold them together



2. Table rows overflow

Sometime there is no enough space for the last table row when it reaches the end of page, and Prawn does not move it to next page. That's why we have to manually add a page break:



More code snippets available at https://gist.github.com/3dd13

Thursday, January 27, 2011

Adding log purging job using Chef

Previous post talked about the linux command to purge old log files. Here is the way of doing the same thing, but with chef


cookbooks/housekeep/recipes/default.rb



# we need cron for scheduler
package cron do
  action :install
end

include_recipe "housekeep::clear_log"

cookbooks/housekeep/recipes/clear_log.rb



cron "housekeep application log files" do
  user "housekeeper"
  # mailto "your@example.com"
  
  # 00:01 every night
  hour "0"
  minute "1"

  # clean up logs older than 30 days
  command "/find ~/log/application-*.log.tar.gz -mtime +30 -exec rm {} \;"
end

Snippet available in gist:https://gist.github.com/798157

Saturday, January 22, 2011

Finding Intersection of streets using Google Geocoding API

after releasing the Green Hong Kong to public and open source it,
my next step is to improve the accuracy of Recycle Bins location.

my last release was just blindly submitting the whole address (not an exact address) string to Google Map API and asked for possible locations.
First, i submit the chinese address.
if no response for chinese address, then i submit again with english address.

Using this way i got almost 99% latitude and longitude back.
BUT, most of them are not accurate.


There are variety of recycle bin addresses Government website provided.
e.g. near the MTR exit, outside XXX Building, opposite to the Bank, near the XXX Lamp Pole, etc.
basically the text is clear ... to me, but not the computer.
so i need to enhance the intelligent in determine nearest latitude and longitude of the recycle bins.


First case i want to tackle is the intersection case.
Government website told me that some recycle bins are located at the intersection of 2 streets.
my first thought is using simple maths to draw 2 straight lines and calculate the intersection.
i look at the Google Geocoding API,  see if there is any concrete data about the street.
then i find out that there is a Address Component Type called "intersection".
which is actually what i needed.

alright, I am getting lazy, because Google did everything for me already.

Thursday, January 20, 2011

First time on Heroku

saw my colleagues using that for a while.
this is the first time i start using it from scratch.
http://greenhk.heroku.com


Besides the performance gain by this cloud hosting thing, the most surprising thing is the powerful toolset it comes with.

1)
Deployment is just a line of command

git push heroku master

then it will push your code to the git reportsitory, handle all the migrations and restart the rails application.

2)
It's like connecting to an application running on your local machine

Troubleshoot on the remote production server and tail the log is just a line of

heroku logs --tail

then it will send back the next block of log incremental changes.



and ... it is free !!
to me, i don't need big database thingy, so i can apply the free plan and just enjoy the performance and simplicity it provides !!