Replace spaces with %20 in groovy - grails

I am pushing some variables to a httpGet statement and it is failing on any spaces, how can I replace the spaces in the variable with %20 prior to running the GET?
I am trying to encode "felton, ca" which is in the variable from with def fromFormatted = URLEncoder.encode(from, "UTF-8")
and when I run the code I get the error
groovy.lang.MissingMethodException: No signature of method: static java.net.URLEncoder.encode() is applicable for argument types: (org.codehaus.groovy.grails.web.json.JSONArray, java.lang.String) values: [[felton, ca], UTF-8]
original input field
section("Departing From:"){
input "from", "text", title: "Address?"
Also tried def fromFormatted = from.encodeAsURL()
with the result of
%5B%22felton%22%2C%22ca%22%5D
apparently since the input text contained a , then it automatically treated that input text as an array. Any thoughts on keeping this from happening, or converting it to a string and maintaining the comma?
I resolved the array by using def fromStr = from.join(",")
unforunately join creates a json string that adds double quotes
%22felton%22%2C%22ca%22
Is there another method for joining the array that would maintain the comma without adding quotes?

Related

Remove all indents and spaces from JSON string except inside its value in Ruby

My problematic string is like this:
'{\n"test":"AAAA",\n"test2":"BBB\n\n\nBBB"\n}'
I want to parse it as JSON object(Hash) by JSON.parse(jsonstring)
The expecting result is:
{ "test": "AAAA", "test2": "BBB\nB"}
However, I get the error:
JSON::ParserError: 809
I happend to know that indentaion code in jsonstring be escaped,
so I tried this:
escaped_jsonstring = '{\n"test":"AAAA",\n"test2":"BBB\n\n\nBBB"\n}'.gsub(/\R/, '\\n')
JSON.parse(escaped_jsonstring)
I still have JSON::ParserError.
Indentations outside the key or value may cause this error.
How can I remove \n(or \r any indentation code) only outside the key or value in Ruby?
which means,
'{\n"test":"AAAA",\n"test2":"BBB\n\n\nBBB"\n}'
↓
'{"test":"AAAA","test2":"BBB\n\n\nBBB"}'
try this
'{\n"test":"AAAA",\n"test2":"BBB\n\n\nBBB"\n}'.gsub(/\B(\\n)+/, "")
\n" is considered inside boundary (so i use \B), meanwhile "\n is considered outside boundary (\b), (\\n)+ to fix case '...,\n\n\n"test2":...
update
turn out \s\n also be considered an inside boundary ... iam not sure there's other cases ...
for now, the updated version
'{\n"test":"AAAA",\n"test2":"BBB \n\n\n BBB"\n}'
.gsub(/([{,\"]\s*)\B(\\n)+/) { $1 }
better way
i found another way to solve your problem, also using regexp, now i will scan through the input text (valid or invalid json) then filter follow the pair pattern "<key>":"<value>" and don't care anything else outside those pairs, finally output the hash
def format(json)
matches = json.scan(/\"(?<key>[^\"]+)\":\"(?<val>[^\"]+)\",*/)
matches&.to_h
end
format('{\n "test\n parser":"AA\nAA", \n\n"test2":"BBB ? ;\n\n\n BBB" \n}')
# {"test\n parser"=>"AA\nAA", "test2"=>"BBB ? ;\n\n\n BBB"}

How do you get strip RTF formatting and get actual string value using DXL in DOORS?

I am trying to get the values in "ID" column of DOORS and I am currently doing this
string ostr=richtext_identifier(o)
When I try to print ostr, in some modules I get just the ID(which is what I want). But in other modules I will get values like "{\rtf1\ansi\ansicpg1256\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\froman\fcharset0 Times New Roman;}} {*\generator Riched20 10.0.17134}\viewkind4\uc1 \pard\f0\fs20\lang1033 SS_\f1\fs24 100\par } " This is the RTF value and I am wondering what the best way is to strip this formatting and get just the value.
Perhaps there is another way to go about this that I am not thinking of as well. Any help would be appreciated.
So the ID column of DOORS is actually a composite- DOORS builds it out of the Module level attribute 'Prefix' and the Object level attribute 'Absolute Number'.
If you wish to grab this value in the future, I would do the following (using your variables)
string ostr = ( module ( o ) )."Prefix" o."Absolute Number" ""
This is opposed to the following, which (despite seeming to be a valid attribute in the insert column dialog) WILL NOT WORK.
string ostr = o."Object Identifier" ""
Hope this helps!
Comment response: You should not need the module name for the code to work. I tested the following successfully on DOORS 9.6.1.10:
Object o = current
string ostr = ( module ( o ) )."Prefix" o."Absolute Number" ""
print ostr
Another solution is to use the identifier function, which takes an Object as input parameter, and returns the identifier as a plain (not RTF) string:
Declaration
string identifier(Object o)
Operation
Returns the identifier, which is a combination of absolute number and module prefix, of object o as a string.
The optimal solution somewhat depends on your underlying requirement for retrieving the object ID.

String contains reserved builtin global variable name issue

Given:
text = "text#$!text" #Note: #$! is normal string not a variable
returns: "texttext"
And
text = 'text#$!text' # with single quote
returns: "text\#$!text"
Question:
How can I just get "text#$!text" as result because it is required to be hardcoded and used as password, so it can't contain backslashes \
The backslash is part of how the string is represented by inspect, like the quotes; neither are part of the actual string itself.
Note that text[0, 5], for example, is "text#".

String is getting enclosed by \ in the params

In my get request, I am sending the parameter like this localhost:3000/home?q="item1 item2"
But in the server params if I observe the q. It changes like this.
"\"item1 item2\""
However I don't want the extra \" in the starting and ending of string, is there any thing I am doing wrong while sending the request?
The scenario is the same even when q="item1+item2"
First of all, there are no \" characters in the string, it is just a way how Rails quotes " strings in logs. In reality, the string has a value of "item1 item2" and the " characters are part of it.
Second, if you don't want the " to be there, you can either just not send it - see #Sudipta Mondal:
localhost:3000/home?q=item1%20item2
or if you need to send it, then remove it afterwards in the controller:
params[:q].to_s[1..-2]
which will remove first and last character, or:
params[:q].gsub /"/, ""
which will remove all occurences of the ".
The value of q parameter in the URL should be: localhost:3000/home?q=item1+item2

namevaluecollection removes "+" characters from querystring

I have the followigurl localhost.dev?q=dyYJDXWoTKjj9Za6Enzg4lB+NHJsrZQehfY1dqbU1fc= and extract the query string as follows:
NameValueCollection query = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);
string str1 = query[0];
If i call query.ToString() it shows the correct characters query string. However, when I access the value from the NameValueCollection 'query[0]' the "+" is replaced by a empty " " i.e. dyYJDXWoTKjj9Za6Enzg4lB NHJsrZQehfY1dqbU1fc=
I've tried specifing different encoding and using the Get method from the namevaluecollection. I've also tried spliting the string, but the "+" is being removed each time. Has anyone got any ideas? Many thanks
You can't use this chars in the url variables, you need use URLEncode and URLDecode of HttpUtility class to convert this into a valid url.
I hope this help you.

Resources