5

I'm not sure why I can't figure this out it's so simple.

I'm trying to concatenate todays date in the format YYYY-MM-dd to a string and it doesn't work.

I've tried creating the date with now = Time.now.strftime("%Y-%m-%d")and concatenating it like this

'http://api.flurry.com/appMetrics/Sessions?apiAccessCode=########&apiKey=#########&startDate='+now+'&endDate='+now

Please advise, thank you.

edit: If I set now = '2016-11-05' then everything works fine. So the issue is definitely in how I'm creating my date.

Walker
  • 1,127
  • 15
  • 39
  • When I create a string with the dates hardcoded, my api works fine. that string looks like this `http://api.flurry.com/appMetrics/Sessions?apiAccessCode=########&apiKey=########&startDate=2016-11-05&endDate=2016-11-05` and when I try to dynamically set the date, it just returns 0 @TerryLi – Walker Nov 06 '16 at 02:57
  • I'm not sure how to test for what `now` is but I do know that `now` is not a string with the date 2016-11-05 because that api string returns a result. The error is in my creation of the date or in the concatenation. @TerryLi Thanks Terry – Walker Nov 06 '16 at 03:02
  • 2
    I have tried `now = Time.now.strftime("%Y-%m-%d")` followed by `url = "http://api.flurry.com/appMetrics/Sessions?apiAccessCode=######&apiKey=######&startDate=" + now + "&endDate=" + now` which works fine in interactive ruby session – Imran Ali Nov 06 '16 at 03:02
  • @ImranAli please read my reply above. Any ideas? It wouldn't be because your url has double quotes and mine has single right. Sorry just recently started programming in rails. – Walker Nov 06 '16 at 03:03
  • `now = DateTime.now.strftime("%Y-%m-%d")` ` old_ios_url = 'http://api.flurry.com/appMetrics/Sessions?apiAccessCode=####&apiKey=####&startDate='+now+'&endDate='+now ` `old_ios_response = RestClient.get(old_ios_url)` `old_ios_result = JSON.parse(old_ios_response)` `old_ios_sessions = old_ios_result['day']['@value']` `result = Integer(old_ios_sessions)` @TerryLi – Walker Nov 06 '16 at 03:14
  • Its hard to tell whats wrong, but may be trying different ways to concatenate could result in success. Try [string interpolation](https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#Interpolation) and [.concat()](http://ruby-doc.org/core-1.9.2/String.html#method-i-concat) – Imran Ali Nov 06 '16 at 03:21
  • @TerryLi It's already there Terry. Thank you – Walker Nov 06 '16 at 03:23
  • Have you tried printing out the value of `old_ios_url` for both hardcoded and concatenated versions? Then tell us if there's any difference. – Terry Li Nov 06 '16 at 03:26
  • @TerryLi I just tried setting `now = '2016-11-05'` and everything works fine. So the problem is definitely in my string from date area – Walker Nov 06 '16 at 03:28
  • I would try two things 1) check if `now == '2016-11-05'` returns `true` 2) try using string interpolation instead of string concatenation and see if the issue is gone. – Terry Li Nov 06 '16 at 03:38

1 Answers1

5

I ended up making it work with string interpolation after it not working with string interpolation. Why wasn't it working? Turns out you have to use double quotes if not it takes the interpolation as literal!!

now = Time.now.strftime("%Y-%m-%d")

"http://api.flurry.com/appMetrics/Sessions?apiAccessCode=########&apiKey=#########&startDate=#{now}&endDate=#{now}"
Walker
  • 1,127
  • 15
  • 39