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

Inherits:
Object
  • Object
show all
Includes:
Common::DocHelpers
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.



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

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

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

Yields:

  • (operation)


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

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



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

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

#delete(doc) ⇒ Object



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

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

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



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

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(id) ⇒ Object



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

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

#get_all(params: {}) ⇒ Object



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

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

#update(doc) ⇒ Object



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

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

#upsert(doc) ⇒ Object



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

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