I am working on analytics ruby api client. When making a call i am sending the params as
dimensions = ["ga:hostName", "pagePath"]
metrics = ["pageValue", "ga:pageviews"]
.call_analytics(dimensions, metrics)
Even if the user does not enter "ga:" when passing params, the code should append "ga:" in the params.
I have done it this way.
dimensions = dimensions.map{|a| ("ga:" + a.split(":").last).split}.flatten
metrics = metrics.map{|a| ("ga:" + a.split(":").last).split}.flatten
Is there a better way to do it?
["ga:hostName", "pagePath"]
.map{|s| s.sub(/\A(?!ga:)/, "ga:")}
#=> ["ga:hostName", "ga:pagePath"]
["pageValue", "ga:pageviews"]
.map{|s| s.sub(/\A(?!ga:)/, "ga:")}
#=> ["ga:pageValue", "ga:pageviews"]
or
["ga:hostName", "pagePath"]
.map{|s| s.start_with?("ga:") ? s : s.prepend("ga:")}
#=> ["ga:hostName", "ga:pagePath"]
["pageValue", "ga:pageviews"]
.map{|s| s.start_with?("ga:") ? s : s.prepend("ga:")}
#=> ["ga:pageValue", "ga:pageviews"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With