Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a base resource class, added the agent resource with find_all, …
…find and find_by_id
- Loading branch information
Showing
6 changed files
with
150 additions
and
10 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
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,24 @@ | ||
module YouGotListed | ||
class Agent < Resource | ||
|
||
def find_all | ||
process_get("/agents/search.php") | ||
end | ||
|
||
def find_by_id(agent_id) | ||
get_agent(agent_id, false) | ||
end | ||
|
||
def find(agent_id) | ||
get_agent(agent_id) | ||
end | ||
|
||
private | ||
|
||
def get_agent(agent_id, raise_error = true) | ||
params = {:id => agent_id} | ||
process_get("/agents/search.php", params, raise_error) | ||
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,15 @@ | ||
module YouGotListed | ||
class Resource | ||
|
||
attr_accessor :client | ||
|
||
def initialize(client) | ||
self.client = client | ||
end | ||
|
||
def process_get(path, params = {}, raise_error = true) | ||
Response.new(self.client.class.get(path, :query => params), raise_error) | ||
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,88 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | ||
|
||
describe YouGotListed::Agent do | ||
|
||
before do | ||
@ygl = new_ygl | ||
@agent = YouGotListed::Agent.new(@ygl) | ||
end | ||
|
||
context "find_all" do | ||
before do | ||
VCR.use_cassette('agent.find_all') do | ||
@response = @agent.find_all | ||
end | ||
end | ||
|
||
it "should be a success" do | ||
@response.success?.should be_true | ||
end | ||
|
||
it "should return an array of agents" do | ||
@response.agents.agent.should_not be_nil | ||
@response.agents.agent.should be_kind_of(Array) | ||
end | ||
end | ||
|
||
context "successful find" do | ||
before do | ||
VCR.use_cassette('agent.find') do | ||
@response = @agent.find('AG-001-046') | ||
end | ||
end | ||
|
||
it "should be a success" do | ||
@response.success?.should be_true | ||
end | ||
|
||
it "should return an array of agents" do | ||
@response.agents.agent.should_not be_nil | ||
@response.agents.agent.should be_kind_of(Hashie::Rash) | ||
end | ||
end | ||
|
||
context "unsuccessful find" do | ||
it "should raise an exception" do | ||
lambda { | ||
VCR.use_cassette('agent.find.error') do | ||
@response = @agent.find('AG-001-04999') | ||
end | ||
}.should raise_exception(YouGotListed::Error, "YouGotListed Error: Invalid user id. (code: 301)") | ||
end | ||
end | ||
|
||
context "successful find_by_id" do | ||
before do | ||
VCR.use_cassette('agent.find') do | ||
@response = @agent.find_by_id('AG-001-046') | ||
end | ||
end | ||
|
||
it "should be a success" do | ||
@response.success?.should be_true | ||
end | ||
|
||
it "should return an array of agents" do | ||
@response.agents.agent.should_not be_nil | ||
@response.agents.agent.should be_kind_of(Hashie::Rash) | ||
end | ||
end | ||
|
||
context "unsuccessful find_by_id" do | ||
it "should raise an exception" do | ||
lambda { | ||
VCR.use_cassette('agent.find.error') do | ||
@response = @agent.find_by_id('AG-001-04999') | ||
end | ||
}.should_not raise_exception | ||
end | ||
|
||
it "should not be a success" do | ||
VCR.use_cassette('agent.find.error') do | ||
@response = @agent.find_by_id('AG-001-04999') | ||
end | ||
@response.success?.should be_false | ||
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,17 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | ||
|
||
describe YouGotListed::Resource do | ||
|
||
before do | ||
@ygl = new_ygl | ||
@resource = YouGotListed::Resource.new(@ygl) | ||
end | ||
|
||
context "initialize" do | ||
it "should instantiate with a client" do | ||
@resource.client.should_not be_nil | ||
@resource.client.should be_kind_of(YouGotListed::Client) | ||
end | ||
end | ||
|
||
end |