Permalink
Browse files

Added the Cameraplus::Search class.

  • Loading branch information...
1 parent 4de3a4a commit 09616b36ec4d58bf9fc928d08b7d09bd26676dd3 @kevintuhumury kevintuhumury committed Mar 25, 2012
Showing with 97 additions and 0 deletions.
  1. +1 −0 lib/cameraplus.rb
  2. +45 −0 lib/cameraplus/search.rb
  3. +51 −0 spec/cameraplus/search_spec.rb
View
@@ -13,5 +13,6 @@
require "cameraplus/photo_exif"
require "cameraplus/photo_recipe"
require "cameraplus/comment"
+require "cameraplus/search"
require "cameraplus/exceptions"
require "cameraplus/version"
View
@@ -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
@@ -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.