diff --git a/lib/twingly/search/query.rb b/lib/twingly/search/query.rb index 6ec4ea4..4efeb38 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 @@ -7,12 +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 [Time, #to_time] start_time search for posts published after - # this time (inclusive). - # @attr [Time, #to_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 + + # @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}. # @@ -56,14 +63,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..3427bf4 100644 --- a/spec/query_spec.rb +++ b/spec/query_spec.rb @@ -70,6 +70,64 @@ 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.parse("2016-02-09 09:01:22 UTC") } + + 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 + + context "when given time not in UTC" do + 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) + 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.parse("2016-02-09 09:01:22 UTC") } + + 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 + + context "when given time not in UTC" do + 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) + 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 +136,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.parse("2013-12-28 09:01:22 UTC") expect(subject.url_parameters).to include('tsTo=2013-12-28+09%3A01%3A22') end end