Skip to content

Commit

Permalink
changed name for gem
Browse files Browse the repository at this point in the history
  • Loading branch information
useruby committed Sep 10, 2012
1 parent 3872e6e commit 70df17a
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 0 deletions.
22 changes: 22 additions & 0 deletions flickr_top_ten.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/flickr_top_ten/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Alexey Artamonov"]
gem.email = ["i@useruby.com"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "flickr_top_ten"
gem.require_paths = ["lib"]
gem.version = FlickrTopTen::VERSION

gem.add_runtime_dependency('flickraw')
gem.add_development_dependency('rspec')
gem.add_development_dependency('debugger')
gem.add_development_dependency('timecop')
end
11 changes: 11 additions & 0 deletions lib/flickr_top_ten.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'flickraw'

require 'flickr_top_ten/version'
require 'flickr_top_ten/core'
require 'flickr_top_ten/exception'
require 'flickr_top_ten/group'
require 'flickr_top_ten/group_set'
require 'flickr_top_ten/photo'

module FlickrTopTen
end
14 changes: 14 additions & 0 deletions lib/flickr_top_ten/core.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module FlickrTopTen
class Core
def self.client
unless @client
FlickRaw.api_key = FlickrTopTen::API_KEY
FlickRaw.shared_secret= FlickrTopTen::SHARED_SECRET

@client = FlickRaw::Flickr.new
end

@client
end
end
end
2 changes: 2 additions & 0 deletions lib/flickr_top_ten/exception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class FlickrTopTen::Exception < Exception
end
46 changes: 46 additions & 0 deletions lib/flickr_top_ten/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'flickr_top_ten/group_filters'

module FlickrTopTen
class Group
include GroupFilters

def initialize attrs = {}
if attrs.has_key? :group_id
@group_id = attrs[:group_id]
else
raise FlickrTopTen::Exception.new('group id is not set')
end
end

def photos attrs = {}
client_groups.pools.getPhotos attrs.merge(group_id: @group_id, extras: 'date_upload, views, url_n')
end

def today_photos
per_page = 500
page = 1
all_photos = photos(per_page: per_page, page: page).select {|photo| select_today_photos(photo)}

while all_photos.size == per_page*page
page += 1
all_photos += photos(per_page: per_page, page: page).select {|photo| select_today_photos(photo)}
end

all_photos
end

protected
def client_groups
Core.client.groups
end

def select_today_photos photo
dateadded = Time.at(photo.dateadded.to_i)
dateupload = Time.at(photo.dateupload.to_i)
today = Time.now

dateadded.day == today.day && dateadded.month == today.month && dateadded.year == today.year \
&& dateupload.day == today.day && dateupload.month == today.month && dateupload.year == today.year
end
end
end
8 changes: 8 additions & 0 deletions lib/flickr_top_ten/group_filters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module FlickrTopTen::GroupFilters
def ten_most_viewed_today_photos
photos = today_photos
.select{|photo| photo.views.to_i > 50}
.map{|photo| FlickrTopTen::Photo.new photo_id: photo.id, secret: photo.secret}
.sort{|photo1, photo2| photo2 <=> photo1}[0...10]
end
end
20 changes: 20 additions & 0 deletions lib/flickr_top_ten/group_set.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'flickr_top_ten/group_filters'

module FlickrTopTen
class GroupSet
include GroupFilters

def initialize attrs = {}
if attrs.has_key?(:group_ids) && !attrs[:group_ids].empty?
@groups = attrs[:group_ids].map {|group_id| Group.new group_id: group_id}
else
raise Exception.new('no group in group set')
end
end

def today_photos
@groups.inject([]){|photos, group| photos + group.today_photos}
.uniq {|photo| photo['id'] + photo['secret']}
end
end
end
60 changes: 60 additions & 0 deletions lib/flickr_top_ten/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module FlickrTopTen
class Photo
include Comparable

attr_reader :photo_id, :secret, :views, :comments

def initialize attrs = {}
@_info = flickr_photos.getInfo attrs

@photo_id = @_info.id
@secret = @_info.secret
@views = @_info.views.to_i
@comments = @_info.comments.to_i
@dateuploaded = Time.at(@_info.dateuploaded.to_i)
end

def uploaded_today?
today = Time.now

@dateuploaded.day == today.day && @dateuploaded.month == today.month \
&& @dateuploaded.year == today.year
end

def favorites
unless @favorites
per_page = 50
page = 1
favorites_count = 0

begin
favorites = flickr_photos.getFavorites photo_id: @photo_id, per_page: per_page, page: page

page += 1
favorites_count += favorites.person.size
end while favorites.person.size == per_page

@favorites = favorites_count
end

@favorites
end

def <=> photo
popularity <=> photo.popularity
end

def popularity
favorites*0.8 + views*0.1 + comments*0.2
end

def url type = :photopage
FlickRaw.url_photopage(@_info) if type == :photopage
end

protected
def flickr_photos
Core.client.photos
end
end
end
3 changes: 3 additions & 0 deletions lib/flickr_top_ten/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module FlickrTopTen
VERSION = "0.0.1"
end
9 changes: 9 additions & 0 deletions spec/flickr_top_ten/core_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'spec_helper'

describe FlickrTopTen::Core do
describe '.client' do
it 'should return flickr client' do
FlickrTopTen::Core.client.should_not be_nil
end
end
end
39 changes: 39 additions & 0 deletions spec/flickr_top_ten/group_set_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

# Raw Street Photography - 355036@N23
# Just Street Photography - 97329809@N00

describe FlickrTopTen::GroupSet do
describe '#initialize' do
it 'should create a group set object' do
FlickrTopTen::GroupSet.new(group_ids: ['355036@N23', '97329809@N00']).should_not be_nil
end

it 'should raise exception if no group in group set' do
lambda {
FlickrTopTen::GroupSet.new
}.should raise_exception(FlickrTopTen::Exception, 'no group in group set')
end
end

context 'raw street photography and just street photography' do
before :each do
@group_set = FlickrTopTen::GroupSet.new(group_ids: ['355036@N23', '97329809@N00'])
end

describe '#today_photos' do
it 'should return list of photos that was uploaded to groups today' do
photos = @group_set.today_photos
photos.should_not be_nil
photos.should_not be_empty
end

it 'should return list where each photo have uniq id' do
photos = @group_set.today_photos
photos.size.should == photos.map{|photo| photo['id']}.uniq.size
end
end

it_behaves_like FlickrTopTen::GroupFilters
end
end
52 changes: 52 additions & 0 deletions spec/flickr_top_ten/group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'spec_helper'

# Raw Street Photography - 355036@N23

describe FlickrTopTen::Group do
describe '#initialize' do
it 'should create a new object for group with selected id' do
FlickrTopTen::Group.new(group_id: '355036@N23').should_not be_nil
end

it 'should raise exception if group id is not set' do
lambda{
FlickrTopTen::Group.new
}.should raise_exception(FlickrTopTen::Exception, 'group id is not set')
end
end

context 'group raw street photography' do
before :each do
@group = FlickrTopTen::Group.new group_id: '355036@N23'
end

describe '#photos' do
it 'should return first 100 photos from group pool' do
photos = @group.photos
photos.should_not be_nil
photos.size.should == 100
photos.first.id.should_not be_nil
end
end

describe '#today_photos' do
it 'should return all photos that was uploaded today' do
photos = @group.today_photos
photos.should_not be_nil
photos.size.should > 0

today = Time.now

photos.each do |photo|
dateadded = photo.dateadded.to_i
Time.at(dateadded).day.should == today.day
Time.at(dateadded).month.should == today.month
Time.at(dateadded).year.should == today.year
end
end
end

it_behaves_like FlickrTopTen::GroupFilters

end
end
59 changes: 59 additions & 0 deletions spec/flickr_top_ten/photo_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe FlickrTopTen::Photo do
before :each do
@photo = FlickrTopTen::Photo.new photo_id: '7863598172', secret: '99a679ffe4'
end

describe '#initialize' do
it 'should create a new photo object' do
@photo.should_not be_nil
@photo.photo_id.should == '7863598172'
@photo.secret.should == '99a679ffe4'
@photo.views.should > 0
@photo.comments.should > 0
end
end

describe '#uploaded_today?' do
it 'should return true if photo was uploaded today' do
Timecop.freeze(Time.local(2012, 'aug', 26, 10)) do
@photo.should be_uploaded_today
end
end

it 'should return false if photo was uploaded not today' do
@photo.should_not be_uploaded_today
end
end

describe '#favorites' do
it 'should return count of favorites for selected photo' do
@photo.favorites.should > 0
end
end

describe '#<=>' do
it 'should return -1 if another photo has more favorites, views and comments' do
photo = FlickrTopTen::Photo.new photo_id: '7724668216', secret: 'de43a60894'

(@photo <=> photo).should == -1
end

it 'should return 1 if another photo has less favorites, views and comments' do
photo = FlickrTopTen::Photo.new photo_id: '7614778386', secret: '875748a57e'

(@photo <=> photo).should == 1
end

it 'should return 0 if another photo has same counts of favorites, views and comments' do
(@photo <=> @photo).should == 0
end
end

describe '#url' do
it 'should return url for photopage' do
@photo.url.should == 'http://www.flickr.com/photos/69546369@N08/7863598172'
end
end
end

0 comments on commit 70df17a

Please sign in to comment.