From 369cdf6707cb4d27ffb7659b0d26dfc6e3dda790 Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Tue, 9 Feb 2016 17:02:17 +0100 Subject: [PATCH 1/5] Automatically convert Query#start_time and #end_time to UTC close #43 --- lib/twingly/search/query.rb | 27 ++++++++++++--- spec/query_spec.rb | 66 ++++++++++++++++++++++++++++++------- 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/lib/twingly/search/query.rb b/lib/twingly/search/query.rb index 6ec4ea4..b4db545 100644 --- a/lib/twingly/search/query.rb +++ b/lib/twingly/search/query.rb @@ -7,12 +7,13 @@ module Search # @attr [String] pattern the search query. # @attr [String] language which language to restrict the query to. # @attr [Client] client the client that this query is connected to. - # @attr [Time, #to_time] start_time search for posts published after + # @attr_reader [Time] start_time search for posts published after # this time (inclusive). - # @attr [Time, #to_time] end_time search for posts published before + # @attr_reader [Time] end_time search for posts published before # this time (inclusive). class Query - attr_accessor :pattern, :language, :client, :start_time, :end_time + attr_accessor :pattern, :language, :client + attr_reader :start_time, :end_time # No need to call this method manually, instead use {Client#query}. # @@ -56,14 +57,30 @@ def request_parameters } end + # Search for posts published after this time (inclusive). + # + # @param [Time, #to_time] time an instance of the Time class + # or an object responding to #to_time. + def start_time=(time) + @start_time = time.to_time.utc + end + + # Search for posts published before this time (inclusive). + # + # @param [Time, #to_time] time an instance of the Time class + # or an object responding to #to_time. + def end_time=(time) + @end_time = time.to_time.utc + end + private def ts - start_time.to_time.strftime("%F %T") if start_time + start_time.strftime("%F %T") if start_time end def ts_to - end_time.to_time.strftime("%F %T") if end_time + end_time.strftime("%F %T") if end_time end end end diff --git a/spec/query_spec.rb b/spec/query_spec.rb index 84a6506..ea90902 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -70,6 +70,60 @@ end end + describe "#start_time=" do + before do + subject.pattern = "semla" + subject.start_time = time + end + + context "when given time in UTC" do + let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+00:00") } + + it "should not change timezone" do + expect(subject.request_parameters).to include(ts: "2016-02-09 09:01:22") + end + end + + context "when given time not in UTC" do + let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+05:00") } + + it "should convert to UTC" do + expect(subject.request_parameters).to include(ts: "2016-02-09 04:01:22") + end + + it "should not modify the given time object" do + expect(subject.start_time).not_to equal(time) + end + end + end + + describe "#end_time=" do + before do + subject.pattern = "semla" + subject.end_time = time + end + + context "when given time in UTC" do + let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+00:00") } + + it "should not change timezone" do + expect(subject.request_parameters).to include(tsTo: "2016-02-09 09:01:22") + end + end + + context "when given time not in UTC" do + let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+05:00") } + + it "should convert to UTC" do + expect(subject.request_parameters).to include(tsTo: "2016-02-09 04:01:22") + end + + it "should not modify the given time object" do + expect(subject.end_time).not_to equal(time) + end + end + end + context "with valid pattern" do before { subject.pattern = "christmas" } @@ -78,18 +132,8 @@ expect(subject.request_parameters).to include(documentlang: 'en') end - it "should add start_time" do - subject.start_time = Time.new(2012, 12, 28, 9, 01, 22) - expect(subject.request_parameters).to include(ts: '2012-12-28 09:01:22') - end - - it "should add end_time" do - subject.end_time = Time.new(2013, 12, 28, 9, 01, 22) - expect(subject.request_parameters).to include(tsTo: '2013-12-28 09:01:22') - end - it "should encode url paramters" do - subject.end_time = Time.new(2013, 12, 28, 9, 01, 22) + subject.end_time = Time.new(2013, 12, 28, 9, 01, 22, "+00:00") expect(subject.url_parameters).to include('tsTo=2013-12-28+09%3A01%3A22') end end From 9a525a74dd9fc91709f7bccc715ac3f95e0e7410 Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Tue, 9 Feb 2016 17:14:28 +0100 Subject: [PATCH 2/5] Require "time" close #44 --- lib/twingly/search/query.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/twingly/search/query.rb b/lib/twingly/search/query.rb index b4db545..af0fe1b 100644 --- a/lib/twingly/search/query.rb +++ b/lib/twingly/search/query.rb @@ -1,4 +1,5 @@ require "faraday" +require "time" module Twingly module Search From 045f93408d4d9529947d4dcbbde13a5271d39a96 Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Tue, 9 Feb 2016 17:43:36 +0100 Subject: [PATCH 3/5] Make #start_time and #end_time show up in documentation Related to lsegal/yard#516 --- lib/twingly/search/query.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/twingly/search/query.rb b/lib/twingly/search/query.rb index af0fe1b..4efeb38 100644 --- a/lib/twingly/search/query.rb +++ b/lib/twingly/search/query.rb @@ -8,13 +8,18 @@ module Search # @attr [String] pattern the search query. # @attr [String] language which language to restrict the query to. # @attr [Client] client the client that this query is connected to. - # @attr_reader [Time] start_time search for posts published after - # this time (inclusive). - # @attr_reader [Time] end_time search for posts published before - # this time (inclusive). class Query attr_accessor :pattern, :language, :client - attr_reader :start_time, :end_time + + # @return [Time] the time that was set with {#start_time=}, converted to UTC. + def start_time + @start_time + end + + # @return [Time] the time that was set with {#end_time=}, converted to UTC. + def end_time + @end_time + end # No need to call this method manually, instead use {Client#query}. # From dd00a5f721a0caf3d28cc35d7c0e5415d600a4a6 Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Wed, 10 Feb 2016 08:49:14 +0100 Subject: [PATCH 4/5] Test that start_time and end_time is in utc (#utc?) --- spec/query_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/query_spec.rb b/spec/query_spec.rb index ea90902..0eb1e28 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -80,6 +80,7 @@ let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+00:00") } it "should not change timezone" do + expect(subject.start_time.utc?).to be(true) expect(subject.request_parameters).to include(ts: "2016-02-09 09:01:22") end end @@ -88,6 +89,7 @@ let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+05:00") } it "should convert to UTC" do + expect(subject.start_time.utc?).to be(true) expect(subject.request_parameters).to include(ts: "2016-02-09 04:01:22") end @@ -107,6 +109,7 @@ let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+00:00") } it "should not change timezone" do + expect(subject.end_time.utc?).to be(true) expect(subject.request_parameters).to include(tsTo: "2016-02-09 09:01:22") end end @@ -115,6 +118,7 @@ let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+05:00") } it "should convert to UTC" do + expect(subject.end_time.utc?).to be(true) expect(subject.request_parameters).to include(tsTo: "2016-02-09 04:01:22") end From 4a19f3567fd7b3ce947ead1aa7681494975c5c86 Mon Sep 17 00:00:00 2001 From: Mattias Roback Date: Wed, 10 Feb 2016 10:44:55 +0100 Subject: [PATCH 5/5] Use "UTC" instead of "+00:00" Ruby doesnt think that Time.parse("... +00:00").utc? is true. Since Time.new cannot take "UTC" as argument i switched so now all dates in the query test uses Time.parse instead. see https://github.com/twingly/twingly-search-api-ruby/pull/46#discussion_r52432909 --- spec/query_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/query_spec.rb b/spec/query_spec.rb index 0eb1e28..3427bf4 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -77,7 +77,7 @@ end context "when given time in UTC" do - let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+00:00") } + let(:time) { Time.parse("2016-02-09 09:01:22 UTC") } it "should not change timezone" do expect(subject.start_time.utc?).to be(true) @@ -86,7 +86,7 @@ end context "when given time not in UTC" do - let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+05:00") } + let(:time) { Time.parse("2016-02-09 09:01:22 +05:00") } it "should convert to UTC" do expect(subject.start_time.utc?).to be(true) @@ -106,7 +106,7 @@ end context "when given time in UTC" do - let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+00:00") } + let(:time) { Time.parse("2016-02-09 09:01:22 UTC") } it "should not change timezone" do expect(subject.end_time.utc?).to be(true) @@ -115,7 +115,7 @@ end context "when given time not in UTC" do - let(:time) { Time.new(2016, 2, 9, 9, 01, 22, "+05:00") } + let(:time) { Time.parse("2016-02-09 09:01:22 +05:00") } it "should convert to UTC" do expect(subject.end_time.utc?).to be(true) @@ -137,7 +137,7 @@ end it "should encode url paramters" do - subject.end_time = Time.new(2013, 12, 28, 9, 01, 22, "+00:00") + subject.end_time = Time.parse("2013-12-28 09:01:22 UTC") expect(subject.url_parameters).to include('tsTo=2013-12-28+09%3A01%3A22') end end