Thursday 29 October 2015

IRB.start equivalent in Python

Well, If anytime you wish to use the IRB.start like feature of Ruby in Python.

They way to do it is.

import code
import readline


vars = globals() ## important to carry over the methods and variable define in program to shell
shell = code.InteractiveConsole(vars)
shell.interact()


Thursday 2 July 2015

Some Ruby Thing

The following post consist of some features in ruby that are lesser known to people.

1. alias for '.call'

class Ticket
  def purchase
    puts "** Purchasing the Ticket **"
  end 
  alias_method :call,:purchase
end

## Following are they way you can invoke the download method
Ticket.new.purchase
## => ** Purchasing the Ticket **
A.new.call
## => ** Purchasing the Ticket **
A.new.()
## => ** Purchasing the Ticket **

2.  Disabling (SSL) Certificate Check from all Gems or Ruby Libraries.

unless Rails.env.production?
  class Net::HTTP
    def verify_mode=(verify_mode)
      @verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
end  

Note:

  • Do this only on your local or development environment.
  • Also the above code only work if the libraries or gem uses 'Net::HTTP' API internally, 
  • Tested with HTTParty,Faraday,RestClient not working with Excon(so Excon adapter over Faraday might not work as well).




3. Locking a file using flock.
    Firstly I advise you to read through flock and also this amazing answer by Arjun Shankar.
 
flock (locking_constant )→ 0 or falseclick to toggle source
Locks or unlocks a file according to locking_constant (a logical or of the values in the table below). Returns false if File::LOCK_NB is specified and the operation would otherwise have blocked. Not available on all platforms.
Locking constants (in class File):
LOCK_EX   | Exclusive lock. Only one process may hold an
          | exclusive lock for a given file at a time.
----------+------------------------------------------------
LOCK_NB   | Don't block when locking. May be combined
          | with other lock options using logical or.
----------+------------------------------------------------
LOCK_SH   | Shared lock. Multiple processes may each hold a
          | shared lock for a given file at the same time.
----------+------------------------------------------------
LOCK_UN   | Unlock.
 

Example

open(file,'w+') do |file|
  ## lock the file so that no one can use it. 
  file.flock(File::LOCK_EX)
  read(file_name).each do |row|
    file.puts jsonify(row)
  end
end 


Thanks.

Tuesday 21 April 2015

Use .wgetrc for default wget settings.

I use wget quite a lot and in the end I always end up using a large number of common options like

-c (to continue/resume a wget process)
--no-check-certificate (ignore ssl certificate check over https)

which eventually make it look very ugly but thanks to .wgetrc I no longer has to do it.

Create a .wgetrc file onto your home directory

JFI, Here how my current .wgetrc look.

continue=on     ## equivalent to -c options
check_certificate=off  ## equivalent to --no-check-certificate

Noting it down here so I just don't forget it future.

Thanks








Friday 10 April 2015

Using HTTPProxy in Net/HTTP globally.

There comes a time when you want to Proxy your Net/HTTP request/response in Ruby.

One of the easy ways to do this in NET/HTTP is by passing proxy parameters in '.new'  method (during initialization)

proxy_addr = 'your.proxy.host'
proxy_port = 8080

Net::HTTP.new('example.com', nil, proxy_addr, proxy_port).start { |http|
  # always proxy via your.proxy.addr:8080
}

Obviously, this works but then querying the website using GET or GET BY URI method there is no provision to pass any proxy parameters altogether(at least that's what I found out.)

Thankfully Ruby-2.0+ (not available in lower versions though, can be seen over here) has a feature with which you can provide proxy as your ENV variable (http_proxy environment variable) and ruby take care of the rest.

So, upon setting  http_proxy in ENV I found, BAAM!!!  it worked and it worked globally (i.e it works even on GET and GET BY URL methods mentioned above.)

BTW, I had the following declaration inside my .bash_profile.


export http_proxy= 'http://127.0.0.1:8080'  (8080 is the port where my Squid Proxy is functional)


Thanks




Monday 16 March 2015

Hack With Rack

Hello everyone, Hope you are having a good day.
Ok, some years ago, one of our clients wanted us to implement Social Network (FB,Twitter,Linkedin) sharing functionality. Knowing all the OAuth implementation that one has to deal with, we decided to use this awesome gem called OmniAuth that does all the heavy lifting tasks of OAuth Authorisation with very basic minimum implementation.
So with OmniAuth we manage to get the work done in time but then we wanted OAuth Authorisation to work with Ajax call as well. I remember going all out crazy (Googling + debugging OmniAuth code) to find a way to do it but honestly we couldn’t find any way do it. (Note: This was some years back I’m not sure about the status as of now.)
So we decide to write our own “Rack Middleware” that tampers with the Omniauth Response and treats the ajax request the way we wanted.
The Middleware

and then we insert the Middleware where we wanted it

Let look at our middleware stack

Yup that's it- a "Hack with Rack" to get a concrete solution for our problem that isn't too fancy but still does the task we needed getting done.

What did I learn today?

Welcome to the what did I learn today series. The intention of this blog spot is to compose the stuff that I learnt day-to-day basics and jo...