Class: UpiLink::UriBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/upi_link/uri_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pa:, pn:, am: nil, tn: nil, tr: nil, cu: "INR", mc: nil) ⇒ UriBuilder

Returns a new instance of UriBuilder.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/upi_link/uri_builder.rb', line 7

def initialize(pa:, pn:, am: nil, tn: nil, tr: nil, cu: "INR", mc: nil)
  VpaValidator.validate!(pa)
  raise ArgumentError, "pn (payee name) is required" if pn.nil? || pn.strip.empty?

  @pa = pa
  @pn = pn
  @am = format_amount(am)
  @tn = tn
  @tr = tr
  @cu = cu
  @mc = mc
end

Instance Attribute Details

#amObject (readonly)

Returns the value of attribute am.



5
6
7
# File 'lib/upi_link/uri_builder.rb', line 5

def am
  @am
end

#cuObject (readonly)

Returns the value of attribute cu.



5
6
7
# File 'lib/upi_link/uri_builder.rb', line 5

def cu
  @cu
end

#mcObject (readonly)

Returns the value of attribute mc.



5
6
7
# File 'lib/upi_link/uri_builder.rb', line 5

def mc
  @mc
end

#paObject (readonly)

Returns the value of attribute pa.



5
6
7
# File 'lib/upi_link/uri_builder.rb', line 5

def pa
  @pa
end

#pnObject (readonly)

Returns the value of attribute pn.



5
6
7
# File 'lib/upi_link/uri_builder.rb', line 5

def pn
  @pn
end

#tnObject (readonly)

Returns the value of attribute tn.



5
6
7
# File 'lib/upi_link/uri_builder.rb', line 5

def tn
  @tn
end

#trObject (readonly)

Returns the value of attribute tr.



5
6
7
# File 'lib/upi_link/uri_builder.rb', line 5

def tr
  @tr
end

Instance Method Details

#to_sObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/upi_link/uri_builder.rb', line 20

def to_s
  params = { pa: pa, pn: pn, cu: cu }
  params[:am] = am if am
  params[:tn] = tn if tn && !tn.strip.empty?
  params[:tr] = tr if tr && !tr.strip.empty?
  params[:mc] = mc if mc && !mc.strip.empty?

  query = params.map { |k, v| "#{k}=#{URI.encode_www_form_component(v)}" }.join("&")
  "upi://pay?#{query}"
end