Ahmed's profileAhmed Live !PhotosBlogListsMore ![]() | Help |
|
Me in other places !
|
Ahmed Live !I am what I am, I do what I want... March 11 Analyze this !While working on a project, I needed to to have a feature like facebook’s “Post a link”. The user enters a URL, clicks on preview and he gets a preview of how that page will be posted on his profile. Facebook does a few tricks
A user then posts that URL. That’s when things start to get even more interesting
I needed to have the same behavior. First, let’s handle the generic case of any webpage. Given any URL, I had to get that pages HTML content and figure out the following
Now to the special pages. We’ll have to keep a database of all sites that we consider special and that we are able to treat differently. For example, youtube, so that if we know it’s a youtube’s video url, we can build the proper embed object.
I felt it was too much for a simple application to handle but I really wanted to get that feature. Before implementing anything, I tried to search and even asked at stackoverflow. Although I didn’t find the answer I wanted, the answers were really helpful. It was then that I learned about oEmbed “oEmbed is a format for allowing an embedded representation of a URL on third party sites. The simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly.” - http://oembed.com/ Unfortunately, it was too good to be true. The list of websites that support oEmbed is very limited. oohEmbed is a project trying to address that issue by providing oEmbed to a greater number of websites. oohEmbed proxies calls to oEmbed providers if possible and provides its own implementation when needed. The thing I like the most about oohEmbed is that it provides a single url through which you access oEmbed for various websites in contrast with the original oEmbed protocol where each website provided its own url for accessing oEmbed. For example to get the representation for a flickr URL (http://www.flickr.com/photos/ianpollock/6707007)
JSONP It’s simply a trick so as to be able to grab JSON data bypassing the cross-domain issues. The idea is, many APIs provide JSON responses.
Flickr supports this technique. Without providing a callback, the call should return the following response <script type=”text/javascript” src=”http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=a38b5237570253cc91d6b54ed9cf1535&place_id=Pu5_HsObApilq4vUtQ&tags=funny&format=json&nojsoncallback=1”></script> {..} Now with the callback <script type=”text/javascript” src=”http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=a38b5237570253cc91d6b54ed9cf1535&place_id=Pu5_HsObApilq4vUtQ&tags=funny&format=json&jsoncallback=showPhotos”></script> showPhotos({..})Solving the problem As I’m using Rails for the application I’m writing and without considering cross-domain javascript calls, the typical scenario would have been as follows
The problem with that solution is that the HTTP requests made by the server are blocking and if too many requests of that type occur, the application servers might get caught in these requests and thus will not be able to service normal requests.
Analyze This! Learning about JSONP – as a clean solution to cross domain communication, I decided it’s best to separate the action of requesting a page and parsing its HTML into a separate application. This way the original application’s performance will not get affected. The new application is called Analyze This. In its current form it’s a simple Sinatra application that has a single action. The kind of requests – blocking and take some time - that Analyze This handles made it a perfect candidate to use an upcoming version of NeverBlock that not only handles DB but sockets and networking as well. For example, an HTML page requesting information about a user entered URL use a code similar to the following
That would result in a call to the javascript function analyze_this({..}) passing in a JSON object that has the page’s info. To see it live, just download Analyze This, run the application and visit the sample page. The Analyze This application itself is no big deal but the point is architecture really matters. October 29 Get a Google Map for WAP !Recently, I’ve been involved in a project where I was creating a WAP site using Ruby On Rails that needed to display certain locations using Google Maps. Given the limited capabilities for the average WAP browsers (mainly the lack of JavaScript) I needed to figure out a way. To my good luck, Google had Google Static Maps API which is basically a URL that you put in the src attribute of an img tag. The URL accepts parameters that will define the image you get: The most important parameters are the Longitude/Latitude of the map’s center and the zoom level. The URL looks like the following http://maps.google.com/staticmap?key={google_maps_api_key}&size={width}x{height}¢er={latitude},{longitude}&zoom={zoom} Since Google Static Maps API is just an image provider, what if we wanted richer interaction (zooming, panning) with the map? We’ll have to implement those features on our own. Here’s the approach we took
Zooming Panning
I got the previous 3 steps form here. For example if we have the Latitude/Longitude and Zoom and width/height of the image in pixels: # To pan north or south we modify latitude similarly In the Google Maps API there’s a converter that allows converting between Latitude/Longitude and Pixels (X,Y) at different zoom levels. Hopefully these were enough info to get you started on using Zooming/Panning on Google Static Maps API. July 24 HTTP Basic Authentication for functional tests !While I was trying to cover a controller with some tests I faced a problem. The controller actions where protected by a filter that prompted the users for login via basic http authentication. I found a solution in rails code here http://github.com/rails/rails/tree/master/actionpack/lib/action_controller/http_authentication.rb where it said you should do your get as follows
This didn't work for me where basic http authentication required sending the encoded credentials in the request headers while the previous get request sent the authorization credentials in the session.
I found the following code snippet in http://snippets.dzone.com/posts/show/3785 which allowed me to set request headers
In my tests, I now write the following
And it works like a charm ! July 20 reCAPTCHA your rails application ! CAPTCHA stands for "Completely Automated Public Turing test to tell Computers and Humans Apart". As the acronym suggests, the main reason of using CAPTCHA is to tell computers and human apart. It is a challenge-response test used to ensure that the response is not machine generated. CAPTCHA comes in many forms, some are more popular than the others
reCAPTCHA is one of the CAPTCHA efforts. It also tries to solve another problem in addition to fighting spam. It tries to improve the process of digitizing books by sending words that cannot be read by computers to the Web in the form of CAPTCHAs for humans to decipher. The question that popped immediately in my mind was how does that reCAPTCHA verify the answers if it's using images of words that the computer couldn't really figure out what they were while scanning them. The answer is simple: it display two words at a time, one word can be easily verified and for the other word, your solution is taken to be a suggestion for that word. That word is used many times in different CAPTCHAs and eventually many people will suggest the same thing. Currently, reCAPTCHA is recommended as the official CAPTCHA implementation by the original CAPTCHA creators. This way reCAPTCHA not only helps you to fight spam but also gets you to participate into a good cause like digitizing the world's books. Using reCAPTCHA in your Rails application is so easy thanks to the recaptcha plugin. This plugin gives you 2 main methods that you can use in your application
recaptcha_tags accepts an options hash which can define the public key with :public_key so that it doesn't look in your environment variables. verify_recaptcha - which uses the private key - doesn't provide a way for you to pass the private_key. I've forked the plugin here and modified verify_recaptcha such that it now accepts an options hash - like recaptcha_tags - which allows you to define :private_key which will be used instead of looking into the environment variables. Fight spam, help in digitizing books, use reCAPTCHA ! update: I sent a pull request to the guys over at http://github.com/ambethia/recaptcha to include my changes. Peter Abrahamsen replied and after a couple of messages we modified the plugin such that we no longer need to set the public and private key in any environment variables. We also added a toggle to enable/disable the plugin. We can use the plugin as follows now Ambethia::ReCaptcha.enabled = trueIf the toggle is set to false the recaptcha_tags will return nothing and the verify_recaptcha will always return true meaning that the recaptcha code does nothing which is what we want in case of disabling it. July 07 version_cache: 1, 2, 3, It's all in the numbers !Building on my previous post about caching where the method_cache plugin was introduced, today I'd like to introduce another Rails plugin that also deals with the caching problem. This time, it's about caching views. It uses a technique called version caching. Version caching frees you from having to worry about writing code that expires your cache. For more info about version caching check Yasser Wahba's blog post explaining version caching and how we used it. I've used version caching in my last 2 projects and found it quite useful and quite easy. The problem is that we used to repeat a lot of code violating DRY. It was so clear we had to do something about it. I've taken that code, refactored it and made it available as a Rails (v2.1.0) plugin available at GitHub. Check it here.
The first 2 are the most important. time_cache as the name implies allows for time caching for a page. it can be used as follows To get the plugin ruby script/plugin install git://github.com/humanzz/version_cache.git Then use the plugin’s generator to generate the cache observer ruby script/generate version_cache_observer Cache Model1 Model2 The first argument “Cache” is taken to be the observer’s name. Any arguments after that are taken to be the models that the observer will observe.
Technorati Tags: ruby - rails - caching - version_cache - rails plugins April 18 method_cache: Let's cache some stuff !Building a large-scale web application can be troublesome. It leaves us facing all sorts of optimization problems. A very good – but no easy – solution is to use caching. Caching is a real saver as it can help with minimizing
which are the two main problems that can affect the performance of a web application. A common problem that developers always face is having to call a method regularly knowing that its results rarely change and its cost is always high. More on that in the following code
Calling user.costy_method with all its costs and behavior appears to be a point that can be optimized. How about caching its result? The user.costy_method needs to be modified so that it caches its result. A common practice for Ruby developers is to cache the result in an instance variable the following way
What if there were more methods like this? They'll all have to be rewritten so as to cache their results. This would be a tedious task with a lot of repeated code which violates DRY concept. Why not have a simpler solution for this particular problem? We wrote the method_cache plugin. Let's see how it works
and voila, every time user.costy_method is called, you'll get the value you expect normally but this time at a much less cost. Another convenience method is expire_method :method_name which can be used like user.expire_method :costy_method. This method will simply clear the instance variable and cache store entry if they're available so that next time user.costy_method is called, a new value will be calculated. Mido - Eng. Muhammed Ali - wrote this plugin. I pretty much loved the simplicity by which this will allow us to cache stuff. More on the plugin features After using the plugin internally for a while we decided it's best if others can make use of it and also contribute. We've setup a project on RubyForge which can be found here. In its current form, the plugin provides the following extensions to ActiveRecord Objects More on the 2 level cache mentioned before, for class methods, we cached the results in static variables but this was a deal breaker once we used several mongrels to serve our application as it's impossible to synchronize the expiry of the static variables on the different mongrels so we decided it was enough to use the fragment cache store as our only store when dealing with class methods. I think it would be nice to refactor the plugin so as to be able to use it with all classes not just ActiveRecord::Base descendants. This would require some minor changes such as defining a method that defines an instance uniquely. In case of ActiveRecord objects, it's just the id. Update To install it ruby script/plugin install git://github.com/humanzz/method_cache.git April 02 Have a cookie, Smile and Flash !Quite often, Ruby on Rails developers use the flash object to store objects for the following request. Flash's most common use is for displaying messages to users after redirections. Flashes are usually rendered in the application's layout. Consider the following 2 scenarios that will stop flashes from working properly
So, what we really need is a solution that doesn't rely on the rendering of the layout or of partial responses. Have a cookie and be a happy - hopefully not so fat - man :) So, we need to edit 2 files
March 19 Martian HeadsetsWeb standards and different browsers implementation have always been a nightmare for software developers. Joel Spolsky gives a nice explanation about the current situation. Check his blog entry "Martian Headsets". March 11 FolderShare is now Windows Live FolderShare Beta (This time for real)One of my favorite programs is FolderShare. For those who don't know, it is a small program that sits in your tray but gives you great features
Microsoft bought FolderShare in 2005 and it was supposed to be integrated into Windows Live services but for the past 2 years there were no updates at all to FolderShare. That didn't bother me, I just loved the service the way it was. Today, however, the foldershare website has been updated at last to reflect the Windows Live brand and a new client is released. It can be downloaded from here. Check WL FolderShare blog. There are no new features yet. However, I don't think that would last for much longer. FolderShare website and service appears to have been using PHP but the updated version, naturally, uses ASP.NET. A couple of features I'd love to see
I'm sure integration is WL ID is coming. It's the only logical thing to do next. Integration with messenger seems logical to me too but will they do it anytime soon ?! March 09 Silverlight 2 'Deep Zoom'Did you try Microsoft Photosynth?! Did you try Google/Windows Live/Yahoo maps?! Have you ever wanted to do something similar with a large collection of images you own ?! Meet Deep Zoom. For some example sites check the following You should have Silverlight 2 Beta 1 runtime in order to try it. You can give it a try and upload some pictures of yours and try it at Windows Live PhotoZoom. More on that from Liveside.net can be found here. For developers, you can create Silverlight applications that take advantage of the Deep Zoom technology so easily
I was thinking about posting my demo application where I used several photos for lovely Angelina Jolie but then I searched for an example and I found a whole lot of them available. So if you struggle or something just search for Deep Zoom and I'm sure you'll get what you want easily. Now you are ready to go. |
This user currently is not registered with Windows Live QnA account. Click here to learn more and get started. |
|||||||||||||
|
|