From 24050a8f2a2a306a3415fbbee402d6abca3ee7e1 Mon Sep 17 00:00:00 2001 From: tcocca Date: Fri, 19 Nov 2010 12:14:06 -0500 Subject: [PATCH] added a base resource class, added the agent resource with find_all, find and find_by_id --- lib/you_got_listed.rb | 6 +- lib/you_got_listed/accounts.rb | 10 +--- lib/you_got_listed/agent.rb | 24 ++++++++ lib/you_got_listed/resource.rb | 15 +++++ spec/you_got_listed/agent_spec.rb | 88 ++++++++++++++++++++++++++++ spec/you_got_listed/resource_spec.rb | 17 ++++++ 6 files changed, 150 insertions(+), 10 deletions(-) create mode 100644 lib/you_got_listed/agent.rb create mode 100644 lib/you_got_listed/resource.rb create mode 100644 spec/you_got_listed/agent_spec.rb create mode 100644 spec/you_got_listed/resource_spec.rb diff --git a/lib/you_got_listed.rb b/lib/you_got_listed.rb index bf35a44..5cd0c71 100644 --- a/lib/you_got_listed.rb +++ b/lib/you_got_listed.rb @@ -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' diff --git a/lib/you_got_listed/accounts.rb b/lib/you_got_listed/accounts.rb index 1894981..dea16e7 100644 --- a/lib/you_got_listed/accounts.rb +++ b/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 diff --git a/lib/you_got_listed/agent.rb b/lib/you_got_listed/agent.rb new file mode 100644 index 0000000..a9afbd2 --- /dev/null +++ b/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 diff --git a/lib/you_got_listed/resource.rb b/lib/you_got_listed/resource.rb new file mode 100644 index 0000000..a5e5692 --- /dev/null +++ b/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 diff --git a/spec/you_got_listed/agent_spec.rb b/spec/you_got_listed/agent_spec.rb new file mode 100644 index 0000000..098ed6b --- /dev/null +++ b/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 diff --git a/spec/you_got_listed/resource_spec.rb b/spec/you_got_listed/resource_spec.rb new file mode 100644 index 0000000..cb951ea --- /dev/null +++ b/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