Class: NationBuilder::People

Inherits:
Object
  • Object
show all
Defined in:
lib/nation_builder/people.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ People

Returns a new instance of People.



5
6
7
# File 'lib/nation_builder/people.rb', line 5

def initialize(client)
  self.client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/nation_builder/people.rb', line 3

def client
  @client
end

Instance Method Details

#create(params) ⇒ Object



25
26
27
28
# File 'lib/nation_builder/people.rb', line 25

def create params
  body = JSON.generate(params)
  JSON.parse(client.post('/api/v1/people', body: body).response.env[:body])
end

#create_or_update(params) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/nation_builder/people.rb', line 35

def create_or_update params
  person = self.match('email' => params['person']['email'])
  if(person)
    new_person = person['person'].merge(params['person'])
    self.update new_person['id'], 'person' => new_person
  else
    self.create params
  end
end

#listObject



21
22
23
# File 'lib/nation_builder/people.rb', line 21

def list
  JSON.parse(client.get('/api/v1/people').response.env[:body])
end

#match(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/nation_builder/people.rb', line 9

def match(params)
  begin 
    JSON.parse(client.get('/api/v1/people/match', params: params).response.env[:body])
  rescue OAuth2::Error => e
    if e.response.parsed['code'] == 'no_matches'
      return nil
    else
      raise e
    end
  end
end

#update(id, params) ⇒ Object



30
31
32
33
# File 'lib/nation_builder/people.rb', line 30

def update id, params
  body = JSON.generate(params)
  JSON.parse(client.put("/api/v1/people/#{id}", body: body).response.env[:body])
end