Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
added to_big_decimal to xml_query_front
Browse files Browse the repository at this point in the history
  • Loading branch information
mynameisrufus committed Sep 6, 2011
1 parent 081d728 commit 81a774f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/handsoap.rb
Expand Up @@ -2,3 +2,4 @@
require 'handsoap/xml_mason'
require 'handsoap/xml_query_front'
require 'handsoap/service'
require 'bigdecimal'
11 changes: 11 additions & 0 deletions lib/handsoap/xml_query_front.rb
Expand Up @@ -86,6 +86,9 @@ def to_boolean
def to_date
self.first.to_date if self.any?
end
def to_big_decimal(decimal_places = 2)
self.first.to_big_decimal(decimal_places) if self.any?
end
def to_s
self.first.to_s if self.any?
end
Expand Down Expand Up @@ -159,6 +162,14 @@ def to_date
return if t.nil?
Time.iso8601(t)
end
# Returns the value of the element as an instance of BigDecimal
#
# See +to_s+
def to_big_decimal(decimal_places = 2)
t = self.to_s
return if t.nil?
BigDecimal.new t, decimal_places
end
# Returns the inner text content of this element, or the value (if it's an attr or textnode).
#
# The output is a UTF-8 encoded string, without xml-entities.
Expand Down
6 changes: 4 additions & 2 deletions tests/account_test.rb
Expand Up @@ -56,7 +56,8 @@ def parse_account(node)
:blacklisted => (node/"@blacklisted").to_boolean,
:application_id => (node/"@application-id").to_i,
:amount_used => (node/"@amount-used").to_i,
:account_id => (node/"@account-id").to_i
:account_id => (node/"@account-id").to_i,
:credit => (node/"@credit").to_big_decimal
end
end

Expand All @@ -71,6 +72,7 @@ def initialize(values = {})
@buy_attempts = values[:buy_attempts]
@blacklisted = values[:blacklisted] || false
@amount_used = values[:amount_used]
@credit = values[:credit]
end
def blacklisted?
!! @blacklisted
Expand All @@ -91,7 +93,7 @@ def setup
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:GetAccountByIdResponse xmlns:ns1="http://schema.example.org/AccountService">
<account msisdn="12345678" account-id="1" created="2009-08-03T11:28:26+02:00" buy-attempts="42" blacklisted="true" application-id="1" amount-used="123456"/>
<account msisdn="12345678" account-id="1" created="2009-08-03T11:28:26+02:00" buy-attempts="42" blacklisted="true" application-id="1" amount-used="123456" credit="24.95"/>
</ns1:GetAccountByIdResponse>
</soap:Body>
</soap:Envelope>'
Expand Down

0 comments on commit 81a774f

Please sign in to comment.