lib/board/candidate_search.rb in board-client-0.1.0 vs lib/board/candidate_search.rb in board-client-0.2.0
- old
+ new
@@ -1,70 +1,45 @@
module Board
- class CandidateSearch < API::Base
+ class CandidateSearch
- DEFAULT_PER_PAGE = 10
+ attr_reader :errors
- attr_reader :params
-
- def initialize(*args)
- @params = args.pop
- self.page = params[:page] || 1
- super
+ def initialize(client, params = {})
+ @client = client
+ @params = params
+ @errors = []
end
- def each_result
- (1..total_pages).each do |page|
- self.page = page
- results.each do |result|
- yield result
- end
- clear
- end
- end
-
- def total_pages
- (total / per_page.to_f).ceil
- end
-
def results
- response['results']
+ initial_search['results']
end
- def total
- response['total']
- end
+ def each_result
+ total = initial_search['total']
+ pages = (total / 10.0).ceil
- def per_page
- params[:per_page] || DEFAULT_PER_PAGE
- end
+ initial_search['results'].each { |r| yield r }
- def page
- params[:page]
+ (2..pages).each do |page|
+ search = @client.candidate_searches(@params.merge(:page => page))
+ results = search['results']
+ results.each { |r| yield r }
+ end
end
- def page=(value)
- params[:page] = value
- end
-
def valid?
- response && (errors.nil? || errors.size == 0)
+ perform_search and @errors.empty?
end
- attr_reader :errors
-
private
- def clear
- @response = nil
- @errors = nil
+ def initial_search
+ @initial_search ||= @client.candidate_searches(@params)
+ rescue Board::Request::Error => e
+ @errors = Yajl::Parser.parse(e.response.body)
end
-
- def response
- @response ||= get "/candidate_searches", params
- rescue RestClient::Exception => e
- @errors = decode_json(e.response)
- end
+ alias perform_search initial_search
end
end