Skip to content

Commit

Permalink
chore: change monetary validator rule
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed May 25, 2020
1 parent 8f8d00d commit 25abaf7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
9 changes: 8 additions & 1 deletion app/controllers/validations/monetary_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ def error_object
attr_accessor :query_key

def query_key_format_must_be_correct
if query_key.blank? || !(query_key.split("-") - ::MonetaryData::VALID_INDICATORS).empty?
if query_key.blank? || !query_key_valid?
errors.add(:query_key, "indicator name is invalid")
end
end

def query_key_valid?
query_keys = query_key.split("-")
extra_keys = query_keys - ::MonetaryData::VALID_INDICATORS
binding.pry
extra_keys.blank? || extra_keys.size == 1 && extra_keys.first =~ /^nominal_apc(\d+)$/
end
end
end
2 changes: 1 addition & 1 deletion app/models/monetary_data.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MonetaryData
VALID_INDICATORS = %w(nominal_apc inflation_rate).freeze
VALID_INDICATORS = %w(nominal_apc nominal_inflation_rate real_inflation_rate).freeze
INITIAL_SUPPLY = 33.6
SECONDARY_SUPPLY_PER_YEAR = 1.344

Expand Down
20 changes: 12 additions & 8 deletions app/serializers/monetary_data_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ class MonetaryDataSerializer

attribute :nominal_apc, if: Proc.new { |_record, params|
params && params[:indicator].include?("nominal_apc")
} do |object, params|
if rs = params[:indicator].match(/(\d+)/)
object.nominal_apc(rs[1].to_i).map(&:to_s)
end
end

attribute :nominal_inflation_rate, if: Proc.new { |_record, params|
params && params[:indicator].include?("nominal_inflation_rate")
} do |object|
object.nominal_apc.map(&:to_s)
object.nominal_inflation_rate.map(&:to_s)
end

