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

Tuesday, July 17, 2012

Scrape email from yellow pages and send them email with 300 second delay


em = []
(1..63).each do |i|
yp = Nokogiri::HTML(open("http://www.yellowpages.com.my/search.jsp?sfor=all&name=logistic&w=&p=#{i}"))
emails = yp.search("a.email").map{|x| x["onclick"].gsub("SqueezeBox.open('/plainmail.jsp?id=", "").split("',")[0] }
emails.each do |eid|
  t = Nokogiri::HTML(open("http://yellowpages.com.my/plainmail.jsp?id=#{eid}"))
  em.push(t.search("input").first["value"])
end
InvoiceMailer.inquiry(em).deliver
sleep 300
end

Saturday, July 7, 2012

ruby on rails daemon


akob:ceramahonline akob$ rails plugin install git://github.com/dougal/daemon_generator.git
Initialized empty Git repository in /Users/akob/hak/ceramahonline/vendor/plugins/daemon_generator/.git/
remote: Counting objects: 25, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 25 (delta 2), reused 20 (delta 1)
Unpacking objects: 100% (25/25), done.
From git://github.com/dougal/daemon_generator
 * branch            HEAD       -> FETCH_HEAD
Daemon Generator
================

To get yourself rolling:
> sudo gem install daemons
> ./script/generate daemon

Then insert your code in the lib/daemons/.rb stub. All pid's and logs will live in the normal log/ folder.  This helps to make things Capistrano friendly.

Individual control script:
> ./lib/daemons/_ctl [start|stop|restart]

App-wide control script (I add this to my capistrano recipe's after_restart task):
> ./script/daemons [start|stop|restart]
akob:ceramahonline akob$

Thursday, February 23, 2012

Ruby: concept of paginating

 paginating an array

Proof of Concept:

Preparing dummy array :
test = []
1.9.2p290 :053 > (1..50).each do |g|
1.9.2p290 :054 > test += [g]
1.9.2p290 :055?> end
=> 1..50
1.9.2p290 :056 > test
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

We know that test[1..5] will return [1, 2, 3, 4, 5] of elements.

Function to get the item number by 5 item per page.

1.9.2p290 :061 > def item_number page
1.9.2p290 :062?> perpage = 5
1.9.2p290 :063?> margin = perpage * (page - 1)
1.9.2p290 :064?> start = margin + 1
1.9.2p290 :065?> ending = page * perpage
1.9.2p290 :066?> return start..ending
1.9.2p290 :067?> end

Using the function to get page number 4

1.9.2p290 :069 > test[item_number 4]
=> [17, 18, 19, 20, 21]

happy arraying!