Detailed changes
@@ -0,0 +1,18 @@
+form!
+title "Buy Account Credit"
+
+field(
+ type: "fixed",
+ label: "Current balance",
+ value: "$#{'%.2f' % @balance}"
+)
+
+field(@payment_methods.to_list_single)
+
+field(
+ datatype: "xs:decimal",
+ range: @range,
+ var: "amount",
+ label: "Amount of credit to buy",
+ required: true
+)
@@ -1,7 +1,5 @@
# frozen_string_literal: true
-require_relative "./xep0122_field"
-
class BuyAccountCreditForm
class AmountValidationError < StandardError
def initialize(amount)
@@ -25,30 +23,13 @@ class BuyAccountCreditForm
RANGE = (15..1000).freeze
- AMOUNT_FIELD =
- XEP0122Field.new(
- "xs:decimal",
- range: RANGE,
- var: "amount",
- label: "Amount of credit to buy",
- required: true
- ).field
-
- def balance
- {
- type: "fixed",
- value: "Current balance: $#{'%.2f' % @balance}"
- }
- end
-
- def add_to_form(form)
- form.type = :form
- form.title = "Buy Account Credit"
- form.fields = [
- balance,
- @payment_methods.to_list_single,
- AMOUNT_FIELD
- ]
+ def form
+ FormTemplate.render(
+ :top_up,
+ balance: @balance,
+ payment_methods: @payment_methods,
+ range: RANGE
+ )
end
def parse(form)
@@ -1,44 +0,0 @@
-# frozen_string_literal: true
-
-require "blather"
-require "nokogiri"
-
-class XEP0122Field
- attr_reader :field
-
- def initialize(type, range: nil, **field)
- @type = type
- @range = range
- @field = Blather::Stanza::X::Field.new(**field)
- @field.add_child(validate)
- end
-
-protected
-
- def validate
- validate = Nokogiri::XML::Node.new("validate", field.document)
- validate.default_namespace = "http://jabber.org/protocol/xdata-validate"
- validate["datatype"] = @type
- validate.add_child(validation)
- validate
- end
-
- def validation
- range_node || Nokogiri::XML::Node.new(
- "basic",
- field.document
- ).tap do |basic|
- basic.default_namespace = "http://jabber.org/protocol/xdata-validate"
- end
- end
-
- def range_node
- return unless @range
-
- Nokogiri::XML::Node.new("range", field.document).tap do |range|
- range.default_namespace = "http://jabber.org/protocol/xdata-validate"
- range["min"] = @range.min.to_s if @range.min
- range["max"] = @range.max.to_s if @range.max
- end
- end
-end
@@ -639,7 +639,7 @@ Command.new(
BuyAccountCreditForm.for(customer).then do |credit_form|
Command.reply { |reply|
reply.allowed_actions = [:complete]
- credit_form.add_to_form(reply.form)
+ reply.command << credit_form.form
}.then do |iq|
CreditCardSale.create(customer, **credit_form.parse(iq.form))
end
@@ -33,23 +33,16 @@ class BuyAccountCreditFormTest < Minitest::Test
end
em :test_for
- def test_balance
- assert_equal(
- { type: "fixed", value: "Current balance: $15.12" },
- @form.balance
- )
- end
-
- def test_add_to_form
- iq_form = Blather::Stanza::X.new
- @form.add_to_form(iq_form)
+ def test_form
+ iq_form = @form.form
assert_equal :form, iq_form.type
assert_equal "Buy Account Credit", iq_form.title
assert_equal(
[
Blather::Stanza::X::Field.new(
type: "fixed",
- value: "Current balance: $15.12"
+ label: "Current balance",
+ value: "$15.12"
),
Blather::Stanza::X::Field.new(
type: "list-single",
@@ -58,7 +51,11 @@ class BuyAccountCreditFormTest < Minitest::Test
required: true,
options: [{ label: "Test 1234", value: "0" }]
),
- BuyAccountCreditForm::AMOUNT_FIELD
+ Blather::Stanza::X::Field.new(
+ var: "amount",
+ label: "Amount of credit to buy",
+ required: true
+ )
],
iq_form.fields
)
@@ -1,61 +0,0 @@
-# frozen_string_literal: true
-
-require "test_helper"
-require "xep0122_field"
-
-class XEP0122FieldTest < Minitest::Test
- def test_field
- field = XEP0122Field.new(
- "xs:decimal",
- range: (0..3),
- var: "v",
- label: "l",
- type: "text-single"
- ).field
-
- example = Nokogiri::XML::Builder.new { |xml|
- xml.field(
- xmlns: "jabber:x:data",
- var: "v",
- type: "text-single",
- label: "l"
- ) do
- xml.validate(
- xmlns: "http://jabber.org/protocol/xdata-validate",
- datatype: "xs:decimal"
- ) do
- xml.range(min: 0, max: 3)
- end
- end
- }
-
- assert_equal example.doc.root.to_xml, field.to_xml
- end
-
- def test_field_no_range
- field = XEP0122Field.new(
- "xs:decimal",
- var: "v",
- label: "l",
- type: "text-single"
- ).field
-
- example = Nokogiri::XML::Builder.new { |xml|
- xml.field(
- xmlns: "jabber:x:data",
- var: "v",
- type: "text-single",
- label: "l"
- ) do
- xml.validate(
- xmlns: "http://jabber.org/protocol/xdata-validate",
- datatype: "xs:decimal"
- ) do
- xml.basic
- end
- end
- }
-
- assert_equal example.doc.root.to_xml, field.to_xml
- end
-end