diff --git a/lib/fortnox/api/types.rb b/lib/fortnox/api/types.rb index ce9c2ef6..6798e9d6 100644 --- a/lib/fortnox/api/types.rb +++ b/lib/fortnox/api/types.rb @@ -38,6 +38,15 @@ module Types AccountNumber = Strict::Int .constrained(gteq: 0, lteq: 9999) .optional + .constructor do |input| + unless input.nil? || input.to_s.empty? + Integer(input) + else + input + end + rescue ArgumentError + input + end ArticleType = Strict::String .constrained(included_in: ArticleTypes.values) diff --git a/spec/fortnox/api/types/account_number_spec.rb b/spec/fortnox/api/types/account_number_spec.rb index f5d6af46..e7d5e84c 100644 --- a/spec/fortnox/api/types/account_number_spec.rb +++ b/spec/fortnox/api/types/account_number_spec.rb @@ -28,4 +28,16 @@ context 'when AccountNumber created with a negative number' do include_examples 'raises ConstraintError', -1 end + + context 'when AccountNumber created with an invalid string' do + include_examples 'raises ConstraintError', 'foo' + end + + context 'when AccountNumber created with valid string' do + subject { klass['1234'] } + + it 'casts it to a number' do + is_expected.to eq 1234 + end + end end