attribute :inflation_rate, if: Proc.new { |_record, params|
params && params[:indicator].include?("inflation_rate")
attribute :real_inflation_rate, if: Proc.new { |_record, params|
params && params[:indicator].include?("real_inflation_rate")
} do |object|
{
nominal_apc: object.nominal_apc(50).map(&:to_s),
nominal_inflation_rate: object.nominal_inflation_rate.map(&:to_s),
real_inflation_rate: object.real_inflation_rate.map(&:to_s)
}
object.real_inflation_rate.map(&:to_s)
end
end
8 changes: 4 additions & 4 deletions test/controllers/api/v1/monetary_data_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class MonetaryDataControllerTest < ActionDispatch::IntegrationTest
expected_nominal_inflation_rate = [16.5, 16.27620221, 16.05839416, 15.84633853, 15.63981042, 15.43859649, 15.24249422, 15.05131128, 14.86486486, 14.68298109, 14.5054945, 14.33224755, 14.16309012, 13.9978791, 13.83647798, 13.67875647, 13.52459016, 13.37386018, 13.2264529, 13.08225966, 12.94117647, 12.80310378, 12.66794625, 12.53561253, 12.40601503, 12.27906976, 12.15469613, 12.03281677, 11.9133574, 11.79624664, 11.68141592, 11.56879929, 11.45833333, 11.349957, 11.24361158, 11.1392405, 11.03678929, 10.93620546, 10.83743842, 10.74043938, 10.64516129, 10.55155875, 10.45958795, 10.36920659, 10.28037383, 10.19305019, 10.10719754, 10.02277904, 6.19413319, 6.16232464, 6.13084112, 6.09967765, 6.0688294, 6.0382916, 6.00805959, 5.97812879, 5.94849474, 5.91915303, 5.89009936, 5.86132952, 5.83283935, 5.80462482, 5.77668193, 5.74900677, 5.72159553, 5.69444444, 5.66754982, 5.64090804, 5.61451557, 5.58836892, 5.56246466, 5.53679945, 5.51137, 5.48617305, 5.46120546, 5.43646408, 5.41194588, 5.38764783, 5.36356699, 5.33970045, 5.31604538, 5.29259896, 5.26935846, 5.24632117, 5.22348444, 5.20084566, 5.17840227, 5.15615175, 5.13409162, 5.11221945, 5.09053285, 5.06902946, 5.04770698, 5.02656313, 5.00559568, 4.98480243, 3.45506894, 3.44514959, 3.43528702, 3.42548076, 3.41573033, 3.40603525, 3.39639505, 3.38680926, 3.37727744, 3.36779911, 3.35837384, 3.34900117, 3.33968067, 3.33041191, 3.32119446, 3.31202789, 3.30291177, 3.2938457, 3.28482927, 3.27586206, 3.26694368, 3.25807373, 3.24925181, 3.24047754, 3.23175053, 3.22307039, 3.21443676, 3.20584926, 3.19730753, 3.18881118, 3.18035988, 3.17195325, 3.16359095, 3.15527262, 3.14699792, 3.13876651, 3.13057805, 3.12243221, 3.11432864, 3.10626702, 3.09824704, 3.09026836, 3.08233067, 3.07443365, 3.066577, 3.05876039, 3.05098354, 3.04324612, 2.37117293, 2.36649678, 2.36183904, 2.35719961, 2.35257836, 2.3479752, 2.34339001, 2.3388227, 2.33427316, 2.32974128, 2.32522697, 2.32073011, 2.31625062, 2.31178838, 2.30734331, 2.3029153, 2.29850425, 2.29411006, 2.28973265, 2.2853719, 2.28102774, 2.27670006, 2.27238877, 2.26809378, 2.26381499, 2.25955232, 2.25530566, 2.25107495, 2.24686007, 2.24266095, 2.23847749, 2.23430962, 2.23015723, 2.22602025, 2.22189859, 2.21779217, 2.2137009, 2.20962469, 2.20556347, 2.20151715, 2.19748564, 2.19346888, 2.18946677, 2.18547925, 2.18150621, 2.1775476, 2.17360333, 2.16967333, 1.86205008, 1.85916519, 1.85628924, 1.85342216, 1.85056393, 1.8477145, 1.84487384, 1.84204189, 1.83921863, 1.836404, 1.83359798, 1.83080052, 1.82801158, 1.82523113, 1.82245912, 1.81969552, 1.81694029, 1.81419339, 1.81145478, 1.80872443, 1.8060023, 1.80328834, 1.80058254, 1.79788484, 1.79519521, 1.79251361, 1.78984002, 1.78717439, 1.78451669, 1.78186688, 1.77922493, 1.7765908, 1.77396446, 1.77134587, 1.768735, 1.76613182, 1.76353629, 1.76094838, 1.75836805, 1.75579527, 1.75323001, 1.75067223, 1.74812191, 1.745579, 1.74304349, 1.74051532, 1.73799449, 1.73548094, 1.59157954, 1.5894714, 1.58736883, 1.58527182, 1.58318035, 1.58109438, 1.57901391, 1.5769389, 1.57486934, 1.5728052, 1.57074647, 1.56869312, 1.56664514, 1.56460249, 1.56256516, 1.56053313, 1.55850638, 1.55648489, 1.55446863, 1.55245759, 1.55045175, 1.54845108, 1.54645558, 1.5444652, 1.54247995, 1.54049979, 1.53852471, 1.53655469, 1.5345897, 1.53262974, 1.53067477, 1.52872479, 1.52677977, 1.52483969, 1.52290453, 1.52097428, 1.51904892, 1.51712843, 1.51521279, 1.51330197, 1.51139597, 1.50949477, 1.50759834, 1.50570668, 1.50381975, 1.50193755, 1.50006005, 1.49818724, 1.42983618, 1.42813451, 1.42643689, 1.4247433, 1.42305373, 1.42136816, 1.41968658, 1.41800897, 1.41633533, 1.41466563, 1.41299986, 1.41133801, 1.40968006, 1.40802601, 1.40637583, 1.40472951, 1.40308705, 1.40144842, 1.39981361, 1.39818262, 1.39655542, 1.394932, 1.39331236, 1.39169647, 1.39008432, 1.3884759, 1.38687121, 1.38527021, 1.38367291, 1.38207929, 1.38048934, 1.37890304, 1.37732038, 1.37574135, 1.37416593, 1.37259412, 1.3710259, 1.36946126, 1.36790019, 1.36634267, 1.3647887, 1.36323826, 1.36169133, 1.36014791, 1.35860799, 1.35707155, 1.35553858, 1.35400907, 1.32103529, 1.31958261, 1.31813312, 1.31668681, 1.31524368, 1.3138037, 1.31236687, 1.31093319, 1.30950263, 1.30807519, 1.30665086, 1.30522962, 1.30381148, 1.30239641, 1.30098441, 1.29957548, 1.29816958, 1.29676673, 1.29536691, 1.2939701, 1.29257631, 1.29118551, 1.2897977, 1.28841288, 1.28703102, 1.28565213, 1.28427618, 1.28290318, 1.28153311, 1.28016597, 1.27880173, 1.27744041, 1.27608197, 1.27472643, 1.27337376, 1.27202396, 1.27067701, 1.26933292, 1.26799167, 1.26665325, 1.26531765, 1.26398486, 1.26265488, 1.2613277, 1.2600033, 1.25868168, 1.25736283, 1.25604674, 1.23979731, 1.23851772, 1.23724076, 1.23596644, 1.23469474, 1.23342565, 1.23215917, 1.23089529, 1.229634, 1.22837529, 1.22711915, 1.22586558, 1.22461457, 1.22336611, 1.22212019, 1.22087681, 1.21963596, 1.21839762, 1.2171618, 1.21592848, 1.21469766, 1.21346933, 1.21224348, 1.2110201, 1.20979919, 1.20858074, 1.20736475, 1.20615119, 1.20494007, 1.20373139, 1.20252512, 1.20132127, 1.20011983, 1.19892079, 1.19772415, 1.19652989, 1.195338, 1.19414849, 1.19296135, 1.19177656, 1.19059413, 1.18941404, 1.18823628, 1.18706086, 1.18588776, 1.18471697, 1.1835485, 1.18238233, 1.17410278, 1.17295514, 1.17180974, 1.17066657, 1.16952564, 1.16838692, 1.16725042, 1.16611613, 1.16498404, 1.16385415, 1.16272644, 1.16160092, 1.16047758, 1.15935641, 1.1582374, 1.15712055, 1.15600585, 1.1548933, 1.15378288, 1.1526746, 1.15156845, 1.15046442, 1.1493625, 1.14826269, 1.14716499, 1.14606938, 1.14497586, 1.14388443, 1.14279507, 1.14170779, 1.14062258, 1.13953942, 1.13845832, 1.13737928, 1.13630227, 1.1352273, 1.13415437, 1.13308346, 1.13201457, 1.13094769, 1.12988283, 1.12881997, 1.1277591, 1.12670023, 1.12564334, 1.12458844, 1.12353551, 1.12248455, 1.11803714, 1.11699643, 1.11595767, 1.11492083, 1.11388592, 1.11285292, 1.11182185, 1.11079268, 1.10976541, 1.10874004, 1.10771657, 1.10669498, 1.10567528, 1.10465745, 1.10364149, 1.10262741, 1.10161518, 1.10060481, 1.09959629, 1.09858962, 1.09758479, 1.0965818, 1.09558064, 1.0945813, 1.09358379, 1.09258809, 1.09159421, 1.09060213, 1.08961185, 1.08862337, 1.08763668, 1.08665178, 1.08566866, 1.08468732, 1.08370775, 1.08272994, 1.08175391, 1.08077962, 1.0798071, 1.07883632, 1.07786728, 1.07689999, 1.07593442, 1.07497059, 1.07400849, 1.0730481, 1.07208943, 1.07113247, 1.06855068, 1.06760002, 1.06665106, 1.06570378, 1.06475818, 1.06381426, 1.06287202, 1.06193143, 1.06099252, 1.06005526, 1.05911965, 1.0581857, 1.05725339, 1.05632272, 1.05539369, 1.0544663, 1.05354053, 1.05261638, 1.05169386, 1.05077295, 1.04985365, 1.04893596, 1.04801987, 1.04710538, 1.04619248, 1.04528118, 1.04437146, 1.04346333, 1.04255677, 1.04165178, 1.04074837, 1.03984652, 1.03894623, 1.0380475, 1.03715033, 1.0362547, 1.03536062, 1.03446808, 1.03357708, 1.03268761, 1.03179967, 1.03091326, 1.03002837, 1.02914499, 1.02826313, 1.02738278, 1.02650394, 1.0256266, 1.02397079, 1.02309777, 1.02222624, 1.02135619, 1.02048762, 1.01962053, 1.01875491, 1.01789076, 1.01702807, 1.01616685, 1.01530708, 1.01444877, 1.0135919, 1.01273649, 1.01188251, 1.01102997, 1.01017887, 1.0093292, 1.00848096, 1.00763415, 1.00678875, 1.00594477, 1.00510221, 1.00426105].map(&:to_s)
expected_real_inflation_rate = [12.5, 12.33045622, 12.16545012, 12.00480192, 11.84834123, 11.69590644, 11.54734411, 11.40250855, 11.26126126, 11.12347053, 10.98901099, 10.8577633, 10.72961373, 10.60445387, 10.48218028, 10.36269429, 10.24590164, 10.13171226, 10.02004007, 9.91080278, 9.80392157, 9.69932105, 9.59692898, 9.49667616, 9.39849624, 9.30232558, 9.20810313, 9.11577028, 9.02527076, 8.93655049, 8.84955751, 8.76424188, 8.68055556, 8.59845228, 8.51788756, 8.43881856, 8.36120401, 8.28500414, 8.21018063, 8.13669649, 8.06451613, 7.99360512, 7.92393027, 7.85545953, 7.788162, 7.72200771, 7.65696784, 7.59301443, 3.77691048, 3.75751502, 3.73831776, 3.71931564, 3.70050573, 3.68188512, 3.66345097, 3.64520048, 3.62713094, 3.60923966, 3.591524, 3.57398142, 3.55660936, 3.53940538, 3.52236704, 3.50549194, 3.48877777, 3.47222222, 3.45582306, 3.43957807, 3.42348511, 3.40754202, 3.39174675, 3.37609723, 3.36059147, 3.34522747, 3.33000333, 3.31491712, 3.299967, 3.28515111, 3.27046768, 3.2559149, 3.24149109, 3.22719449, 3.21302345, 3.19897633, 3.18505149, 3.17124736, 3.15756236, 3.14399497, 3.13054368, 3.11720699, 3.10398345, 3.09087162, 3.07787011, 3.06497752, 3.05219249, 3.03951368, 1.51538111, 1.51103053, 1.50670483, 1.50240384, 1.49812734, 1.49387511, 1.48964695, 1.48544266, 1.48126203, 1.47710488, 1.47297099, 1.46886016, 1.46477222, 1.46070698, 1.45666424, 1.45264382, 1.44864551, 1.44466916, 1.44071459, 1.43678161, 1.43287003, 1.42897971, 1.42511044, 1.42126208, 1.41743444, 1.41362736, 1.40984067, 1.40607424, 1.40232786, 1.39860139, 1.39489469, 1.39120757, 1.38753989, 1.38389149, 1.38026225, 1.37665198, 1.37306055, 1.36948782, 1.36593362, 1.36239782, 1.35888028, 1.35538086, 1.35189942, 1.34843581, 1.34498992, 1.34156158, 1.33815067, 1.33475707, 0.66605982, 0.66474628, 0.66343792, 0.66213472, 0.66083661, 0.6595436, 0.65825561, 0.65697267, 0.6556947, 0.6544217, 0.65315365, 0.65189048, 0.6506322, 0.64937875, 0.64813013, 0.64688631, 0.64564727, 0.64441294, 0.64318332, 0.64195839, 0.64073812, 0.63952249, 0.63831145, 0.637105, 0.63590309, 0.63470571, 0.63351282, 0.63232442, 0.63114046, 0.62996093, 0.62878581, 0.62761505, 0.62644866, 0.62528659, 0.62412881, 0.62297533, 0.6218261, 0.62068109, 0.61954029, 0.6184037, 0.61727124, 0.61614295, 0.61501875, 0.61389867, 0.61278263, 0.61167067, 0.61056273, 0.60945881, 0.30425654, 0.30378515, 0.30331523, 0.30284676, 0.30237973, 0.30191412, 0.30144997, 0.30098724, 0.30052591, 0.300066, 0.2996075, 0.2991504, 0.29869469, 0.29824037, 0.29778744, 0.29733586, 0.29688567, 0.29643683, 0.29598934, 0.2955432, 0.29509841, 0.29465494, 0.29421283, 0.29377204, 0.29333254, 0.29289438, 0.29245751, 0.29202195, 0.2915877, 0.29115471, 0.29072303, 0.29029262, 0.28986347, 0.2894356, 0.28900898, 0.28858362, 0.28815952, 0.28773667, 0.28731505, 0.28689465, 0.2864755, 0.28605755, 0.28564084, 0.28522533, 0.28481102, 0.28439793, 0.28398602, 0.28357531, 0.14159959, 0.14141205, 0.14122498, 0.14103842, 0.14085234, 0.14066676, 0.14048167, 0.14029705, 0.14011292, 0.13992927, 0.13974613, 0.13956344, 0.13938125, 0.1391995, 0.13901824, 0.13883747, 0.13865715, 0.1384773, 0.13829791, 0.13811899, 0.13794054, 0.13776254, 0.13758501, 0.13740793, 0.13723131, 0.13705513, 0.13687941, 0.13670416, 0.13652932, 0.13635496, 0.13618103, 0.13600754, 0.1358345, 0.1356619, 0.13548971, 0.13531798, 0.13514669, 0.13497584, 0.13480541, 0.1346354, 0.13446582, 0.13429669, 0.13412795, 0.13395967, 0.13379178, 0.13362434, 0.1334573, 0.13329068, 0.06656592, 0.0664867, 0.06640767, 0.06632883, 0.06625017, 0.0661717, 0.06609342, 0.06601531, 0.06593739, 0.06585967, 0.06578211, 0.06570474, 0.06562755, 0.06555055, 0.06547374, 0.06539708, 0.06532062, 0.06524434, 0.06516823, 0.0650923, 0.06501655, 0.06494097, 0.06486556, 0.06479034, 0.06471528, 0.0646404, 0.06456569, 0.06449115, 0.0644168, 0.0643426, 0.0642686, 0.06419474, 0.06412105, 0.06404754, 0.06397419, 0.06390103, 0.06382802, 0.06375518, 0.0636825, 0.06360999, 0.06353765, 0.06346546, 0.06339344, 0.06332158, 0.06324991, 0.06317837, 0.063107, 0.06303579, 0.0314832, 0.03144859, 0.03141404, 0.03137956, 0.03134517, 0.03131086, 0.0312766, 0.03124245, 0.03120835, 0.03117433, 0.03114039, 0.03110652, 0.03107272, 0.031039, 0.03100534, 0.03097176, 0.03093825, 0.03090483, 0.03087147, 0.03083817, 0.03080497, 0.03077182, 0.03073874, 0.03070574, 0.0306728, 0.03063994, 0.03060715, 0.03057442, 0.03054178, 0.0305092, 0.03047668, 0.03044425, 0.03041185, 0.03037957, 0.03034732, 0.03031516, 0.03028304, 0.03025102, 0.03021906, 0.03018717, 0.03015533, 0.03012356, 0.03009186, 0.03006025, 0.03002868, 0.02999718, 0.02996574, 0.02993438, 0.01495172, 0.0149363, 0.01492088, 0.01490553, 0.01489018, 0.01487488, 0.01485961, 0.01484437, 0.01482915, 0.01481398, 0.01479883, 0.01478371, 0.01476863, 0.01475356, 0.01473853, 0.01472355, 0.01470858, 0.01469364, 0.01467874, 0.01466387, 0.01464902, 0.01463421, 0.01461943, 0.01460468, 0.01458994, 0.01457525, 0.0145606, 0.01454595, 0.01453134, 0.01451677, 0.01450222, 0.01448771, 0.01447321, 0.01445876, 0.01444434, 0.01442994, 0.01441555, 0.0144012, 0.01438688, 0.0143726, 0.01435834, 0.01434411, 0.0143299, 0.01431574, 0.01430158, 0.01428746, 0.01427337, 0.01425932, 0.00712267, 0.00711572, 0.00710877, 0.00710183, 0.00709492, 0.00708801, 0.00708111, 0.00707422, 0.00706735, 0.0070605, 0.00705365, 0.00704682, 0.00704002, 0.00703322, 0.00702643, 0.00701965, 0.00701289, 0.00700614, 0.00699941, 0.00699268, 0.00698598, 0.00697928, 0.00697259, 0.00696591, 0.00695927, 0.00695261, 0.00694598, 0.00693936, 0.00693275, 0.00692615, 0.00691958, 0.00691299, 0.00690644, 0.0068999, 0.00689336, 0.00688683, 0.00688034, 0.00687383, 0.00686735, 0.00686087, 0.00685441, 0.00684797, 0.00684153, 0.0068351, 0.0068287, 0.00682229, 0.00681591, 0.00680954, 0.00340159, 0.00339842, 0.00339526, 0.00339211, 0.00338897, 0.00338581, 0.00338268, 0.00337956, 0.00337643, 0.0033733, 0.00337019, 0.00336709, 0.00336398, 0.00336088, 0.00335778, 0.00335471, 0.00335163, 0.00334856, 0.00334549, 0.00334243, 0.00333936, 0.00333632, 0.00333327, 0.00333022, 0.00332719, 0.00332416, 0.00332115, 0.00331813, 0.0033151, 0.0033121, 0.00330909, 0.00330611, 0.00330311, 0.00330012, 0.00329715, 0.00329416, 0.0032912, 0.00328823, 0.00328528, 0.00328233, 0.00327937, 0.00327644, 0.0032735, 0.00327056, 0.00326764, 0.00326471, 0.00326179, 0.00325888, 0.00162799, 0.00162653, 0.0016251, 0.00162365, 0.00162221, 0.00162076, 0.00161934, 0.00161791, 0.00161648, 0.00161504, 0.00161361, 0.00161219, 0.00161078, 0.00160936, 0.00160795, 0.00160654, 0.00160512, 0.00160371, 0.00160232, 0.00160091, 0.0015995, 0.0015981, 0.00159671, 0.00159532, 0.00159393, 0.00159253, 0.00159115, 0.00158977, 0.00158838, 0.001587, 0.00158564, 0.00158426, 0.00158289, 0.00158152, 0.00158016, 0.00157878, 0.00157742, 0.00157606, 0.0015747, 0.00157336, 0.001572, 0.00157065, 0.0015693, 0.00156796, 0.00156661, 0.00156526, 0.00156394, 0.00156259, 0.00078062, 0.00077996, 0.0007793, 0.00077864, 0.00077796, 0.00077731, 0.00077664, 0.000776, 0.00077532, 0.00077468, 0.00077402, 0.00077338, 0.00077272, 0.00077207, 0.00077141, 0.00077075, 0.00077012, 0.00076947, 0.00076882, 0.00076817, 0.00076752, 0.00076688, 0.00076625, 0.0007656].map(&:to_s)

valid_get api_v1_monetary_datum_url("inflation_rate")
valid_get api_v1_monetary_datum_url("nominal_apc50-nominal_inflation_rate-real_inflation_rate")

assert_equal expected_nominal_apc, json.dig("data", "attributes", "inflation_rate", "nominal_apc")
assert_equal expected_nominal_inflation_rate, json.dig("data", "attributes", "inflation_rate", "nominal_inflation_rate")
assert_equal expected_real_inflation_rate, json.dig("data", "attributes", "inflation_rate", "real_inflation_rate")
assert_equal expected_nominal_apc, json.dig("data", "attributes", "nominal_apc")
assert_equal expected_nominal_inflation_rate, json.dig("data", "attributes", "nominal_inflation_rate")
assert_equal expected_real_inflation_rate, json.dig("data", "attributes", "real_inflation_rate")
end

test "should respond with error object when indicator name is invalid" do
Expand Down

0 comments on commit 25abaf7

Please sign in to comment.