Skip to content

Commit

Permalink
Added the Cameraplus::Search class.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevintuhumury committed Mar 25, 2012
1 parent 4de3a4a commit 09616b3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cameraplus.rb
Expand Up @@ -13,5 +13,6 @@
require "cameraplus/photo_exif"
require "cameraplus/photo_recipe"
require "cameraplus/comment"
require "cameraplus/search"
require "cameraplus/exceptions"
require "cameraplus/version"
45 changes: 45 additions & 0 deletions lib/cameraplus/search.rb
@@ -0,0 +1,45 @@
module Cameraplus
class Search

attr_reader :page, :user, :photos

def self.find(filters)
results = Cameraplus::API::Search.find filters
results.map { |result| new result }
end

def initialize(result)
@result ||= result
parse
end

private

def parse
parse_page
parse_user if has_user?
parse_photos if has_photos?
end

def parse_page
@page ||= Page.new @result
end

def parse_user
@user ||= User.new @result
end

def parse_photos
@photos ||= @result.images.map { |photo| Photo.new photo }
end

def has_user?
@result.has_key? "user"
end

def has_photos?
@result.has_key? "images"
end

end
end
51 changes: 51 additions & 0 deletions spec/cameraplus/search_spec.rb
@@ -0,0 +1,51 @@
require "spec_helper"

describe Cameraplus::Search do

context "by username" do

use_vcr_cassette :search

let(:results) { Cameraplus::Search.find username: "mostlylisa" }

it "should find a list of results" do
results.should be_an Array
end

context "with a Search object selected" do

let(:result) { results.first }

context "#page" do

it "should be a Page" do
result.page.should be_a Cameraplus::Page
end

end

context "#user" do

it "should be a User" do
result.user.should be_a Cameraplus::User
end

end

context "#photos" do

it "should be an Array" do
result.photos.should be_an Array
end

it "should contain a Cameraplus::Photo" do
result.photos.first.should be_a Cameraplus::Photo
end

end

end

end

end

0 comments on commit 09616b3

Please sign in to comment.