lib/ecoportal/api/v1/people.rb in ecoportal-api-0.7.5 vs lib/ecoportal/api/v1/people.rb in ecoportal-api-0.8.2

- old
+ new

@@ -35,25 +35,26 @@ cursor_id = nil; results = 0 puts "\n" unless silent loop do params.update(cursor_id: cursor_id) if cursor_id response = client.get("/people", params: params) - raise "Request failed - Status #{response.status}: #{response.body}" unless response.success? + body = body_data(response.body) + raise "Request failed - Status #{response.status}: #{body}" unless response.success? - unless silent || (total = response.body["total_results"]) == 0 - results += response.body["results"].length + unless silent || (total = body["total_results"]) == 0 + results += 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| + body["results"].each do |person| yield person_class.new(person) end - break unless (cursor_id = response.body["cursor_id"]) + break unless (cursor_id = body["cursor_id"]) end self end # Gets all the people via api requests. @@ -68,14 +69,17 @@ 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. - # @return [WrappedResponse] an object with the api response. + # @return [Person] the person with `id` (internal or external) contained in `doc`. def get(doc) - response = client.get("/people/"+CGI.escape(get_id(doc))) - Common::WrappedResponse.new(response, person_class) + id = get_id(doc) + response = client.get("/people/"+CGI.escape(id)) + body = body_data(response.body) + return person_class.new(body) if response.success? + raise "Could not get person #{id} - Error #{reponse.status}: #{body}" end # Requests an update of a person via api. # @param doc [Person, Hash] data that at least contains an `id` (internal or external) of the target person. # @return [Response] an object with the api response. @@ -105,11 +109,11 @@ # Requests to completelly remove from an organization an existing person via api. # @param doc [Person, Hash] data that at least contains an `id` (internal or external) of the target person. # @return [Response] an object with the api response. def delete(doc) id = get_id(doc) - client.delete("/people/"+CGI.escape(id)) + client.delete("/people/"+CGI.escape(id)) end # Creates a `BatchOperation` and yields it to the given bock. # @yield [batch_op] adds multiple api requests for the current batch. # @yieldparam batch_op [BatchOperation] @@ -148,17 +152,17 @@ private JobStatus = Struct.new(:id, :complete?, :errored?, :progress) def job_status(job_id) response = client.get("/people/job/#{CGI.escape(job_id)}/status") - + body = body_data(response.body) raise "Status error" unless response.success? JobStatus.new( - response.body["id"], - response.body["complete"], - response.body["errored"], - response.body["progress"] + body["id"], + body["complete"], + body["errored"], + body["progress"] ) end def job_result(job_id, operation) # The batch operation is responsible for logging the output @@ -184,14 +188,21 @@ def create_job(operation) job_id = nil client.without_response_logging do client.post("/people/job", data: operation.as_json).tap do |response| - job_id = response.body["id"] + job_id = body_data(response.body)["id"] end end job_id end + + # Hook for other api versions to obtain the raw data of a response + # @note this was introduced to allow `v2` to reuse this class + def body_data(body) + body + end + end end end end