jquery autocomplete cities with geonames - avoid duplicates in autocomplete suggestion - jquery-ui

I'm using the jqueryUI autocomplete functionality (http://jqueryui.com/demos/autocomplete/#remote-jsonp) to search surrounding shops of cities in germany and austria. my code is the following:
$.ajax({
url: "http://ws.geonames.org/searchJSON?lang=de&country=DE&country=AT&isNameRequired=true&featureClass=A&featureCode=ADM3&featureCode=ADM4&featureCode=ADM1",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
}...
the problem now is, for austria, i need all the administrative devisions ADM3, ADM1... to get all of the cities. For Germany ADM4 would be enough. But because of the other codes, I get Berlin for example three times, because its ADM3, ADM1 and ADM4.
How can i remove the duplicates of certain cities to autosuggest it only once?

Related

Google Docs API : How to update alignment of the table?

I have a Table in the google docs and want to change its alignment to -0.71 but i do not see any Python API to change table properties. This can be done easily using following UI on google UI (as shown below):
I also tries looking at following requests but could not find it:
updateTableColumnProperties
updateTableCellStyle
For debugging, i created a doc with mentioned alignment and tried dumping JSON of it. But i do not see alignment keyword in the JSON.
Thanks #jescanellas for reply.
I found a hack, this may not be the best solution but works.
1) Update paragraph style and set the indentation, alignment as required. Here the start_idx is the index where table needs to be created.
request = [{
'updateParagraphStyle': {
'paragraphStyle': {
'namedStyleType': 'HEADING_5',
'direction': 'LEFT_TO_RIGHT',
'alignment': 'START',
'indentFirstLine': {
'magnitude': -51.839999999999996,
'unit': 'PT'
},
'indentStart': {
'magnitude': -51.839999999999996,
'unit': 'PT'
},
},
'fields': '*',
'range': {
'startIndex': start_idx,
'endIndex': end_idx
}
}
}]
2) Create the table, it will get created at new indented place.
request = [{
'insertTable': {
'rows': 1,
'columns': 1,
'location': {
'segmentId':'',
'index': start_idx
}
},
}]
It's not currently possible to do so. You can create a Feature Request for the Docs API, and you can also subscribe to this one for Apps Script by clicking on the star next to the Issue number to give more priority to the request and to receive updates.
In case of the second request being implemented, you could call the script from the command line using Clasp.

$.ajax request breaking js: true spec in rspec

I have a spec that passes looking like the following:
scenario "Brand likes a comment", js: true do
visit post_path(question, as: brand)
within("#comment_#{comment.id}") do
click_on "Like"
end
expect(page).to have_text "Like •1"
end
I've recently added a page with chart.js graph and I am loading the data for that graph with an ajax request that looks as so:
$.ajax({
url: "/admin/reporting/weekly_new_users",
type: "get",
dataType: "json",
cache: false,
success: function(response){
var ctx = document.getElementById("weeklyUsers");
new Chart(ctx, {
type: 'line',
data: {
labels: response.weekly_users.map(arr => arr[0]),
datasets: [{
label: '# of New Users',
data: response.weekly_users.map(arr => arr[1]),
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
],
borderColor: [
'rgba(255,99,132,1)',
],
borderWidth: 1
}]
},
});
}
})
};
This ajax request is causing my spec with js:true to fail, with the following error message:
Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
ActionController::RoutingError:
No route matches [GET] "/likes/1"
When I comment out the ajax request the spec passes. Not sure whats causing this, any ideas?
You've indicated you're using capybara-webkit as your driver for JS tests - If that's actually true, then most likely you're not fully polyfilling and transpiling your JS code to be compatible with it. This ends up meaning you have errors (unsupported JS) in the concatenated assets served to your app which prevents some of the JS from being processed. In this case I'm guessing your Like link/button is normally processed by JS (possibly you've set a method on the link for Rails to use - POST, PUT, PATCH etc) but the errors in the JS are preventing the JS handlers from being attached to the link/button.
As for what in your $.ajax is causing issues, one possibility is that you're using arrow functions which are not supported by capybara-webkit (it's roughly equivalent to a 6-7 year old version of Safari). In order to continue using capybara-webkit you need to make sure all of your JS is transpiled and polyfilled to be compatible with really old browsers. A MUCH better solution, although possibly more work, is to swap over to using Selenium with headless Chrome or Firefox for your testing. Not only do you get the ability to test on modern browsers your users are actually using, but also the ability to easily swap between headed and headless when trying to debug issues.

