Typo, the Amazon sidebar, Text Filters and more
April 16th, 2006
The wedding last night was great fun! Thanks Sally!!! Now back to our previously scheduled rubynoob article.
This busy weekend my goal was to figure out why the Amazon toolbar wouldn't work for me. Here's what the sidebar looks like:
My problem, though, is that it doesn't quite work as expected. I tried linking Amazon books in several different ways with no success. I just couldn't figure out how to make it work. So, I figured that investigating the component would be a great way to learn a little bit more about Rails, and I was right!
Long story short - in order to get typo to create sidebar links to Amazon items, you need to choose a text filter in the blog settings page which uses the Amazon post-processing filter. None of the default text filters uses the Amazon post-processing filter, so you'll either need to create a new one or edit an existing one and make sure to include the it as well as inserting your Amazon associate's id. I couldn't find any documentation anywhere that spells this out, but the rest of this article explains how I figured this out.
The first thing I did was to try to locate the component. I found it down in C:\InstantRails\rails_apps\typo\components\plugins\sidebars. The file is amazon_controller.rb. The code of interest is:
It looks like we build an array of all of the asins from the contents of the web page and then set @asins to a list of up to the maximum number of links (defined in the config panel shown above) of asins. The page will then use this information to render the ads (which you can see at the bottom of my sidebar here). As described in my previous post about breakpoints, I had no idea what that first line of code did. So, I opened up the PDF of my handy-dandy Programming Ruby book and flipped to the Ruby Library Reference for arrays. I understood everything up to the inject method. What the heck is that? As the book says, it's mixed into class array from class enumerable. In my PDF, I found the pertinent section on page 463 or you can just open a command prompt on your Ruby environment and enter "ri Enumberable.inject" and get the same information. So, in this case "asin_list" is an array that we're filling up by iterating through params[:contents] items until we run into an item with whiteboard information. Then we load the whiteboard's asins into our asin_list. So, after looking at the documentation for a while it started to make some sort of sense.
If you're still stuck on figuring out this line of code, set a breakpoint like I explained in the previous post and then when you get to the irb prompt try a few things. First, just type in "params[:contents]" and hit enter. You'll get a big blob of text. Copy and paste this into notepad and study it carefully. You can search for whiteboard, and unless you set up the Amazon post-processing text filter you'll see that none of the whiteboards contain any information. However, plent of other items have information. One item that caught my eye was "title." So, let's play with our confusing line of code and see if we can get an array of blog entry titles. At the irb prompt in the paused breakpointer console window, type in:
This will return an array of all of your blog titles showing on the page of your blog that you're looking at. Something like ["title1", "My other title", "Another title"]. Pretty neat huh?
The reason why the Amazon sidebar wasn't working for me is because nothing was ever getting set into the whiteboard hash key "asins." There was nothing at all in the whiteboard hash. So, I figured there had to be some other mention of the whiteboard somewhere else in the typo code, so I opened the scite text editor and went to the Search > Find in Files... menu item and chose to search for the text "whiteboard" under every .rb file under the typo directory (including subfolders). Looking at the results, it looks like the only place where whiteboard[:asins] is being set in the entire typo app is in the components\plugins\textfilters\amazon_controller.rb file. This is the controller for the Amazon post-processing text filter. Using this filter allows you to enter links to amazon items with an href like this: href="amazon:097669400X" and turns that into something like this: href="http://www.amazon.com/exec/obidos/ASIN/097669400X/scottstuff-20."
So, this is when it finally dawned on me that the only way for the Amazon sidebar component to show links on the side bar is to have the asins show up in the content whiteboard, and the only way to get the asins into the whiteboard is by using the Amazon post-processor text filter. Along the way I learned how to debug a rails app, what the heck the inject method of Enumerable does, and how to use post-processing text filters. All in all, I feel like I learned a good bit.
1 2 3 4 5 6 7 8 9 10 |
def content asin_list = params[:contents].to_a.inject([]) { |acc, item| acc | item.whiteboard[:asins].to_a } @asins = asin_list.compact[0,@sb_config['maxlinks'].to_i] @assoc_id = @sb_config['associate_id'] if @asins.empty? render :text => "" return end end |
If you're still stuck on figuring out this line of code, set a breakpoint like I explained in the previous post and then when you get to the irb prompt try a few things. First, just type in "params[:contents]" and hit enter. You'll get a big blob of text. Copy and paste this into notepad and study it carefully. You can search for whiteboard, and unless you set up the Amazon post-processing text filter you'll see that none of the whiteboards contain any information. However, plent of other items have information. One item that caught my eye was "title." So, let's play with our confusing line of code and see if we can get an array of blog entry titles. At the irb prompt in the paused breakpointer console window, type in:
1 2 |
params[:contents].to_a.inject([]){|acc, item| acc | item.title.to_a }
|
The reason why the Amazon sidebar wasn't working for me is because nothing was ever getting set into the whiteboard hash key "asins." There was nothing at all in the whiteboard hash. So, I figured there had to be some other mention of the whiteboard somewhere else in the typo code, so I opened the scite text editor and went to the Search > Find in Files... menu item and chose to search for the text "whiteboard" under every .rb file under the typo directory (including subfolders). Looking at the results, it looks like the only place where whiteboard[:asins] is being set in the entire typo app is in the components\plugins\textfilters\amazon_controller.rb file. This is the controller for the Amazon post-processing text filter. Using this filter allows you to enter links to amazon items with an href like this: href="amazon:097669400X" and turns that into something like this: href="http://www.amazon.com/exec/obidos/ASIN/097669400X/scottstuff-20."
So, this is when it finally dawned on me that the only way for the Amazon sidebar component to show links on the side bar is to have the asins show up in the content whiteboard, and the only way to get the asins into the whiteboard is by using the Amazon post-processor text filter. Along the way I learned how to debug a rails app, what the heck the inject method of Enumerable does, and how to use post-processing text filters. All in all, I feel like I learned a good bit.
June 10th, 2007 at 07:21 PM Hey there.. I can't get this sidebar to work! Where is this ASIN in the link for Amazon? I added that text filter and all. Also, do you know what the Associate ID is for? Thanks.
June 10th, 2007 at 07:21 PM Never mind, I get it now.
December 16th, 2007 at 04:51 PM
Guys please; I need a wise advice about online applications. I need a credit card and want to apply online. My credit is not bad. Credit score is 550. Is it safe and secure to make it online? It will be my first experience online therefore I have some apprehensions.
discover 0% for life of balance transfer
December 19th, 2007 at 09:46 AM
Hello guys! Please, somebody, give me wise advice. My credit is really bad. I have one bad credit card and need one more to solve some money problems. How to know whether my application will be denied or not. And is this a reliable website to apply online? I am waiting for your answer. Thanks
discover 0 apr balance transfer bal transfer discover card
January 4th, 2008 at 11:28 AM
I was lucky enough o get my credit card information stolen. My credit history was messed up. It took me awhile to solve the problem. After that I didn’t want to use credit cards online because I didn’t want to screw everything up again. Now I need a new one and I’m thinking about applying for one at
poor credit balance transfer