Thursday, April 28, 2011
"getaddrinfo: Temporary failure in name resolution"
This error is because the system can't resolve the DNS names.
Tuesday, April 12, 2011
NoMethodError: undefined method `pluralize'
include ActionView::Helpers::TagHelper
>> helper.pluralize(2,'person')
=> "2 people"
Setting User Agent in Typhoeus
Typhoeus::Request.new("http://www.example.com",:user_agent => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.10) Gecko/20100915 Ubuntu/10.04 (lucid) Firefox/3.6.10")
Making Parallel Requests using Typhoeus
Making Parallel Requests
# Generally, you should be running requests through hydra. Here is how that looks
hydra = Typhoeus::Hydra.new
first_request = Typhoeus::Request.new("http://localhost:3000/posts/1.json")
first_request.on_complete do |response|
post = JSON.parse(response.body)
third_request = Typhoeus::Request.new(post.links.first) # get the first url in the post
third_request.on_complete do |response|
# do something with that
end
hydra.queue third_request
return post
end
second_request = Typhoeus::Request.new("http://localhost:3000/users/1.json")
second_request.on_complete do |response|
JSON.parse(response.body)
end
hydra.queue first_request
hydra.queue second_request
hydra.run # this is a blocking call that returns once all requests are complete
first_request.handled_response # the value returned from the on_complete block
second_request.handled_response # the value returned from the on_complete block (parsed JSON)
The execution of that code goes something like this. The first and second requests are built and queued. When hydra is run the first and second requests run in parallel. When the first request completes, the third request is then built and queued up. The moment it is queued Hydra starts executing it. Meanwhile the second request would continue to run (or it could have completed before the first). Once the third request is done, hydra.run returns.
Typhoeus Examples
require File.dirname(__FILE__) + '/../lib/typhoeus.rb'hydra = Typhoeus::Hydra.newhydra.disable_memoizationurls = ['http://google.com','http://testphp.vulnweb.com','http://demo.testfire.net','http://example.net',]10.times {|i|req = Typhoeus::Request.new( urls[ i % urls.size] )req.on_complete {|res|puts 'URL: ' + res.effective_urlputs 'Time: ' + res.time.to_sputs 'Connect time: ' + res.connect_time.to_sputs 'App connect time: ' + res.app_connect_time.to_sputs 'Start transfer time: ' + res.start_transfer_time.to_sputs 'Pre transfer time: ' + res.pretransfer_time.to_sputs '-------------'}hydra.queue( req )puts 'Queued: ' + req.url}putsputs 'Harvesting responses...'putshydra.runputsputs 'Done.'puts
Super Simple Authentication
SuperSimpleAuthentication
===================
First, install the plugin:
script/plugin install http://svn.ariejan.net/plugins/super_simple_authentication
When the plugin is installed, you may generate your SSA controller. This controller verifies your password and makes sure you stay authenticated for the duration of your visit.
script/generate super_simple_authentication sessions
Your password is located in config/super_simple_authentication.yml. Change it.
In the SessionsController, you’ll find an include statement. Move this include to your application controller.
The generator automatically added routes to your config/routes.rb file. If you want easy access to login and logout functionality, add these two lines to your config/routes.rb file as well:
map.login '/login', :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy', :method => :delete
You can now protect you actions and controllers with a before_filter:
# Protect all actions in the controller
before_filter :authorization_required
# Protect all actions, except :index and :recent
before_filter :authorization_required, :except => [:index, :recent]
# Protect only :destroy
before_filter :authorization_required, :only => :destroy
In your views, you can check if you are authorized or not with authorized? E.g.
<% if authorized? %>
# ... do secret admin stuff
<% end %>
Wednesday, April 6, 2011
Setting proxy for Nokogiri
Nokogiri::HTML(open(url, :proxy => 'http://(ip_address):(port)'))
Also please read this to get proxy ip address http://www.torproject.org/
Logs all HTTP(S) traffic between your computer and the Internet
Fiddler is a Web Debugging Proxy which logs all
Fiddler is freeware and can debug traffic from virtually any application, including Internet Explorer, Mozilla Firefox, Opera, and thousands more.
http://www.fiddler2.com/fiddler2/
EOFError: end of file reached
Ruby EOFError: end of file reached
EOFError: end of file reached
from /usr/lib/ruby/1.8/net/protocol.rb:135:in `sysread'
from /usr/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill'
from /usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
from /usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
from /usr/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
from /usr/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
from /usr/lib/ruby/1.8/net/protocol.rb:126:in `readline'
from /usr/lib/ruby/1.8/net/http.rb:2020:in `read_status_line'
from /usr/lib/ruby/1.8/net/http.rb:2009:in `read_new'
The key point to resolve this kinda issue is to encode the url, before fire the connect or get.
@agent.get URI.encode(url)
OR
This is a timeout error you can handle it like below.
1 | require 'net/pop3' |
Encoding and Decoding entities in HTML and XHTML documents
HTMLEntities is a simple library to facilitate encoding and decoding of named (
ý
and so on) or numerical ({
orĪ
) entities in HTML and XHTML documents.The current release is version 4.1.0.
Changes since version 4.0.0
- Now works with Ruby 1.9.1 and JRuby 1.3.1.
- Reverted lazy loading of entity mappings as this is not thread-safe.
- Finally removed the deprecated
String#encode_entities
and#decode_entities
methods. - Added :expanded charset: about 1000 SGML entities.
Usage
If you are running the examples below in
irb
, please make sure that you are running in a UTF-8 terminal and have set $KCODE
to 'u'
to enable the display of UTF-8 characters.Decoding
require 'htmlentities'
coder = HTMLEntities.new
string = "élan"
coder.decode(string) # => "élan"
Encoding
This is slightly more complicated, due to the various options. The encode method takes a variable number of parameters, which tell it which instructions to carry out.
require 'htmlentities'
coder = HTMLEntities.new
string = "<élan>"
coder.encode(string) # => "<élan>"
coder.encode(string, :named) # => "<élan>"
coder.encode(string, :decimal) # => "<élan>"
coder.encode(string, :hexadecimal) # => "<élan>"
Documentation
For more details, you can browse the rdoc-generated documentation online.
Getting it
HTMLEntities is available as a tarball or gem on its RubyForge files page.
You should also be able to install it using RubyGems:
gem install htmlentities
The current source code can be found on GitHub asthreedaymonk/htmlentities.
remove & ruby
I have a string variable contains "&". When i inspect it "&" is get replaced with "\302\240". Son now i can remove it by string_variable.gsub(/\302\240/,"").
Timing Ruby Code – It Is Easy With Benchmark
beginning_time = Time.now
(1..10000).each { |i| i }
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
You get the current time before and the current time after the code you need to measure the running time of and you simply subtract them from each other (to get the time in seconds). The output is as follows:
justin@justin-ubuntu-vm:~/projects$ ruby time.rb
Time elapsed 4.049 milliseconds
But this seemed rather inflexible, and you wouldn't want to have to do it over and over again, so I rolled it into a method, to allow myself to time the execution of other methods, like so.
def time_method(method, *args)
beginning_time = Time.now
self.send(method, args)
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
end
def method_to_time(*args)
(1..10000).each { |i| i }
end
time_method(:method_to_time)
As you can see we can now pass a symbol version of the method name we want to time into ourtime_method. Inside the time_method we will call "send" on the current object passing in the method name (which will essentially call the method we want to time) and will wrap this call in our timing code, producing similar output (to that from above):
justin@justin-ubuntu-vm:~/projects$ ruby time.rb
Time elapsed 6.198 milliseconds
This is somewhat better, but now we can only time methods, not arbitrary code which is not very nice. So, we enhance our time_method by allowing it to take a block.
def time_method(method=nil, *args)
beginning_time = Time.now
if block_given?
yield
else
self.send(method, args)
end
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
end
time_method do
(1..10000).each { |i| i }
end
justin@justin-ubuntu-vm:~/projects$ ruby time.rb
Time elapsed 6.198 milliseconds
This allows us to time arbitrary code as well as methods – awesome!
However as I was browsing some code the other day (for unrelated reasons), I discovered that it wasn't so awesome, because I reinvented the wheel and the existing wheel is potentially better.
If enjoy my Ruby posts (like this one), here is some of the Ruby related stuff I am planning to write about in the near future.
- Passing methods as arguments in Ruby
- Ruby object serialization (and deserialization :))
- A look at serializing Ruby blocks/procs
- plus much more…
Ruby Benchmark Module
Ruby already has a module as part of its standard libraries that basically does all that timing stuff I was playing with above, it's the Benchmark module. For example:
require "benchmark"
time = Benchmark.measure do
(1..10000).each { |i| i }
end
puts time
As you can see we pass the block we want to time to Benchmark.measure which will return output that will look like this when printed:
justin@justin-ubuntu-vm:~/projects$ ruby time.rb
0.010000 0.000000 0.010000 ( 0.003298)
You're probably getting the picture already, but if not here is another way to use benchmark which will label our output to make it clearer:
require "benchmark"
Benchmark.bm(7) do |x|
x.report("first:") { (1..10000).each { |i| i } }
x.report("second:") { (1..10000).each { |i| i }}
x.report("third:") { (1..10000).each { |i| i }}
end
justin@justin-ubuntu-vm:~/projects$ ruby time.rb
user system total real
first: 0.000000 0.010000 0.010000 ( 0.008010)
second: 0.000000 0.000000 0.000000 ( 0.005251)
third: 0.000000 0.000000 0.000000 ( 0.003678)
Not only did we run and time our code, but we did it multiple times producing, the user, system, totaland real times for each run. You can imagine how useful this could be especially considering that if we use one of the other methods of the Benchmark module (Benchmark.benchmark) it will allow us to format our output as well as easily produce averages and totals for all the runs.
Of course if you don't want all that extraneous stuff printed, the easiest way to use it is:
require "benchmark"
time = Benchmark.realtime do
(1..10000).each { |i| i }
end
puts "Time elapsed #{time*1000} milliseconds"
justin@justin-ubuntu-vm:~/projects$ ruby time.rb
Time elapsed 5.35893440246582 milliseconds
Which simply gives you the real elapsed time in seconds which you can then print out yourself. In hindsight I am still pretty happy with my own time_method way as it is simple, flexible and mine :). But it is good to know that Ruby already provides a facility to easily time execution without having to write any extra code.
Subscribe to:
Posts (Atom)