I have this text on a website I want to scrape:
event: new SEvent({"event_id":"Id","date":"Sat 27 Aug 2016"})
I want to pass this from my controller to my JavaScript file, which is already set up.
I'm having issues parsing the information so that only this is returned:
SEvent({"event_id":"Id","date":"Sat 27 Aug 2016"})
Here's what I tried to no avail:
info = text.to_s.scan(/\"(event)/).uniq
Don't you basically want to remove the "event: new " part of the input string? Maybe I misread your question - if not, this is what you could do:
input = 'event: new SEvent({"event_id":"Id","date":"Sat 27 Aug 2016"})'
input.gsub('event: new ', '')
=> 'SEvent({"event_id":"Id","date":"Sat 27 Aug 2016"})'
or a safer option
input.gsub('event: new SEvent', 'SEvent')`
=> 'SEvent({"event_id":"Id","date":"Sat 27 Aug 2016"})'
You can try with following regex:
/event: new (\w+\([^)]+\))/
info will be an array containing only the matches.
Currently you're matching only the string "event, but to match the entire string you want, you can use something like:
scan(/SEvent\(\{\"event[^\)]*\)/)
That will match the opening identifier string SEvent({"event then everything up to the next closing parentesis. which should capture your entire desired string
SEvent({"event_id":"Id","date":"Sat 27 Aug 2016"})
Also, I wanted to mention the square brackets with the carat [^\)]* means match 0 or more characters EXCEPT closing parenthesis.
Related
I want to access the value of the document_id value in the DocumentUser instance variable and add it to an array. However, while the function ids is defined and I can access the id value with it, there is no function document_ids defined for this class. I could go ahead and define it, but my question is - can I access the document_ids value without doing that, perhaps by somehow using the map function (which confuses me)? I thought I could iterate through the model since it looks like it returns an Array, but no dice. Thanks!
[110] pry(#<#<Class:0x00007f99646b1b60>>)> #current_user.document_users.each { |n| puts n }
#<DocumentUser:0x00007f9957cce118>
=> [#<DocumentUser:0x00007f9957cce118
id: 382,
user_id: 26638,
document_id: 282,
created_at: Wed, 08 May 2019 15:05:42 CDT -05:00,
updated_at: Wed, 08 May 2019 15:05:42 CDT -05:00>]
Yes, it would be this:
#current_user.document_users.map(&:document_id)
which is a shorthand of this:
#current_user.document_users.map { |document_user| document_user.document_id }
Since it's probably ActiveRecord class, it's even better way to achieve this result, using pluck:
#current_user.document_users.pluck(:document_id)
One of my co-workers has written an application in Grails 2.4.4 (I know, it's dated). One problem the app has is that you can enter a date like 2/31/2015 and it will be accepted as valid and will show up in your domain object as 3/3/2015 instead.
Is there any easy way to prevent this from happening using grails? Or do we instead have to rely on client side validation for this particular property?
Thanks.
Assuming that Grails is using DateFormat to parse the date String, the issue is that it's using a lenient Calendar. For example, with lenient set to true (the default), the result is as you described:
import java.text.SimpleDateFormat
def sdf = new SimpleDateFormat('MM/dd/y')
assert sdf.parse('02/31/2015').toString() == 'Tue Mar 03 00:00:00 EST 2015'
But, if you change it to false, you'll get an exception for the same date:
sdf.lenient = false
try {
sdf.parse('02/31/2015').toString() == 'Tue Mar 03 00:00:00 EST 2015'
} catch (java.text.ParseException ex) {
// java.text.ParseException: Unparseable date: "02/31/2015"
println 'Oops'
}
So to handle this validation in your controller, you can
Create a command object.
Have the command object accept the date as a String rather than a Date, and perform the date validation.
Modify the controller action to use the command object instead of params. This may require modifying the GSP code as well.
I am using Googlespreadsheet API, it hangs for ever on this method:
spreadSheetEntry.getWorksheets();
It works before 12:00 pm, July 29 EDT, failed sometime around 13:00 pm
Code:
String feedUrlStr = feedUrl.toString() + "?title-exact=false&title=" + URLEncoder.encode(query);
feedUrl = new URL(feedUrlStr);
this.SpreadSheetFeed = this.service.getFeed(feedUrl, SpreadsheetFeed.class);
this.SpreadSheets = this.SpreadSheetFeed.getEntries(); //it works here,can find spreadsheet.
it hangs when trying to get worksheets.
this.SpreadSheets.get(sheetindex).getWorksheets();//hangs on the function getWorksheets();
I'm getting the same thing when hitting the https://spreadsheets.google.com/feeds/worksheets/key/private/full URL directly.
It is still documented here: https://developers.google.com/google-apps/spreadsheets/?hl=en#sheets_api_urls_visibilities_and_projections - didn't see any sings that it would be deprecated...
I have a form on which I set a start Date and a finish Date for a entity.
On the Web Api side, before saving the date to the database,I set the start date: 2013-09-25 00:00:00.000 and the the end date as 2013-09-26 23:59:59.000.
var vote = (VotingSet)Entity;
vote.Start = new DateTime(vote.Start.Year, vote.Start.Month, vote.Start.Day, 0, 0, 0, 0);
vote.End = new DateTime(vote.End.Year, vote.End.Month, vote.End.Day, 23, 59, 58);
This is from the JSON that is send to the rest service looks like this:
Start: "2013-09-25T00:00:00.000Z"
End: "2013-09-26T00:00:00.000Z"
After the save, in the javascript client, the entity is updated with the new key and with the properties that come from the server.
The observable date objects will have the following value
Start: Wed Sep 25 2013 03:00:00 GMT+0300 (GTB Daylight Time)
End: Fri Sep 27 2013 02:59:58 GMT+0300 (GTB Daylight Time)
This is what i am getting back from the server
Start: "2013-09-25T00:00:00.000"
End: "2013-09-26T23:59:58.000"
How can i make sure that the hours in my object are not modified?
EDIT:
There is a a good explaniation here on what's happening with the datetime in javascript.
In the end i used this snipped to solve my problem:
breeze.DataType.parseDateFromServer = function (source) {
var date = moment(source);
return date.toDate();
};
It override's breeze own function with adds a time offset to the datetime.
Breeze does not manipulate the datetimes going to and from the server in any way EXCEPT to add a UTZ timezone specifier to any dates returned from the server that do not already have one. This is only done because different browsers interpret dates without a timezone specifier differently and we want consistency between browsers.
This is discussed in more detail in the answer posted here.
You are passing ISO8601 formatted timestamps, which is good. When you pass the Z at the end, you are indicating that the timestamp represents UTC. When you load those into JavaScript, it's going to take that into account.
You still need to show more code if you are looking for a useful response. What you've currently described from .NET doesn't quite line up with the timestamps you've provided. And it seems like most of this problem has to do with JavaScript and you haven't yet shown any of that code, so I can only guess what you might be doing. Please update your question, and understand that we have no knowledge of your system other than what you show us.
It's possible you may find moment.js to be useful in this scenario, but I can't elaborate further without seeing the relevent JavaScript code.
when I have:
var cultureInfo = CultureInfo.CreateSpecificCulture("fr-fr");
return date.ToString("dd MMM", cultureInfo);
I get back: 01 févr.
When I run the same code but with "en-us", I get back 01 Feb
What I would like is to get 01 fév
Any idea why this is happening (essentially the French culture adds an extra letter and a period) and how to get it to display only 3 month letters?
Thanks
--MB
I think you'll have to specifically set the DateTimeFormatInfo.AbbreviatedMonthNames property. You can have a look at the MSDN documentation here:
http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.abbreviatedmonthnames.aspx
Note that, according to the above doc, you also need to set the DateTimeFormatInfo.AbbreviatedMonthGenitiveName property as well.