Class: Ecoportal::API::V1::People

Inherits:
Object
  • Object
show all
Includes:
Common::DocHelpers, Enumerable
Defined in:
lib/ecoportal/api/v1/people.rb

Direct Known Subclasses

Internal::People

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ People

Returns a new instance of People.



10
11
12
# File 'lib/ecoportal/api/v1/people.rb', line 10

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/ecoportal/api/v1/people.rb', line 8

def client
  @client
end

Instance Method Details

#batch {|operation| ... } ⇒ Object

Yields:

  • (operation)


54
55
56
57
58
59
60
# File 'lib/ecoportal/api/v1/people.rb', line 54

def batch
  operation = Common::BatchOperation.new("/people", person_class)
  yield operation
  @client.post("/people/batch", data: operation.as_json).tap do |response|
    operation.process_response(response)
  end
end

#create(doc) ⇒ Object



43
44
45
46
# File 'lib/ecoportal/api/v1/people.rb', line 43

def create(doc)
  body = get_body(doc)
  @client.post("/people", data: body)
end

#delete(doc) ⇒ Object



62
63
64
65
# File 'lib/ecoportal/api/v1/people.rb', line 62

def delete(doc)
  id = get_id(doc)
  @client.delete("/people/"+CGI::escape(id))
end

#each(params: {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ecoportal/api/v1/people.rb', line 14

def each(params: {}, &block)
  page = 1
  loop do
    response = @client.get("/people", params: params.merge(page: page))
    raise "Request failed." unless response.success?
    response.body["results"].each do |person|
      yield person_class.new(person)
    end
    break if page >= response.body["total_pages"]
    page += 1
  end
  self
end

#get(doc) ⇒ Object



28
29
30
31
# File 'lib/ecoportal/api/v1/people.rb', line 28

def get(doc)
  response = @client.get("/people/"+CGI::escape(get_id(doc)))
  Common::WrappedResponse.new(response, person_class)
end

#get_all(params: {}) ⇒ Object



33
34
35
# File 'lib/ecoportal/api/v1/people.rb', line 33

def get_all(params: {})
  each(params).to_a
end

#newObject



67
68
69
# File 'lib/ecoportal/api/v1/people.rb', line 67

def new
  person_class.new
end

#update(doc) ⇒ Object



37
38
39
40
41
# File 'lib/ecoportal/api/v1/people.rb', line 37

def update(doc)
  body = get_body(doc)
  id   = get_id(doc)
  @client.patch("/people/"+CGI::escape(id), data: body)
end

#upsert(doc) ⇒ Object



48
49
50
51
52
# File 'lib/ecoportal/api/v1/people.rb', line 48

def upsert(doc)
  body = get_body(doc)
  id   = get_id(doc)
  @client.post("/people/"+CGI::escape(id), data: body)
end