Class: Colppy::Customer

Inherits:
Resource show all
Defined in:
lib/colppy/resources/customer.rb

Constant Summary

Constants inherited from Resource

Resource::VALID_INVOICE_TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#inspect

Constructor Details

#initialize(client: nil, company: nil, **params) ⇒ Customer

Returns a new instance of Customer.



61
62
63
64
65
66
67
# File 'lib/colppy/resources/customer.rb', line 61

def initialize(client: nil, company: nil, **params)
  @client = client if client && client.is_a?(Colppy::Client)
  @company = company if company && company.is_a?(Colppy::Company)
  @id = params.delete(:idCliente)
  @name = params.delete(:RazonSocial)
  @data = params
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/colppy/resources/customer.rb', line 3

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/colppy/resources/customer.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/colppy/resources/customer.rb', line 3

def name
  @name
end

Class Method Details

.all(client, company) ⇒ Object



6
7
8
# File 'lib/colppy/resources/customer.rb', line 6

def all(client, company)
  list(client, company)
end

.get(client, company, id) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/colppy/resources/customer.rb', line 27

def get(client, company, id)
  response = client.call(
    :customer,
    :read,
    extended_parameters(client, company, { idCliente: id })
  )
  if response[:success]
    new(response[:data].merge(client: client, company: company))
  else
    response
  end
end

.list(client, company, parameters = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/colppy/resources/customer.rb', line 10

def list(client, company, parameters = {})
  call_parameters = base_params.merge(parameters)
  response = client.call(
    :customer,
    :list,
    extended_parameters(client, company, call_parameters)
  )
  if response[:success]
    results = response[:data].map do |params|
      new(params.merge(client: client, company: company))
    end
    parse_list_response(results, response[:total].to_i, call_parameters)
  else
    response
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/colppy/resources/customer.rb', line 99

def []=(key, value)
  key_sym = key.to_sym
  if protected_data_keys.include?(key_sym)
    raise ResourceError.new("You cannot change any of this values: #{protected_data_keys.join(", ")} manually")
  end
  @data[key_sym] = value
end

#new?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/colppy/resources/customer.rb', line 69

def new?
  id.nil? || id.empty?
end

#saveObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/colppy/resources/customer.rb', line 73

def save
  ensure_client_valid! && ensure_company_valid!

  response = @client.call(
    :customer,
    operation,
    save_parameters
  )
  if response[:success]
    response_data = response[:data]
    case operation
    when :create
      @id = response_data[:idCliente]
    when :update
      @data[:NombreFantasia] = response_data[:nombreCliente]
    end
    self
  else
    false
  end
end