PHP Google Spreadsheets choose sheet to write in

I'm implementing an integration with Google Spreadsheets API and PHP. I'm using the library suggested by google.
I need to have many sheets (pages) in the same file. So far, I found that I can create new pages using something like this:
$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
'requests' => array('addSheet' => array('properties' => array('title' => $title )))));
$result = $service->spreadsheets->batchUpdate(SHEET_ID,$body);
But how can I choose when to write on one sheet(page) or the other? Is there some method that allows me to choose the sheet by it's label?
Hope you can help me.
Rather than specifying a sheet alone, based on the Basic Writing sample, you can write on a specific range on a specific sheet using a PUT method:
PUT https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5?valueInputOption=USER_ENTERED
This spreadsheets.values.update request will write the values on the cells from A1 to D5 on Sheet1. Note that the ValueInputOption query parameter is required and determines if the values written will be parsed (for example, whether or not a string is converted into a date).
The request body would then look like this:
{
"range": "Sheet1!A1:D5",
"majorDimension": "ROWS",
"values": [
["Item", "Cost", "Stocked", "Ship Date"],
["Wheel", "$20.50", "4", "3/1/2016"],
["Door", "$15", "2", "3/15/2016"],
["Engine", "$100", "1", "30/20/2016"],
["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]
],
}

Fusion Tables query for grouped data timing out

Using the guidelines here I have successfully queried a simple fusion table for some basic data with the following code:
google.load('visualization', '1', { packages: ['corechart'] });
function drawVisualization() {
google.visualization.drawChart({
containerId: 'visualization',
dataSourceUrl: 'http://www.google.com/fusiontables/gvizdata?tq=',
query: 'SELECT sector, revenue FROM 2961086',
chartType: 'LineChart',
options: {
title: 'Net Revenue by Sector',
vAxis: {
title: 'Revenue'
},
hAxis: {
title: 'Sector'
}
}
});
}
google.setOnLoadCallback(drawVisualization);
A problem arises when I attempt to aggregate the data by sector. I've tried the following
query: 'SELECT sector, revenue FROM 2961086 GROUP BY sector'
with no luck, the request eventually times out.
I threw together two pages demonstrating the issue.
I've also tried queries with various other parameters that work with no trouble whatsoever. Am I missing something?
I'm not a google.visualization user, just a fusion-table user but I would guess that you need an aggregate function in your query: SELECT sector, sum(revenue) from ... GROUP BY sector
Eric

Rails 3 - Creating a JSON response to display Search Results

I'm working to have Rails 3 respond with a JSON request which will then let the app output the search results with the jQuery template plugin...
For the plugin to work, it needs this type of structure:
[
{ title: "The Red Violin", url: "/adadad/123/ads", desc: "blah yada" },
{ title: "Eyes Wide Shut", url: "/adadad/123/ads", desc: "blah yada" },
{ title: "The Inheritance", url: "/adadad/123/ads", desc: "blah yada" }
]
In my Rails 3 controller, I'm getting the search results which come back as #searchresults, which contain either 0 , 1 , or more objects from the class searched.
My question is how to convert that to the above structure (JSON)...
Thank you!
Update
Forgot to mention. The front-end search page will need to work for multiple models which have different db columns. That's why I'd like to learn how to convert that to the above to normalize the results, and send back to the user.
I am not really sure what is the problem here, since you can always call ".to_json" on every instance or collection of instances or hash, etc.
You can use .select to limit the number of fields you need, ie:
Object.select(:title, :url, :desc).to_json
I am guessing that the #searchresults is ActiveRecord::Relation, so you probably can use:
#searchresults.select(:title, :url, :desc).to_json

Resources