lib/tracksale/answer.rb in tracksale-0.0.5 vs lib/tracksale/answer.rb in tracksale-0.0.7
- old
+ new
@@ -13,12 +13,12 @@
def campaign
Tracksale::Campaign.find_by_code(campaign_code)
end
class << self
- def all
- raw_all.map { |answer| create_from_response(answer) }
+ def all( start_time=(Time.now-86_400), end_time=(Time.now+86_400))
+ raw_all(start_time,end_time).map { |answer| create_from_response(answer) }
end
def create_from_response(raw_response)
new.tap do |answer|
answer.time = Time.at(raw_response['time'].to_i)
@@ -42,12 +42,16 @@
answer.categories = raw_response['categories'].map { |c| c['name'] }
answer.justifications = convert_justif(raw_response['justifications'])
end
end
- def raw_all
- client.get('report/answer?tags=true&limit=' + LIMIT.to_s)
+ def raw_all( start_time=(Time.now-86_400), end_time=(Time.now+86_400))
+ start_date = start_time.strftime('%Y-%m-%d')
+ end_date = end_time.strftime('%Y-%m-%d')
+ all_request = "report/answer?tags=true&limit=#{LIMIT}&start=#{start_date}&end=#{end_date}"
+
+ client.get(all_request)
end
def client
Tracksale.configuration.client.new
end
@@ -58,13 +62,18 @@
end.reduce(&:merge)
end
def convert_justif(multiple_answers)
multiple_answers.map do |single_answer|
- {
- JSON.parse(single_answer['name']).values.first =>
- single_answer['children'].map { |c| JSON.parse(c).values.first }
- }
+ begin
+ {
+ JSON.parse(single_answer['name']).values.first =>
+ single_answer['children'].map { |c| JSON.parse(c).values.first }
+ }
+
+ rescue JSON::ParserError
+ { single_answer['name'] => single_answer['children'] }
+ end
end
end
end
end
end