Class: NationBuilder::People

Inherits:
RemoteController show all
Includes:
Actions::Create, Actions::List, Actions::Show, Actions::Update
Defined in:
lib/nation_builder/people.rb

Instance Attribute Summary

Attributes inherited from RemoteController

#client

Instance Method Summary collapse

Methods included from Actions::Create

#create

Methods included from Actions::Update

#update

Methods included from Actions::List

#list

Methods included from Actions::Show

#show

Methods inherited from RemoteController

#base_path, #base_uri, #initialize

Constructor Details

This class inherits a constructor from NationBuilder::RemoteController

Instance Method Details

#add(params) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nation_builder/people.rb', line 56

def add(params)
  body = JSON.generate(params)
  begin
    JSON.parse(client.put("#{base_path}/add", body: body).response.env[:body])
  rescue OAuth2::Error => e
    if e.response.parsed['code'] == 'validation_failed'
      raise NationBuilder::Exceptions::ValidationError.new(e.response.parsed['message'], e.response.parsed['validation_errors'], e.response.parsed['code'])
    else
      raise e
    end
  end

end

#add_tagging(person_id, tag) ⇒ Object



74
75
76
77
# File 'lib/nation_builder/people.rb', line 74

def add_tagging(person_id, tag)
  body = JSON.generate({'tagging' => {'tag' => tag}})
  JSON.parse(client.put(taggings_path(person_id), body: body).response.env[:body])
end

#controller_nameObject



8
9
10
# File 'lib/nation_builder/people.rb', line 8

def controller_name
  'people'
end

#create_or_update(params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nation_builder/people.rb', line 28

def create_or_update params
  person = self.match('email' => params['person']['email'])
  if person
    new_person = person['person'].merge(params['person'])
    r = self.update new_person['id'], 'person' => new_person
    {is_new: false, response: r}
  else
    r = self.create params
    {is_new: true, response: r}
  end
end

#delete_tagging(person_id, tag) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/nation_builder/people.rb', line 79

def delete_tagging(person_id, tag)
  response = client.delete("#{taggings_path(person_id)}/#{tag}")
  if response.response.env[:status] == 204
    return true
  else
    return response
  end
end

#match(params) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nation_builder/people.rb', line 12

def match(params)
  begin
    JSON.parse(client.get("#{base_path}/match", params: params).response.env[:body])
  rescue OAuth2::Error => e
    if e.response.parsed && e.response.parsed['code'] == 'no_matches'
      return nil
    else
      raise e
    end
  end
end

#match_or_add(params) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/nation_builder/people.rb', line 40

def match_or_add(params)
  person = self.match('email' => params['person']['email'])
  if person
    r = self.add(params)
    {is_new: false, response: r}
  else
    r = self.add(params)
    {is_new: true, response: r}
  end
end

#push(params) ⇒ Object



51
52
53
54
# File 'lib/nation_builder/people.rb', line 51

def push(params)
  body = JSON.generate(params)
  JSON.parse(client.put("#{base_path}/push", body: body).response.env[:body])
end

#search(params) ⇒ Object



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

def search(params)
  JSON.parse(client.get("#{base_path}/search", params: params).response.env[:body])
end

#taggings(person_id) ⇒ Object



70
71
72
# File 'lib/nation_builder/people.rb', line 70

def taggings(person_id)
  JSON.parse(client.get(taggings_path(person_id)).response.env[:body])
end