Class: Sellsy::Contact

Inherits:
Object
  • Object
show all
Defined in:
lib/sellsy/contact.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#birth_dateObject

Returns the value of attribute birth_date.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def birth_date
  @birth_date
end

#emailObject

Returns the value of attribute email.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def email
  @email
end

#faxObject

Returns the value of attribute fax.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def fax
  @fax
end

#first_nameObject

Returns the value of attribute first_name.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def first_name
  @first_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def id
  @id
end

#last_nameObject

Returns the value of attribute last_name.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def last_name
  @last_name
end

#linked_idObject

Returns the value of attribute linked_id.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def linked_id
  @linked_id
end

#linked_typeObject

Returns the value of attribute linked_type.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def linked_type
  @linked_type
end

#mobileObject

Returns the value of attribute mobile.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def mobile
  @mobile
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def name
  @name
end

#roleObject

Returns the value of attribute role.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def role
  @role
end

#telephoneObject

Returns the value of attribute telephone.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def telephone
  @telephone
end

#third_idsObject

Returns the value of attribute third_ids.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def third_ids
  @third_ids
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def title
  @title
end

#websiteObject

Returns the value of attribute website.



5
6
7
# File 'lib/sellsy/contact.rb', line 5

def website
  @website
end

Class Method Details

.find(people_id) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sellsy/contact.rb', line 36

def self.find(people_id)
  command = {
      'method' => 'Peoples.getOne',
      'params' => {
          'id' => people_id
      }
  }

  response = MultiJson.load(Sellsy::Api.request command)
  contact = Contact.new

  if response['response']
    value = response['response']
    contact.id = value['id']
  end

  contact
end

.find_by_contact(contact_id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sellsy/contact.rb', line 55

def self.find_by_contact(contact_id)
  command = {
      'method' => 'Peoples.getOne',
      'params' => {
          'thirdcontactid' => contact_id
      }
  }

  response = MultiJson.load(Sellsy::Api.request command)
  contact = Contact.new

  if response['response']
    value = response['response']
    contact.id = value['id']
  end

  contact
end

.search(name, b_date = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sellsy/contact.rb', line 74

def self.search(name, b_date = nil)
  contacts = []

  unless name.blank?
    command = {
        'method' => 'Peoples.getList',
        'params' => {
            'search' => {
                'contains' => name,
                'birthdate' => b_date.blank? ? nil : Date.parse(b_date).to_datetime.to_i,
            }
        }
    }

    response = MultiJson.load(Sellsy::Api.request command)

    if response['response']
      response['response']['result'].each do |key, value|
        contact = Contact.new
        contact.id = key
        contact.linked_type = value['linkedtype']
        contact.linked_id = value['linkedid']
        contact.name = value['name']
        contact.first_name = value['forename']
        contact.email = value['email']
        contact.third_ids = ((value['prospectList'] || []) + (value['thirdList'] || []) + (value['supplierList'] || [])).map {|e| e['id']}
        contacts << contact
      end
    end
  end

  contacts
end

Instance Method Details

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sellsy/contact.rb', line 8

def create
  command = {
      'method' => 'Peoples.create',
      'params' => {
          'people' => to_params
      }
  }

  response = MultiJson.load(Sellsy::Api.request command)

  @id = response['response']['id'] if response['response']

  response['status'] == 'success'
end

#get_addressesObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/sellsy/contact.rb', line 124

def get_addresses
  command = {
      'method' => 'Peoples.getAddresses',
      'params' => {
          'id' => id
      }
  }

  response = MultiJson.load(Sellsy::Api.request command)
  client = Contact.new

  if response['response']
    value = response['response']
    client.id = value['id']
  end

  client
end

#to_paramsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sellsy/contact.rb', line 108

def to_params
  {
    'civil' => civil_enum(@title),
    'name' => @last_name || @name,
    'forename' => @first_name,
    'email' => @email,
    'tel' => @telephone,
    'fax' => @fax,
    'mobile' => @mobile,
    'web' => @website,
    'position' => @role,
    'birthdate' => @birth_date.blank? ? '' : Date.parse(@birth_date).to_datetime.to_i,
    'thirdids' => @third_ids.blank? ? nil : @third_ids
  }
end

#updateObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sellsy/contact.rb', line 23

def update
  command = {
      'method' => 'Peoples.update',
      'params' => {
          'id' => @id,
          'people' => to_params
      }
  }

  response = MultiJson.load(Sellsy::Api.request command)
  response['status'] == 'success'
end