forked from kevintuhumury/cameraplus
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4de3a4a
commit 09616b3
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |