- 浏览: 849580 次
- 性别:
- 来自: lanzhou
文章分类
最新评论
-
liu346435400:
楼主讲了实话啊,中国程序员的现状,也是只见中国程序员拼死拼活的 ...
中国的程序员为什么这么辛苦 -
qw8226718:
国内ASP.NET下功能比较完善,优化比较好的Spacebui ...
国内外开源sns源码大全 -
dotjar:
敢问兰州的大哥,Prism 现在在12.04LTS上可用么?我 ...
最佳 Ubuntu 下 WebQQ 聊天体验 -
coralsea:
兄弟,卫星通信不是这么简单的,单向接收卫星广播信号不需要太大的 ...
Google 上网 -
txin0814:
我成功安装chrome frame后 在IE地址栏前加上cf: ...
IE中使用Google Chrome Frame运行HTML 5
In this post I’m going to show you how I created the little Flickr stream you can see running down the right hand edge of this site.
Step 1: Get a Flickr API key
Visit this page and follow the instructions to get a key and write it down somewhere safe. Of course you are going to need a Flickr account to do this as well!
Step 2: Install the flickr_fu
gem
This is the library that makes all the magic happen. Install the gem on your machine:
1 |
sudo gem install flickr-fu |
Notice that’s a hyphen between ‘flickr’ and ‘fu’, not an underscore. And remember that you’ll need to install this gem on your production server as well, so make a note to do that.
Step 3: Tell your rails app to include the flickr_fu
library
Just a quick visit to environment.rb
to pull it in:
1 2 3 |
# config/environment.rb require ' flickr_fu ' |
Underscore used this time. Very confusing!
Step 4: Configure the gem using a flickr.yml
file
The best place to store this is in your config
directory along with all your other config settings. Get that piece of paper handy that you wrote down your API
key on because you will need it now:
1 2 3 4 5 |
# config/flickr.yml key: " <paste your key in here> " secret: " <paste your secret in here> " token_cache: " token_cache.yml " |
You won’t need the API key any more so you can eat that piece of paper.
Step 5: Have a play!
Right. Time to mess with it! What we are going to do now is fire up the rails console and retrieve some of your images. However the flickr APIs need to know your Flickr ID. For this we will use a website called idGettr
What you do is paste in the URL
for your Flickr photostream and it spits out your Flickr ID which we can then use with the APIs. So for me I typed in http://www.flickr.com/photos/antonjenkins/
and it returned an ID of 12864272@N02
.
Write this down somewhere, memorise it and then eat the piece of paper.
So lets have a play in the rails console:
1 2 3 4 |
# ./script/console >> flickr = Flickr .new(File .join(RAILS_ROOT , ' config ' , ' flickr.yml ' )) => #<Flickr::Base:0x395d514 @token_cache="token_cache.yml", @api_secret="oooh, that's a secret!", @api_key="I could tell you but I'd have to kill you"> |
What we’ve done there is pointed flickr_fu
to our little flickr.yml
file and it’s given us back a connection to Flickr. So lets use it (and that Flickr ID we looked up a minute ago)....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
>> photos = flickr.photos.search(:user_id => " 12864272@N02 " ) # Boom! Loads of output! >> photos.count => 90 # I seem to have an array of photos. Lets look at one... >> photos[1 ].title => " Quack! " >> photos[1 ].url => " http://farm4.static.flickr.com/3104/3303703736_ba4bea1dc5.jpg " # Jackpot! |
So we’re very happy now – we’re using ruby to talk to Flickr! What we need to do now is get this on to our website.
Step 6: Writing a flickr helper
It’s a good idea to separate out functionality like this into
helpers and partials, rather than weave it directly into your existing
code. So we’re going to create a flickr_helper.rb
in the app/helpers
directory like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# app/helpers/flickr_helper.rb module FlickrHelper def user_photos (user_id, photo_count = 12 ) flickr = Flickr .new(File .join(RAILS_ROOT , ' config ' , ' flickr.yml ' )) flickr.photos.search(:user_id => user_id).values_at(0 ..(photo_count - 1 )) end def render_flickr_sidebar_widget (user_id, photo_count = 12 , columns = 2 ) begin photos = user_photos(user_id, photo_count).in_groups_of(2 ) render :partial => ' /flickr/sidebar_widget ' , :locals => { :photos => photos } rescue Exception render :partial => ' /flickr/unavailable ' end end end |
The method which we will be calling from our view to display our flickr photos is:
1 |
render_flickr_sidebar_widget(user_id, photo_count = 12 , columns = 2 ) |
This method will prepare an array of photos and pass them to a
partial (which we’ll get to in a minute). You may have noticed the call
to in_groups_of
which is quite interesting:
1 |
photos = user_photos(user_id, photo_count).in_groups_of(2
)
|
Because we want two columns of photos in our little sidebar we need to group the array of photos. This is where in_groups_of
comes into play. Let’s see how it works on the rails console:
1 2 3 4 5 6 7 8 |
>> Array(1 ..10 ).in_groups_of(4 ) => [[1 , 2 , 3 , 4 ], [5 , 6 , 7 , 8 ], [9 , 10 , nil , nil ]] # Notice how it pads out the last array with nils # We want to do group our flickr photos into groups of 2, one for each column >> Array(1 ..6 ).in_groups_of(2 ) => [[1 , 2 ], [3 , 4 ], [5 , 6 ]] |
So our photos will group like so:
This grouped array gets passed to the flickr/sidebar_widget
partial. Let’s take a look at that now:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# app/views/flickr/_sidebar_widget.html.erb <div class =" flickr " > <ul> <% photos.each do |row| -%> <li> <% row.each do |p| %> <%= link_to(image_tag(p.url(:square ), :class => ' flickr_photo ' , :title => p.title, :border => 0 , :size => ' 75x75 ' ), p.url_photopage) %> <% end %> </li> <% end -%> </ul> </div> |
I’m then styling this with the following CSS :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
.flickr ul { list-style-type: none; margin: 0; padding: 0; } .flickr ul li { border-bottom: 0; margin: 0; padding: 0; } .flickr_photo { width: 75px; height: 75px; padding: 12px 10px 32px 13px; margin: 0; background: #fff url(../images/pixellated/polaroid.jpg) no-repeat; } |
Each flickr thumbnail is placed on top of a little fake polaroid image I knocked up:
You may have also noticed a call in our flickr helper to an unavailable
partial if there was an exception whilst trying to speak to flickr:
1 2 3 |
rescue Exception render :partial => ' /flickr/unavailable ' end |
I’m leaving that partial blank so if flickr is down it just doesn’t
render anything. But if you want you can display something else instead
of the flickr output then place it in the _unavailable.html.erb
partial.
Step 7: Call the helper in your application.html.erb
layout template
Just a simple call to…
1 |
<%=
render_flickr_sidebar_widget('
12864272@N02
'
) %>
|
Of course you’ll put your own flickr ID in there instead of mine. Unless you really like my photos?! ;o)
Are we done yet?
Ah. Not quite! You may have noticed something about this when you started testing it…... it’s slow as hell! Every single page render now has to wait for a round trip to flickr. Not cool!
Step 8: Fragment caching to the rescue!
This is a perfect candidate for fragment caching. The flickr photos aren’t going to be changing too often so we can cache them and expire them when we know they have changed.
So we’re going to surround the call to the flickr helper in a fragment cache block:
1 2 3 |
<% cache (" flickr_sidebar " ) do %> <%= render_flickr_sidebar_widget(' 12864272@N02 ' ) %> <% end %> |
Remember this won’t speed up your renders on your development server as caching is disabled by default in development, but on production it will fly. It won’t cache the images (and why would you want to as flickr is built to serve images?) but it will cache the actual HTML code which required the costly API calls to flickr in order to generate.
Now we need some code to expire this cache when you’ve added some photos to flickr and you want to update the photos on the website. In fact, it would be really cool if you could expire this cache using capistrano, so you wouldn’t even need to ssh into your server to do this. For this I refer you to my previous post on expiring page and fragment caches using capistrano .
发表评论
-
Rails 3 Beta版本月将出 Merb融合带来选择
2010-01-11 09:48 1419Rails 3,目前流行Web开发框架Rails的一个升级版 ... -
MerbAdmin:Merb数据管理好帮手
2010-01-11 09:43 907Merb中要加入类似Django的Admin功能早有传闻,如今 ... -
rails cms
2009-12-28 20:29 1669Rails CMS alternatives ======= ... -
Generating Thousands of PDFs on EC2 with Ruby
2009-12-24 18:01 1038The Problem For about two mont ... -
Shrink your JavaScript with the Google Compiler Rails Plugin
2009-11-16 11:27 933Like it or not, JavaScript has ... -
Thank you, Rails
2009-11-06 18:21 567It’s fashionable, or perhaps in ... -
Top 50 Ruby on Rails Websites
2009-10-31 15:18 943We’re big fans of Ruby on Rails ... -
Let a human test your app, not (just) unit tests
2009-10-31 09:26 853I’m a big believer in unit test ... -
Heroku Gets Add-Ons: Serious Ruby Webapp Hosting Made Easy
2009-10-30 07:37 913Heroku is a Ruby webapp hosti ... -
Rails + Google Analytics = easy goal tracking
2009-10-29 20:38 892Google Analytics is an indis ... -
Ruby on Rails Roadshow in Austin Thursday
2009-10-29 14:25 808Justin Britten founded Prefine ... -
Ruby on Rails and the importance of being stupid
2009-10-21 08:13 806A tale of two servers… Server ... -
How a 1-Engineer Rails Site Scaled to 10 Million Requests Per Day
2009-10-20 14:49 775Ravelry is an online knitting ... -
Installing Rails on CentOS 5
2009-10-20 14:24 1191Note: Since this post origina ... -
CentOS配置lighttpd和rails
2009-10-20 14:22 1123lighttpd版本:1.4.18 fastcgi版本: ... -
Cells:将组件开发带入Ruby2.3
2009-10-20 09:17 1117cells "将使得面向组 ... -
High Quality Ruby on Rails Example Applications
2009-10-15 16:34 1460Sometimes to best way to get ... -
Install Passenger on Ubuntu
2009-10-07 10:17 806Phusion Passenger is one of the ... -
Installing Ruby on Rails with Apache on Ubuntu 9.04 (Jaunty)
2009-10-07 10:00 1015Installing Passenger and Depe ... -
Ruby on Rails with Nginx on Ubuntu 9.04 (Jaunty)
2009-10-07 09:57 1066Install Required Packages ...
相关推荐
Developing and using Win/Mac Libraries FireMonkey is the first native CPU and GPU powered platform for rich business applications. In this paper, we will look at what is required to create a shared ...
### 整合 JMeter 与 Ant 的方法及实践 #### 概述 在软件开发过程中,性能测试是一项至关重要的任务,它有助于确保应用程序能够在高负载下稳定运行。JMeter 是一款广泛使用的开源负载测试工具,它能够模拟大量并发...
【ORDERED NEURONS INTEGRATING TREE STRUCTURES INTO RECURRENT NEURA】 自然语言的理解与处理在人工智能领域中占据着核心地位,尤其是对于自然语言处理(NLP)算法来说,理解和利用语言的内在层次结构至关重要。...
This book is your step-by-step guide to building your business website in Drupal, complete with a blog, events calendar, catalogue of products and services, and more. In this book, we will build a ...
Integrating the Orca Optimizer into MySQL 在 MySQL 中集成 Orca 优化器是为了解决 MySQL 查询优化器的限制,特别是在处理复杂查询时。MySQL 查询优化器原本设计用于简单的 OLTP 类型查询,但是在处理更复杂的...
to-end guide on how to dismantle your monolithic application and embrace the microservice architecture - right from architecting your services and modeling them to integrating them into your ...
Finally NVIDIA will also provide some tips and guideline for integrating DXR into your engine, and discuss open challenges. Speakers: Ignacio Llamas, Senior Manager, Real Time Rendering Software &...
"Integrating it into my app can be a pain..." Not anymore! simple-chrome-custom-tabs will help you with that. Description simple-chrome-custom-tabs provides easy integration of Chrome Custom Tabs ...
【Zenoss 网络管理】与【Opengear 控制台服务器集成】是现代数据中心IT管理的关键技术。Zenoss Enterprise 是一款先进的IT管理产品,它优化了对业务应用程序和服务支持基础设施的监控,尤其在动态的数据中心环境中。...
After beginning with an introduction to testing in R, the book explores more advanced cases such as integrating tests into R packages; testing code that accesses databases; testing C++ code with Rcpp;...
Expand your imagination by letting go of the limitations of traditional animation mediums, software packages, or workflows and integrating 2D and 3D assets. With the updated and expanded second ...
This book takes the mystery out of working with the Dart language and integrating Flutter into your already existing workflows and development projects. With Flutter Recipes, you’ll learn how to ...
Apress - Integrating Serverless Architecture.2019.epub
1 Learn how to leverage Big Data by effectively integrating it into your data warehouse. 2 Includes real-world examples and use cases that clearly demonstrate Hadoop, NoSQL, HBASE, Hive, and other Big...
《Integrating Voice and Data Networks》是由Cisco Press出版的一本经典教材,主要面向网络管理人员、开发人员以及希望使用Cisco公司的语音和数据集成产品的CCIE(Cisco Certified Internetwork Expert,思科认证...
NASA 应用遥感培训 (ARSET) NASA ARSET为灾害监测和其他遥感主题提供网络研讨会,其中许多使用地球引擎。 华盛顿大学 此页面具有出色的集科开发的入门教程的 凯瑟琳·库恩和吉利安Deines为电子科学研究所的...