Class: CuentaDigital::Coupon

Inherits:
Object
  • Object
show all
Defined in:
lib/cuenta_digital/coupon.rb

Constant Summary collapse

ATTRIBUTES_PRECENSE =
i[id price first_due_date code concept currency].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Coupon

Returns a new instance of Coupon.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cuenta_digital/coupon.rb', line 26

def initialize(params = {})
  @id = params[:id]
  @price = params[:price]
  @site = params[:site]
  @first_due_date = params[:first_due_date]
  @code = params[:code]
  @email_from = params[:email_from].nil? || params[:email_from].empty? ? nil : params[:email_from]
  @email_to = params[:email_to].nil? || params[:email_to].empty? ? nil : params[:email_to]
  @concept = params[:concept]
  @currency = params[:currency] ? params[:currency].to_sym : nil
  @hash = Digest::MD5.hexdigest(params[:key_hash]) if params[:key_hash]
  @price_second_due_date = params[:price_second_due_date]
  @second_due_date = params[:second_due_date]
  @m0 = params[:m0] || 0
  @m2 = params[:m2] || 0
  @m4 = params[:m4] || 1
  @errors = {}
end

Instance Attribute Details

#codeObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def code
  @code
end

#conceptObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def concept
  @concept
end

#currencyObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def currency
  @currency
end

#email_fromObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def email_from
  @email_from
end

#email_toObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def email_to
  @email_to
end

#errorsObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def errors
  @errors
end

#first_due_dateObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def first_due_date
  @first_due_date
end

#hashObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def hash
  @hash
end

#idObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def id
  @id
end

#m0Object

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def m0
  @m0
end

#m2Object

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def m2
  @m2
end

#m4Object

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def m4
  @m4
end

#priceObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def price
  @price
end

#price_second_due_dateObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def price_second_due_date
  @price_second_due_date
end

#response_bodyObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def response_body
  @response_body
end

#response_codeObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def response_code
  @response_code
end

#second_due_dateObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def second_due_date
  @second_due_date
end

#siteObject

Su numero de CuentaDigital



7
8
9
# File 'lib/cuenta_digital/coupon.rb', line 7

def site
  @site
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/cuenta_digital/coupon.rb', line 107

def error?
  response.error?
end

#generate(xml: true, wget: false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cuenta_digital/coupon.rb', line 76

def generate(xml: true, wget: false)
  return false unless valid?

  retries = 0
  begin
    if wget
      @response_code = '200'
      @response_body = `wget -O- "#{uri(xml: xml).to_s}"`
    else
      partial_response = Net::HTTP.get_response(uri(xml: xml))
      @response_code = partial_response.code
      @response_body = partial_response.body
    end
    response
  rescue => e
    if retries < 3
      retries += 1
      retry
    end
    raise e
  end
end

#generated?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/cuenta_digital/coupon.rb', line 103

def generated?
  response.invoice_generated?
end

#paramsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cuenta_digital/coupon.rb', line 45

def params
  attr_params = { id: @id,
                  precio: @price,
                  site: @site,
                  venc: @first_due_date,
                  codigo: @code,
                  concepto: @concept.to_sym,
                  moneda: CuentaDigital::CURRENCIES[@currency],
                  m0: @m0,
                  m2: @m2,
                  m4: @m4,
                  desde: @email_from,
                  hacia: @email_to,
                  precio2: @price_second_due_date,
                  vence2: @second_due_date,
                  hash: @hash }

  attr_params.delete_if { |k, v| v.nil? }
end

#responseObject



99
100
101
# File 'lib/cuenta_digital/coupon.rb', line 99

def response
  @response = CuentaDigital::Response.new(@response_body)
end

#uri(xml: true) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/cuenta_digital/coupon.rb', line 65

def uri(xml: true)
  uri_params = params
  uri_params[:xml] = 1 if xml

  uri_request = CuentaDigital.uri_coupon_generation

  uri_request.query = URI.encode_www_form(uri_params.to_a)

  uri_request
end

#valid?Boolean

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/cuenta_digital/coupon.rb', line 111

def valid?
  validate!
  errors.empty?
end