Dynamically update instance variable - ruby-on-rails

I would like to make an API call dynamically. API (Get) result must vary dynamically based on the query string passed.
I have hard coded the API URL without any query string, and supplied the API URL as an instance variable:
#apiRequest = HTTParty.get("http://localhost:1880/api/devices")
#parseApiRequest = JSON.parse(#apiRequest.body)
I want #apiRequest to change dynamically based on user input from a form (I had already developed a form that gets inputs from user's).
Is it possible to change the value of an instance variable dynamically? User's input value must be passed onto that controller as a query string.
Sample image that gets user input:
Getting User Input
The instance variable must be updated dynamically as,
#apiRequest = HTTParty.get("http://localhost:1880/api/devices?device_id=123456")
Kindly suggest.

Simply interpolate the dynamic data into the api request parameter string:
extraParamsOrPath = "assemble this from form inputs"
urlPrefix = "http://localhost:1880/"
#apiRequest = HTTParty.get("#{urlPrefix}/#{extraParamsOrPath}")
And make sure the user input is sanitized ;)

Related

For each in by reference or value

I have the following code:
dim key
for each key in Request.Querystring
'do something
key = sanitized_param(key)
next
My question for you classic-asp connoisseur, does classic-asp, or asp in general, pass the variables as references(memory), or by value? Trying to figure out if I sanitize the key variable and pass it back to itself, is it just "alive" for that loop, or does the new value get passed to the original QueryString?
Request.QueryString retrieves the query string parameters by value from the page headers.
You can only make changes to a query string once its been retrieved via Request.QueryString, but you can't make changes directly to Request.QueryString as it's read-only (If you could make changes you would presumably use Response.QueryString, but this isn't a valid response command).
I'm guessing you're trying to sanitize all your query strings in one go? This isn't really possible or indeed necessary. You would typically sanitize a query string as and when you request it:
Response.Write(sanitized_param(Request.QueryString("myQS")))
Or to assign the query string to a variable first then sanitize it:
Dim myQS
myQS = Request.QueryString("myQS")
myQS = sanitized_param(myQS)
' or
myQS = sanitized_param(Request.QueryString("myQS"))
Once the query string has been assigned to a variable and sanitized you're able to reference that variable as often as you like without having to pass it to your sanitize function again.
Also, your example code doesn't make much sense. The key value in your for each loop is referencing just the names of your query strings and not their values. If Response.QueryString was a valid ASP command you would do:
Response.QueryString(key) = sanitized_param(Request.QueryString(key))
But again, this isn't possible.
EDIT: This solution might be what you're looking for:
Create a dictionary object, call it "QueryString" for example. Loop through all your query strings and add a sanitized version to the dictionary object.
Dim QueryString : Set QueryString = Server.CreateObject("Scripting.Dictionary")
For Each Item In Request.QueryString
QueryString.Add Item,sanitized_param(Request.QueryString(Item))
next
Now, to retrieve a sanitized version of a query string just use:
QueryString.Item("query_string_name")
Or for the original unsanitized version you could still use:
Request.QueryString("query_string_name")
Just like Request.QueryString, the dictionary object is forgiving and won't return an error if you ask for a query string that doesn't exist.
You could also create a function for retrieving sanitized query strings, for example:
Function SanitizedQS(ByVal qsName)
SanitizedQS = sanitized_param(Request.QueryString(qsName))
End Function
And rather than using Request.QueryString("query_string_name") just use SanitizedQS("query_string_name").

How do you get the value of a data attribute within an event-based rule in Adobe Dynamic Tag Manager (DTM)?

I have an event-based rule configured to fire on click of an element with a specific class. On click, I would like to capture the value of a data attribute that exists. The DTM documentation says you can capture attribute values using this syntax:
%this.data-event%
or
%this.id%
Example HTML:
On click of links with the class "test", I would like to store the value of "event name" within an eVar. When I used the above syntax however, the syntax is converted to a string and in the Adobe server call as:
v25:%this.data-event%
What is the best way to dynamically grab the value of an attribute of an HTML element on click within DTM?
DTM documentation says you can do that, but in practice I too have found that it doesn't seem to work as advertised most of the time, and will instead populate it with a literal (un-eval'd) string like that.
So what I do instead is under Conditions > Rule Conditions I create a Custom condition. In the Custom condition, I add the following:
// example to get id
_satellite.setVar('this_id',this.id);
// example to get href
_satellite.setVar('this_href',this.href);
return true;
Basically I create on-the-fly data elements using javascript, and then return true (so the condition doesn't affect the rule from triggering).
Then I use %this_id%, %this_href%, etc. syntax from the data element I created, in the Adobe Analytics section variable fields.
The easist way to capture the values of a data attribute against an eVar or prop on the element clicked using DTM is to set the input as the following:
%this.getAttribute(data-attributename)%
For example, if there was a data attribute on an element of data-social-share-destination='facebook' simply input %this.getAttribute(data-social-share-destination)%. This will then capture the value of 'facebook'
More detail on data attributes can be found at http://www.digitalbalance.com.au/our-blog/event-based-tracking-using-html5-custom-data-attributes/
I found a solution. The best way to grab the value of an attribute on click is to use this syntax:
%this.getAttribute(data-title)%
The key is to not use quotes around the attribute name AND ensure the attribute has a value.
If the attribute is missing, the expression is not replaced by an empty string as one would normally expect from experience in other platforms, but instead will display the raw un-interpolated code.

How to update value in angular ui typeahead directive if no matching option is found

I've an array of objects containing title and salary which is used in typeahead directive.
I display department name and get entire object as value.
If none of the options match, I want user entered string to be converted to object still. Is there any way to update that?
This answer is pretty late, but I would just use ng-blur (to trap the end of the users input) along with a variable bound to typeahead-no-results. Then test if the variable is true in the method bound to ng-blur, and, if so, make an object out of the String supplied by the user and push it to your data source. Simple example found here.

How to access hash value stored in local variable

I'm doing an external API query ussing HTTParty, the resultant of that query is a hash that gets stored in an instance variable in my controller. Without saving it to my database I need to access the contents of the hash to send it as a string to another external app.
Here is my controller HTTParty call
#api_response = HTTParty.get("http://xxxxxxxxx.xx/vehicle/reg/#{#user.reg_number}/xxxxxxxxxxxxxxxxxxxxx")
Here is the response I get that is stored in #api_response:
{"response"=>
{"basic"=>
{"reg"=>"xxx", "make"=>"xxxx", "model"=>"xxxx", "version"=>"xxxxx", "body"=>"xxxxxx", "doors"=>"x", "reg_date"=>"xxxxxx", "engine_cc"=>"xxxxxx", "colour"=>"xxxxx", "fuel"=>"xxxxxx", "transmission"=>"x", "data_type"=>"x", "co2_emissions"=>"xxx"}
}
}
As it is I'm able to display the contents of #api_response in my views, but I need to retrieve the info and pass it on.
You access values in a hash using square brackets surrounding the hash key. For example, to access reg out of that response, you would do:
#api_response["response"]["basic"]["reg"]
Is that all you're looking for, or did you need to do something else with it?

getting label value from form

i have a form created with some textboxes and dropdown lists, they are updated dinamically using jquery, some of the values that i update, i push them in some labels, because i don't want that the user modify them. i need those values but i don't know how to take them and push into an object that i created.
i don't know if i made myself clear. this is and example of my code
Pedidos miDetalle = new Pedidos(); //<= object
miDetalle.codTipoArt = Request.Form["ddTipoArticulo"]; // <= value from dropdown list
miDetalle.clave1 = Request.Form["tbClave1"]; // <= value from textbox
miDetalle.nombreArticulo = Request["#lbNombreTela"]; // <= how i'm trying to get the value
Inside your controller action you could get only values that were part of the POST. And only values of input fields will be sent to the server. So you could use hidden fields to store those values on the client. You cannot store those values in some labels and expect those values to be sent to the server when the form is submitted. So using jQuery instead of setting the values in labels you need to put them in addition in hidden fields with specific names and on the server you will be able to fetch those values using Request["MyHiddenFieldName"] or even better using a view model.

Resources