Skip to content

Commit

Permalink
added a base resource class, added the agent resource with find_all, …
Browse files Browse the repository at this point in the history
…find and find_by_id
  • Loading branch information
tcocca committed Nov 19, 2010
1 parent 3087353 commit 24050a8
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 10 deletions.
6 changes: 4 additions & 2 deletions lib/you_got_listed.rb
Expand Up @@ -5,6 +5,8 @@
require 'will_paginate'

require 'you_got_listed/client'
require 'you_got_listed/accounts'
require 'you_got_listed/error'
require 'you_got_listed/resource'
require 'you_got_listed/response'
require 'you_got_listed/error'
require 'you_got_listed/accounts'
require 'you_got_listed/agent'
10 changes: 2 additions & 8 deletions lib/you_got_listed/accounts.rb
@@ -1,14 +1,8 @@
module YouGotListed
class Accounts

attr_accessor :client

def initialize(client)
self.client = client
end
class Accounts < Resource

def search(optional_params = {})
Response.new(self.client.class.get("/accounts/search.php", optional_params))
process_get("/accounts/search.php", optional_params)
end

end
Expand Down
24 changes: 24 additions & 0 deletions lib/you_got_listed/agent.rb
@@ -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
15 changes: 15 additions & 0 deletions lib/you_got_listed/resource.rb
@@ -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
88 changes: 88 additions & 0 deletions spec/you_got_listed/agent_spec.rb
@@ -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
17 changes: 17 additions & 0 deletions spec/you_got_listed/resource_spec.rb
@@ -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

0 comments on commit 24050a8

Please sign in to comment.