0
votes

I am recieving either of these formats from an external service.

#successCase: if success/error this will be thrown with proper status code

{:sellerid=>2200, :status=>"success"}

#failureCase: for unexpected errors am recieving this kind of hash and this is causing errors

{:property=>"instance.seller_mobile", :message=>"does not meet maximum length of 10", :schema=>{:type=>"string", :required=>true, :minLength=>1, :maxLength=>10}, :instance=>"01234ssss56789", :stack=>"instance.seller_mobile does not meet maximum length of 10"}

This is what I do with the response and here is where error happens(I think)

`if response[:status] == 'success'` 
#doing something
elsif response[:status] == 'error'
#doing something
end

If its #successCase: response this works fine. But if its #failureCase: am getting error

TypeError: no implicit conversion of Symbol into Integer

I can see that its raising error here Failure/Error: if parsed_response[:status] == 'success'

Could someone tell me how to fix this?

PS: "I tried in console and it seems to be working right but rails throws error"

1
Do the hash for the failureCase have the :status key? and/or any errorvalue?Sebastian Palma
@SebastiánPalma no..Abhilash
Do you know the Ruby and Rails versions?Sebastian Palma
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux] & Rails 5.0.1Abhilash
This error happens frequently when what you believe to be a hash somehow ends up wrapped in an array. [:status] will then complain that :status can not be converted from Symbol to Integer, the only allowed argument type for Array#[].Michael Kohl

1 Answers

0
votes

I have came across similar issue, you may try call response.stringify_keys after get response, and then use response["status"] == 'success' instead of response[:status] == 'success'

If you can touch the code in your external service. Before response result the you can call to_json, this will convert symbol key to string key.

# in rails console
> response = {status: 'success', sellerid: 2200}
=> {:status=>"success", :sellerid=>2200} #symbol key
> response = {"status" => 'success', "sellerid" => 2200}
=> {"status"=>"success", "sellerid"=>2200}          
> response.to_json
=> "{\"status\":\"success\",\"sellerid\":2200}"

After that just call response["status"] == 'success' in your side.