Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #5 from ninoseki/add-range-validation
Browse files Browse the repository at this point in the history
feat: add range validation
  • Loading branch information
ninoseki committed Apr 20, 2019
2 parents 515d3d7 + 7f6fd7d commit 9222432
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/hachi/clients/artifact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def delete_by_id(id)
delete("/api/case/artifact/#{id}") { |json| json }
end

def search(data:, data_type:)
def search(data:, data_type:, range: "all")
validate_range range

artifact = Models::Artifact.new(data: data, data_type: data_type)
payload = {
query: {
Expand All @@ -41,7 +43,7 @@ def search(data:, data_type:)
}
}

post("/api/case/artifact/_search?range=all", payload) { |json| json }
post("/api/case/artifact/_search?range=#{range}", payload) { |json| json }
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lib/hachi/clients/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ def delete(path, params = {}, &block)
delete.add_field "Authorization", "Bearer #{api_key}"
request(delete, &block)
end

def validate_range(range)
return true if range == "all"
raise ArgumentError, "range should be 'all' or `from-to`" unless range.match?(/(\d+)-(\d+)/)

from, to = range.split("-").map(&:to_i)
return true if from < to

raise ArgumentError, "from should be smaller than to"
end
end
end
end
7 changes: 5 additions & 2 deletions lib/hachi/clients/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def create(title:, description:, severity: nil, start_date: nil, owner: nil, fla
post("/api/case", kase.payload) { |json| json }
end

def search(query)
def search(query, range: "all")
validate_range range

payload = {
query: {
_and:
Expand All @@ -45,7 +47,8 @@ def search(query)
]
}
}
post("/api/case/_search?range=all", payload) { |json| json }

post("/api/case/_search?range=#{range}", payload) { |json| json }
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/clients/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

RSpec.describe Hachi::Clients::Base do
let(:api_endpoint) { "http://locahost" }
let(:api_key) { "key" }

describe "#validate_range" do
subject { described_class.new(api_endpoint: api_endpoint, api_key: api_key) }

context "when given a valid range" do
let(:ranges) { %w(0-1 all 0-100 1-100) }

it "returns true" do
ranges.each do |range|
expect(subject.send(:validate_range, range)).to be(true)
end
end
end

context "when given an invalid range" do
let(:ranges) { %w(1 test 100-0) }

it "raise an ArgumentError" do
ranges.each do |range|
expect { subject.send(:validate_range, range) }.to raise_error(ArgumentError)
end
end
end
end
end

0 comments on commit 9222432

Please sign in to comment.