Permalink
Browse files

Listing methods, travis config, some more tests, atomic commits, what…

… atomic commits? 👊
  • Loading branch information...
1 parent f753351 commit 4f973eefb40778980b352a40d63fa4aac0afa24b @nikz nikz committed Apr 15, 2012
Showing with 89 additions and 5 deletions.
  1. +7 −0 .travis.yml
  2. +1 −0 lib/realestate.rb
  3. +37 −2 lib/realestate/client.rb
  4. +1 −1 realestate-ruby.gemspec
  5. +20 −0 test/remote/live_client_test.rb
  6. +1 −0 test/test_helper.rb
  7. +22 −2 test/unit/client_test.rb
View
@@ -0,0 +1,7 @@
+language: ruby
+rvm:
+ - 1.8.7
+ - 1.9.2
+ - 1.9.3
+# uncomment this line if your project needs to run something other than `rake`:
+# script: bundle exec rspec spec
View
@@ -11,6 +11,7 @@ module Realestate
# exceptions...
class CredentialsRequired < StandardError; end
class AuthenticationError < StandardError; end
+ class ApiError < StandardError; end
end
View
@@ -20,6 +20,35 @@ def initialize(options = {})
end
end
+ # List / Search listings
+ def listings(options = {})
+ # max results is more like "results per page"
+ options[:max_results] ||= 100
+ options[:format] ||= "full"
+
+ sort_column = options.delete(:sort)
+ sort_direction = options.delete(:sort_direction)
+ # -price_min for price_min, descending etc..
+ options[:sort_order] = "#{"-" if sort_direction == :desc}#{sort_column}" if sort_column.present?
+
+ listings = []
+ more = true
+ offset = 0
+
+ while more do
+ result = request(:get, "listings", options)
+
+ listings = listings + result["listings"]
+
+ offset = offset + options[:max_results]
+ options[:offset] = offset
+
+ more = result["more"]
+ end
+
+ listings
+ end
+
private
def request(method, path, params = {})
@@ -59,10 +88,16 @@ def calculate_api_sig(request_path, params_array)
end
def process_response(response)
- if response.success?
+ puts @reponse.body
+ case response.code
+ when 200..299
response.parsed_response
- elsif response.code == 401
+ when 401
raise AuthenticationError.new(response.body)
+ when 400
+ raise ApiError.new(response)
+ else
+ raise "Unhandled Response Code: #{response.code}: #{response.body}"
end
end
View
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'test-unit'
gem.add_development_dependency 'shoulda'
gem.add_development_dependency 'redgreen'
- gem.add_development_dependency 'yaml'
+ gem.add_development_dependency 'webmock'
gem.authors = ["Nik Wakelin", "Jared Armstrong", "Glen Barnes"]
gem.description = %q{A Ruby wrapper for the Realestate.co.nz API.}
@@ -75,6 +75,26 @@ class LiveClientTest < ActiveSupport::TestCase
end
+ context "searching for listings" do
+ setup do
+ @client = create_test_client
+ end
+
+ should "be able to look for listings in Fiji" do
+ fiji_listings = @client.listings(:district_id => 1, :format => "id")
+ assert !fiji_listings.empty?
+ end
+
+ should "be able to search for listing ids" do
+ fiji_listings = @client.listings(:district_id => 1, :format => "id")
+
+ ids = fiji_listings.map { |f| f["id"] }[0...3]
+
+ listings_by_id = @client.listings(:id => ids)
+ assert_equal 3, listings_by_id.size
+ end
+ end
+
private
def create_test_client
View
@@ -1,6 +1,7 @@
require 'rubygems'
require 'test/unit'
+require 'webmock/test_unit'
require 'shoulda'
require 'redgreen'
require 'yaml'
View
@@ -27,17 +27,37 @@ class ClientTest < ActiveSupport::TestCase
api_sig = @client.send(:calculate_api_sig, "suburbs/", :a => 5)
# here's a signature I prepared earlier...
- assert_equal "c742042c6a82e517ddd637e1ee027e2d", api_sig
+ assert_equal "963c93415aaba0b605d93daea7faa642", api_sig
end
should "order the params correctly" do
api_sig = @client.send(:calculate_api_sig, "suburbs/", :b => "Z", :a => [6, 3], "baz" => 45)
- assert_equal "1500f0d54ae8bef8c10a9d42e9a694fb", api_sig
+ assert_equal "d1902758cc1feeea3c0a9b6c96d3f7d0", api_sig
end
end
+ context "category methods" do
+ setup do
+ @client = create_test_client
+ end
+
+ should "work with mock data" do
+ flunk
+ end
+ end
+
+ context "requesting listings" do
+ setup do
+ @client = create_test_client
+ end
+
+ should "return all pages with pagination" do
+ flunk
+ end
+ end
+
private

0 comments on commit 4f973ee

Please sign in to comment.