lib/ecoportal/api/v1/people.rb in ecoportal-api-0.7.0 vs lib/ecoportal/api/v1/people.rb in ecoportal-api-0.7.1
- old
+ new
@@ -25,34 +25,47 @@
# - it ignores the key `cursor_id:` of `params:`.
# - `each` is called by `to_a`
# @param params [Hash]
# @option params [String] :per_page the number of people you get per request.
# @option params [String] :q some text to search. Omit this parameter to target all the people.
+ # @param silent [Boolean] `false` to show percentage of progress.
# @yield [person] does some stuff with the person.
# @yieldparam person [Person]
- def each(params: {}, &block)
- return to_enum(:each) unless block
- cursor_id = nil
+ def each(params: {}, silent: false, &block)
+ return to_enum(:each, params: params, silent: silent) unless block
+ cursor_id = nil; results = 0
+ puts "\n"
loop do
params.update(cursor_id: cursor_id) if cursor_id
response = client.get("/people", params: params)
- raise "Request failed." unless response.success?
+ raise "Request failed - Status #{response.status}: #{response.body}" unless response.success?
+
+ unless silent || (total = response.body["total_results"]) == 0
+ results += response.body["results"].length
+ percent = results * 100 / total
+ msg = "People GET"
+ msg += " (search=#{params[:q]})" if params.key?(:q)
+ print "#{msg}: #{percent.round}% (of #{total})\r"
+ $stdout.flush
+ end
+
response.body["results"].each do |person|
yield person_class.new(person)
end
break unless (cursor_id = response.body["cursor_id"])
end
self
end
# Gets all the people via api requests.
- # @note it ignores the key `:page` in `params:`.
+ # @note it ignores the key `:cursor_id` in `params:`.
# @param params [Hash]
# @option params [Integer] :per_page the number of people you get per request.
# @option params [String] :q some text to search.
+ # @param silent [Boolean] `false` to show percentage of progress.
# @return [Array<Person>] the array of people got via api.
- def get_all(params: {})
- each(params: params).to_a
+ def get_all(params: {}, silent: false)
+ each(params: params, silent: silent).to_a
end
# Gets a person via api.
# @note if the request has `success?` the returned `object.result` gives an object with that `Person`.
# @param doc [String, Hash, Person] data containing an `id` (internal or external) of the target person.