Class: Ecoportal::API::V1::People
- Inherits:
-
Object
- Object
- Ecoportal::API::V1::People
- Extended by:
- Common::BaseClass
- Includes:
- Common::DocHelpers, Enumerable
- Defined in:
- lib/ecoportal/api/v1/people.rb
Direct Known Subclasses
Defined Under Namespace
Classes: JobStatus
Constant Summary collapse
- JOB_TIMEOUT =
240- DELAY_STATUS_CHECK =
5
Instance Attribute Summary collapse
-
#client ⇒ Common::Client
readonly
a
Common::Clientobject that holds the configuration of the api connection.
Instance Method Summary collapse
-
#batch(job_mode: true) {|batch_op| ... } ⇒ Ecoportal::API::Common::Response
Creates a
BatchOperationand yields it to the given bock. -
#create(doc) ⇒ Response
Requests to create a person via api.
-
#delete(doc) ⇒ Response
Requests to completelly remove from an organization an existing person via api.
-
#each(params: {}, silent: false) {|person| ... } ⇒ Object
Iterates all the people of the organization.
-
#get(doc) ⇒ Person
Gets a person via api.
-
#get_all(params: {}, silent: false) ⇒ Array<Person>
Gets all the people via api requests.
-
#initialize(client) ⇒ People
constructor
An instance object ready to make people api requests.
-
#job {|operation| ... } ⇒ Ecoportal::API::Common::Response
The results of the batch job.
-
#new ⇒ Person
Creates a new
Personobject. -
#update(doc) ⇒ Response
Requests an update of a person via api.
-
#upsert(doc) ⇒ Response
Requests to update an existing person or if it does not exist, to create it, via api.
Methods included from Common::BaseClass
Methods included from Common::DocHelpers
Constructor Details
#initialize(client) ⇒ People
19 20 21 |
# File 'lib/ecoportal/api/v1/people.rb', line 19 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Common::Client (readonly)
a Common::Client object that holds the configuration of the api connection.
5 6 7 |
# File 'lib/ecoportal/api/v1/people.rb', line 5 def client @client end |
Instance Method Details
#batch(job_mode: true) {|batch_op| ... } ⇒ Ecoportal::API::Common::Response
Creates a BatchOperation and yields it to the given bock.
129 130 131 132 133 134 135 136 137 |
# File 'lib/ecoportal/api/v1/people.rb', line 129 def batch(job_mode: true, &block) return job(&block) if job_mode operation = Common::BatchOperation.new("/people", person_class, logger: client.logger) yield operation # The batch operation is responsible for logging the output client.post("/people/batch", data: operation.as_json).tap do |response| operation.process_response(response) end end |
#create(doc) ⇒ Response
Requests to create a person via api.
102 103 104 105 |
# File 'lib/ecoportal/api/v1/people.rb', line 102 def create(doc) body = get_body(doc) client.post("/people", data: body) end |
#delete(doc) ⇒ Response
Requests to completelly remove from an organization an existing person via api.
119 120 121 122 |
# File 'lib/ecoportal/api/v1/people.rb', line 119 def delete(doc) id = get_id(doc) client.delete("/people/"+CGI.escape(id)) end |
#each(params: {}, silent: false) {|person| ... } ⇒ Object
- it ignores the key
cursor_id:ofparams:. eachis called byto_a
Iterates all the people of the organization.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ecoportal/api/v1/people.rb', line 33 def each(params: {}, silent: false, &block) return to_enum(:each, params: params, silent: silent) unless block cursor_id = nil; results = 0 puts "\n" unless silent loop do params.update(cursor_id: cursor_id) if cursor_id body = nil; response = nil loop do response = client.get("/people", params: params) body = response && body_data(response.body) break if response.success? puts "Request failed - Status #{response.status}: #{body}" end response = client.get("/people", params: params) #body = response && body_data(response.body) #raise "Request failed - Status #{response.status}: #{body}" unless response.success? 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 body["results"].each do |person| yield person_class.new(person) end break unless (cursor_id = body["cursor_id"]) end self end |
#get(doc) ⇒ Person
if the request has success? the returned object.result gives an object with that Person.
Gets a person via api.
82 83 84 85 86 87 88 |
# File 'lib/ecoportal/api/v1/people.rb', line 82 def get(doc) 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 |
#get_all(params: {}, silent: false) ⇒ Array<Person>
it ignores the key :cursor_id in params:.
Gets all the people via api requests.
74 75 76 |
# File 'lib/ecoportal/api/v1/people.rb', line 74 def get_all(params: {}, silent: false) each(params: params, silent: silent).to_a end |
#job {|operation| ... } ⇒ Ecoportal::API::Common::Response
Returns the results of the batch job.
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ecoportal/api/v1/people.rb', line 140 def job operation = Common::BatchOperation.new("/people", person_class, logger: client.logger) yield operation job_id = create_job(operation) status = wait_for_job_completion(job_id) if status&.complete? job_result(job_id, operation) else msg = "Job `#{job_id}` not complete. Probably timeout after #{JOB_TIMEOUT} seconds. Current status: #{status}" raise API::Errors::TimeOut.new msg end end |
#new ⇒ Person
Creates a new Person object.
156 157 158 |
# File 'lib/ecoportal/api/v1/people.rb', line 156 def new person_class.new end |
#update(doc) ⇒ Response
Requests an update of a person via api.
93 94 95 96 97 |
# File 'lib/ecoportal/api/v1/people.rb', line 93 def update(doc) body = get_body(doc) id = get_id(doc) client.patch("/people/"+CGI.escape(id), data: body) end |
#upsert(doc) ⇒ Response
Requests to update an existing person or if it does not exist, to create it, via api.
110 111 112 113 114 |
# File 'lib/ecoportal/api/v1/people.rb', line 110 def upsert(doc) body = get_body(doc) id = get_id(doc) client.post("/people/"+CGI.escape(id), data: body) end |