Skip to content

Commit a3855a2

Browse files
authored
Merge pull request #82 from ynab/gracefully-handle-invalid-flag
Gracefully handle invalid transaction flag values from server
2 parents cafdef7 + 661d17e commit a3855a2

9 files changed

+109
-18
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ynab (3.4.0)
4+
ynab (3.5.0)
55
typhoeus (~> 1.0, >= 1.0.1)
66

77
GEM

config.json

-9
This file was deleted.

lib/ynab/models/account_type.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ def self.build_from_hash(value)
4141
# @param [String] The enum value in the form of the string
4242
# @return [String] The enum value
4343
def build_from_hash(value)
44-
return value if AccountType.all_vars.include?(value)
45-
raise "Invalid ENUM value #{value} for class #AccountType"
44+
if AccountType.all_vars.include?(value)
45+
return value
46+
else
47+
return nil
48+
end
4649
end
4750
end
51+
4852
end

lib/ynab/models/scheduled_transaction_frequency.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ def self.build_from_hash(value)
4141
# @param [String] The enum value in the form of the string
4242
# @return [String] The enum value
4343
def build_from_hash(value)
44-
return value if ScheduledTransactionFrequency.all_vars.include?(value)
45-
raise "Invalid ENUM value #{value} for class #ScheduledTransactionFrequency"
44+
if ScheduledTransactionFrequency.all_vars.include?(value)
45+
return value
46+
else
47+
return nil
48+
end
4649
end
4750
end
51+
4852
end

lib/ynab/models/transaction_cleared_status.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ def self.build_from_hash(value)
3131
# @param [String] The enum value in the form of the string
3232
# @return [String] The enum value
3333
def build_from_hash(value)
34-
return value if TransactionClearedStatus.all_vars.include?(value)
35-
raise "Invalid ENUM value #{value} for class #TransactionClearedStatus"
34+
if TransactionClearedStatus.all_vars.include?(value)
35+
return value
36+
else
37+
return nil
38+
end
3639
end
3740
end
41+
3842
end

lib/ynab/models/transaction_flag_color.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ def self.build_from_hash(value)
3535
# @param [String] The enum value in the form of the string
3636
# @return [String] The enum value
3737
def build_from_hash(value)
38-
return value if TransactionFlagColor.all_vars.include?(value)
39-
raise "Invalid ENUM value #{value} for class #TransactionFlagColor"
38+
if TransactionFlagColor.all_vars.include?(value)
39+
return value
40+
else
41+
return nil
42+
end
4043
end
4144
end
45+
4246
end

spec/api/transactions_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@
4444
expect(response.data.transactions.length).to be 2
4545
end
4646
end
47+
48+
it 'gracefully handles invalid flag_color values' do
49+
# transactions_invalid_flags contains transactions with invalid/unsupported flag_color values
50+
VCR.use_cassette("transactions_invalid_flags") do
51+
response = instance.get_transactions(budget_id)
52+
expect(client.last_request.response.options[:code]).to be 200
53+
expect(response.data.transactions.length).to be 2
54+
# We expect the flag_color to have been converted to nil for these transactions
55+
expect(response.data.transactions[0].flag_color).to be_nil
56+
expect(response.data.transactions[1].flag_color).to be_nil
57+
end
58+
end
4759
end
4860

4961
describe 'GET /budgets/{budget_id}/category/{category_id}/transactions' do

spec/fixtures/vcr_cassettes/transactions_invalid_flags.yml

+46
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class {{classname}}{{#allowableValues}}{{#enumVars}}
2+
{{{name}}} = {{{value}}}.freeze{{/enumVars}}
3+
4+
{{/allowableValues}}
5+
def self.all_vars
6+
@all_vars ||= [{{#allowableValues}}{{#enumVars}}{{{name}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}].freeze
7+
end
8+
9+
# Builds the enum from string
10+
# @param [String] The enum value in the form of the string
11+
# @return [String] The enum value
12+
def self.build_from_hash(value)
13+
new.build_from_hash(value)
14+
end
15+
16+
# Builds the enum from string
17+
# @param [String] The enum value in the form of the string
18+
# @return [String] The enum value
19+
def build_from_hash(value)
20+
if {{classname}}.all_vars.include?(value)
21+
return value
22+
else
23+
return nil
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)