Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions lib/twingly/search/query.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "faraday"
require "time"

module Twingly
module Search
Expand All @@ -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}.
#
Expand Down Expand Up @@ -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
Expand Down
70 changes: 59 additions & 11 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,64 @@
end
end

describe "#start_time=" do
before do
subject.pattern = "semla"
subject.start_time = time
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also include tests like this? expect(subject.start_time.utc?).to be(true) or is it too redundant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not. I don't think they need their own it block though. I'll add them to the "should convert to UTC" block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to change, but "when given non-UTC time" has better flow imo :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so too, but I can live with this 😄

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" }

Expand All @@ -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
Expand Down