Skip to content
Browse files

Added balance check function with tests.

  • Loading branch information...
1 parent edd8d85 commit ab46476f28cf8c06407240c364f9c7be068c838e @dosire committed Apr 18, 2012
Showing with 68 additions and 0 deletions.
  1. +5 −0 lib/copyscape.rb
  2. +14 −0 lib/copyscape/balance.rb
  3. +4 −0 lib/copyscape/request_base.rb
  4. +1 −0 test/balance.html
  5. +6 −0 test/balance.xml
  6. +38 −0 test/balance_test.rb
View
5 lib/copyscape.rb
@@ -1,6 +1,7 @@
require 'copyscape/response'
require 'copyscape/url_search'
require 'copyscape/text_search'
+require 'copyscape/balance'
require 'copyscape/version'
module Copyscape
@@ -16,6 +17,10 @@ def url_search(url)
def text_search(*prms)
TextSearch.new(*prms)
end
+
+ def balance
+ Balance.new
+ end
end
View
14 lib/copyscape/balance.rb
@@ -0,0 +1,14 @@
+require 'copyscape/request_base'
+
+module Copyscape
+
+ class Balance < RequestBase
+
+ def initialize(format = :xml, encoding = 'UTF-8')
+ http_response = get_response_balance(format)
+ @response = Response.new(http_response)
+ end
+
+ end
+
+end
View
4 lib/copyscape/request_base.rb
@@ -32,6 +32,10 @@ def get_response(params)
def post_response(params)
self.class.post('/', :body => base_params.merge(params)).body
end
+
+ def get_response_balance(format)
+ self.class.get("/?u=#{Copyscape.username}&k=#{Copyscape.api_key}&o=balance&f=#{format.to_s}").body
+ end
end
View
1 test/balance.html
@@ -0,0 +1 @@
+<html><head><title>Copyscape Premium balance for joe</title></head><body><B>$9.25</B> remaining, <B>185</B> searches remaining, <B>185</B> available today.</body></html>
View
6 test/balance.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<remaining>
+ <value>9.25</value>
+ <total>185</total>
+ <today>185</today>
+</remaining>
View
38 test/balance_test.rb
@@ -0,0 +1,38 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class BalanceTest < Test::Unit::TestCase
+
+ context "request balance in xml" do
+ setup do
+ Copyscape::Balance.expects(:get).once.returns(balance_response_duplicate(:xml))
+ @balance = Copyscape::Balance.new(:xml)
+ end
+
+ should "have the correct url" do
+ assert_match raw_response_duplicate(:xml), @balance.raw_response
+ end
+ end
+
+ context "request balance in html" do
+ setup do
+ Copyscape::Balance.expects(:get).once.returns(balance_response_duplicate(:html))
+ @balance = Copyscape::Balance.new(:html)
+ end
+
+ should "have the correct url" do
+ assert_match raw_response_duplicate(:html), @balance.raw_response
+ end
+ end
+
+ private
+
+ def balance_response_duplicate(format)
+ o = Object.new
+ o.stubs(:body).returns(File.read(File.expand_path("../balance.#{format.to_s}", __FILE__)))
+ o
+ end
+
+ def raw_response_duplicate(format)
+ balance_response_duplicate(format).body
+ end
+end

0 comments on commit ab46476

Please sign in to comment.
Something went wrong with that request. Please try again.