Access only one attribute of API in response - Ruby on Rails 4 - ruby-on-rails

I am calling an API that returns an array of JSON objects and I can access return values of the API call
[{"param1":1,"param2":"blah1"},
{"param1":2,"param2":"blah2"},
{"param1":3,"param2":"blah3"}]
I know that i can access each Param1 through iteration or by static indexing like #client[0].param1 #client[1].param1 #client[2].param1 but the thing is , i don't want param2 and i want just param1 . is there any way , to access param1 without iteration or static indexing
so that i could get the below result in response
[{"param1":1},
{"param1":2},
{"param1":3}]
Update
The thing to notice is that i want to filter the result while making
the request (before getting the response when we know the attribute
name)

Try to use delete.
Deletes and returns a key-value pair from hsh whose key is equal to
key. If the key is not found, returns the default value. If the
optional code block is given and the key is not found, pass in the key
and return the result of block.
data = [{"param1":1,"param2":"blah1"},
{"param1":2,"param2":"blah2"},
{"param1":3,"param2":"blah3"}]
data.each {|x| x.delete("param2")}
For more information about delete.
I hope this help you.

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 to check the result from AWSTask

I am calling an API from AWS, whose model isn't with me right now, I want to know what JSON it returns in response to a request so that I can build the model and use it. Is there a way to print the JSON ? I tried printing result property of task but its empty ( may be its obvious), is there a way to do this ?

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?

Backbone.js populating a collection with fetch and data -- how to pass duplicate keys?

I have an endpoint in my application that returns data based on the given query string. The query string can (and often has to) contain duplicate keys, for example /api/entities/related?filter1=val1&filter1=val2&filter2=val3 to identify two filters of type filter1. Flask deals with this nicely, for example when doing request.args.to_dict, I would get {'filter1': ['val1', 'val2'], 'filter2': 'val3'}.
My question is how to achieve the same thing with Backbone when fetching a collection from an endpoint. Currently I might have
this.fetch({data: {'filter1': 'val1', 'filter1': 'val2', 'filter2': 'val3'}});
Since duplicate keys will override each other in javascript objects, my filter1 value will end up being val2. However, when doing
this.fetch({data: {'filter1': ['val1', 'val2'], 'filter2': 'val3'}});
the url ends up being /api/entities/related?filter1%5B%5D=val1&filter1%5B%5D=val2, which at least does use two identical keys but obviously does not work.
Is this an encoding problem or should I approach it differently?
You can pass a querystring with duplicate keys using an Array as a value.
this.fetch({ data: $.param({ filter1: ['value1','value2']}) });
Or, you can always set the URL like this with querystring params.
var MyModel = Backbone.Model.extend({
"url": function() {
return '/' + encodeURI('?filter1=' + val1 + '&filter1=' + val2);
}
});
If you have to change params all the time, you can do something like and work with it directly:
this.model.destroy({
url: '/' + encodeURI('?filter1=' + val1 + '&filter1=' + val2)
});
If you have multiple concrete params (meaning the values changes but the key are static) that changes per fetch (ie: search), you can check out a paging implementation on Gist that might be useful for your situation whatever it may be.
Hope this helps.
Thanks for everyone's input.
Apparently the data you pass does not have to be an object -- I found that the easiest way in my case was to pass a string ('filter1=val1&filter2=val2') to fetch() and that worked perfectly.

What is the difference between put and add method in statehelper in JSF 2

What is the difference between:
Object put(Serializable key, Object value)
void add(Serializable key, Object value)
methods in StateHelper in JSF?
I found the api documentation not that helpful myself and investigated it. Each time add is called it appends another value to a list which is saved under the given key. If you call a get on that key you get back a list. The add method saves you creating that list and watches for corner cases, ex. creating the list when the key is empty.
The put you mentioned works similar to a map-like put. It saves a value under a key.
In contrast, there is an overloaded put with 3 parameters. It creates a map under that key and does a put on that map with another pair of key/value. Again, a get on the key gives you a map.
Thats basically how add and put work. There is some more going on to make partial states working. To sum it up: when you want to add several values under a key you can use add. put with 2 parameters gives you map-like behavior. put with 3 parameters allows you to fill a map under a key.
From the Mojarra API documentation:
void add(java.io.Serializable key, java.lang.Object value)
Store the specified value in a List that is internal to the
StateHelper.
java.lang.Object put(java.io.Serializable key, java.lang.Object value)
Return the previously stored value and store the specified key/value
pair.
I guess MyFaces implemented it in a similar way.

Resources