Class: Moip::Customer

Inherits:
Model
  • Object
show all
Includes:
HTTParty, Header
Defined in:
lib/moip/models/customer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Header

#auth, #base_url, #default_header

Methods inherited from Model

build, #set_parameters, #to_json, #validate_response

Instance Attribute Details

#addressObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def address
  @address
end

#billing_infoObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def billing_info
  @billing_info
end

#birthdate_dayObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def birthdate_day
  @birthdate_day
end

#birthdate_monthObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def birthdate_month
  @birthdate_month
end

#birthdate_yearObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def birthdate_year
  @birthdate_year
end

#codeObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def code
  @code
end

#cpfObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def cpf
  @cpf
end

#creation_dateObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def creation_date
  @creation_date
end

#creation_timeObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def creation_time
  @creation_time
end

#customersObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def customers
  @customers
end

#emailObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def email
  @email
end

#fullnameObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def fullname
  @fullname
end

#phone_area_codeObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def phone_area_code
  @phone_area_code
end

#phone_numberObject



7
8
9
# File 'lib/moip/models/customer.rb', line 7

def phone_number
  @phone_number
end

Instance Method Details

#attributesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/moip/models/customer.rb', line 18

def attributes
	{
    "code" => code,
    "email" => email,
    "fullname" => fullname,
    "cpf" => cpf,
    "phone_area_code" => phone_area_code,
    "phone_number" => phone_number,
    "birthdate_day" => birthdate_day,
    "birthdate_month" => birthdate_month,
    "birthdate_year" => birthdate_year,
    "address" => address,
    "billing_info" => billing_info
	}
end

#createObject

metodo que envia as informações para a API do moip e cria um novo cliente see moiplabs.github.io/assinaturas-docs/api.html#criar_cliente



104
105
106
107
108
109
110
111
# File 'lib/moip/models/customer.rb', line 104

def create
	if self.valid?
		response = self.class.post(base_url(:customers, :params => "new_vault=true"), default_header(self.to_json)).parsed_response
		self.validate_response response
	else
		false
	end
end

#find(code) ⇒ Object



127
128
129
130
# File 'lib/moip/models/customer.rb', line 127

def find code
	response = self.class.get(base_url(:customers, :code => code), default_header).parsed_response
	self.set_parameters response unless response.nil?
end

#loadObject



97
98
99
100
# File 'lib/moip/models/customer.rb', line 97

def load
	list = self.class.get(base_url(:customers), default_header).parsed_response
	self.costumers = list["costumers"]
end

#update_billing_info(billing_info) ⇒ Object

metodo que envia as informações para a API do moip e atualiza os dados do cartão see moiplabs.github.io/assinaturas-docs/api.html#atualizar_cartao



116
117
118
119
120
121
122
123
124
# File 'lib/moip/models/customer.rb', line 116

def update_billing_info billing_info
	if billing_info.valid?
		response = self.class.put(base_url(:customers, :code => self.code, :status => "billing_infos"), default_header(billing_info.build_update.to_json)).parsed_response
		puts response
		self.validate_response response
	else
		false
	end
end

#validates_format_of_emailObject



78
79
80
81
82
83
84
# File 'lib/moip/models/customer.rb', line 78

def validates_format_of_email
	if !self.email.nil? and self.email.match /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
		true
	else
		self.errors.add :email, I18n.t("moip.errors.invalid_format") and return
	end
end

#validates_presence_of_addressObject



58
59
60
61
62
63
64
65
66
# File 'lib/moip/models/customer.rb', line 58

def validates_presence_of_address
	self.errors.add :address, I18n.t("moip.errors.presence_of_address") and return if @address.nil?

	if @address.valid?
		true
	else
		self.errors.add @address.errors.first[0], @address.errors.first[1]
	end
end

#validates_presence_of_billing_infoObject



68
69
70
71
72
73
74
75
76
# File 'lib/moip/models/customer.rb', line 68

def validates_presence_of_billing_info
	self.errors.add :billing_info, I18n.t("moip.errors.presence_of_billing_info") and return if @billing_info.nil?

	if @billing_info.valid?
		true
	else
		self.errors.add @billing_info.errors.first[0], @billing_info.errors.first[1]
	end
end