Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Created an ImageListing class to make controller cleaner.

Remove my own photostream url method, as it is now part of flickraw.
  • Loading branch information...
commit 5dc51cc5ed47d7a25dfe01c1802a09cbd3bb8a21 1 parent 30cbcbc
@funkaoshi authored
Showing with 38 additions and 38 deletions.
  1. +35 −35 groupviewer.rb
  2. +3 −3 views/group.haml
View
70 groupviewer.rb
@@ -2,17 +2,11 @@
require 'flickraw'
require 'haml'
-# make nicer photostream URLs than Flickraw does by default.
-module FlickRaw
- def self.url_photostream(r)
- URL_PHOTOSTREAM +
- if r.respond_to?(:pathalias) and r.pathalias
- r.pathalias
- elsif r.owner.respond_to?(:nsid)
- r.owner.nsid
- else
- r.owner
- end + "/"
+# Models and the like
+
+class String
+ def is_untitled?
+ return self.empty? || self == '.' || self =~ /IMG_/ || self =~ /DSC_/
end
end
@@ -23,10 +17,20 @@ class Image
def initialize(photo)
@id, @img_url, @flickr_url = photo.id, FlickRaw::url(photo), FlickRaw::url_photopage(photo)
@photographer = "<a href='#{FlickRaw::url_photostream(photo)}'>#{photo.ownername}</a>"
- @title = photo.title.empty? ? 'untitled' : photo.title
+ @title = photo.title.is_untitled? ? 'untitled' : photo.title
end
end
+# All the information about a page of images that need to be displayed
+class ImageListing
+ attr_accessor :flickr_id, :title, :page, :pages, :sequence
+
+ def initialize(id, title, page, pages, sequence)
+ @flickr_id, @title, @page, @pages, @sequence = id, title, page, pages, sequence
+ end
+end
+
+
# Sinatra !!
configure do
@@ -50,42 +54,38 @@ def initialize(photo)
end
end
-helpers do
+helpers do
+ def set_paging(params, page)
+ params[:per_page] = 30 unless page == 0
+ params[:page] = page unless page == 0
+ end
+
# Loads 30 medium images from the flickr group
def load_group(group, page)
params = { :group_id => group, :extras => 'path_alias, owner_name' }
- params[:per_page] = 30 unless page == 0
- params[:page] = page unless page == 0
+ set_paging(params, page)
begin
photos = flickr.groups.pools.getPhotos(params)
group_info = flickr.groups.getInfo(:group_id => group)
rescue FlickRaw::FailedResponse => e
halt 404
end
- @group_id = group
- @name = group_info.name
- @mode = 'group'
- @page = page.to_i
- @pages = photos.pages
- @sequence = build_sequence(photos)
+ @list = ImageListing.new(group, group_info.name, page.to_i, photos.pages, build_sequence(photos))
+ @mode = "group"
end
+ # Loads 30 medium images from a flickr user
def load_favs(user_name, page)
user_id = flickr.people.findByUsername(:username => user_name).id
params = { :user_id => user_id, :extras => 'path_alias, owner_name' }
- params[:per_page] = 30 unless page == 0
- params[:page] = page unless page == 0
+ set_paging(params, page)
begin
photos = flickr.favorites.getPublicList(params)
rescue FlickRaw::FailedResponse => e
halt 404
end
- @name = "#{user_name}'s Favourites"
- @group_id = user_name
- @mode = 'favs'
- @page = page.to_i
- @pages = photos.pages
- @sequence = build_sequence(photos)
+ @list = ImageListing.new(user_name, "#{user_name}'s Favourites", page.to_i, photos.pages, build_sequence(photos))
+ @mode = "favs"
end
# build list of images.
@@ -98,21 +98,21 @@ def build_sequence(photos)
def nav_links
next_link =
- if @page == @pages
+ if @list.page == @list.pages
"Next"
else
- "<a class='next_page' href='/#{@mode}/#{@group_id}?pg=#{@page+1}'>Next</a>"
+ "<a class='next_page' href='/#{@mode}/#{@list.flickr_id}?pg=#{@list.page+1}'>Next</a>"
end
prev_link =
- case @page
+ case @list.page
when 1
"Prev"
when 2
- "<a class='prev_page' href='/#{@mode}/#{@group_id}'>Prev</a>"
+ "<a class='prev_page' href='/#{@mode}/#{@list.flickr_id}'>Prev</a>"
else
- "<a class='prev_page' href='/#{@mode}/#{@group_id}?pg=#{@page-1}'>Prev</a>"
+ "<a class='prev_page' href='/#{@mode}/#{@list.flickr_id}?pg=#{@list.page-1}'>Prev</a>"
end
- "#{prev_link} | #{@page} of #{@pages} | #{next_link}"
+ "#{prev_link} | #{@list.page} of #{@list.pages} | #{next_link}"
end
end
View
6 views/group.haml
@@ -3,14 +3,14 @@
%head
%meta(charset='utf-8')
%meta(name='viewport' content='width=680, maximum-scale=1.0')
- %title GROUP VIEWQVZ :: #{@name}
+ %title GROUP VIEWQVZ :: #{@list.title}
%link(rel='stylesheet' href='/site.css' type='text/css' media='screen' title='default style sheet')
%script(type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js')
%script(type='text/javascript' src='/paging_keys.js')
%body
#content
#header
- %h1 Viewing #{@name}
+ %h1 Viewing #{@list.title}
%form(action='/group' method='post')
%p
%label(for='searchbox') View another group?
@@ -19,7 +19,7 @@
#nav
%p= "#{nav_links} &nbsp; &nbsp; &nbsp; j/k keys will move between photos (and pages)"
#images
- - @sequence.each do |img|
+ - @list.sequence.each do |img|
.photo.hentry{:id => img.id}
%a{:href => img.flickr_url}>
%img{:src => img.img_url}>
Please sign in to comment.
Something went wrong with that request. Please try again.