Skip to content

Commit

Permalink
Merge pull request #22 from arronmabrey/uri_parsing
Browse files Browse the repository at this point in the history
Feature/uri_parsing
  • Loading branch information
dwbutler committed Sep 30, 2014
2 parents 9d58c24 + 4a58def commit ea25fe0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/logstash-logger/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ module Device

def self.new(opts)
opts = opts.dup

if parsed_uri_opts = parse_uri_config(opts)
opts = parsed_uri_opts
end

type = opts.delete(:type) || DEFAULT_TYPE

device_klass_for(type).new(opts)
end

def self.parse_uri_config(opts)
if uri = opts[:uri]
parsed = URI.parse(uri)
{type: parsed.scheme, host: parsed.host, port: parsed.port, path: parsed.path}
end
end

def self.device_klass_for(type)
case type.to_sym
when :udp then UDP
Expand Down
48 changes: 48 additions & 0 deletions spec/device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,52 @@
end
end

describe ".parse_uri_config" do
subject(:parse_uri_config) { described_class.parse_uri_config(uri_config) }

context "when uri_config is valid" do
let(:uri_config) { udp_uri_config }
it { is_expected.to eq({type: 'udp', host: 'localhost', port: 5228, path: ''}) }
end

context "when uri is invalid" do
let(:uri_config) { invalid_uri_config }
specify { expect{ parse_uri_config }.to raise_error(URI::InvalidURIError) }
end
end

describe "Parsing URI configurations" do
subject(:new_device) { described_class.new(uri_config) }

context "when URI config is udp" do
let(:uri_config) { udp_uri_config }
it { is_expected.to be_a LogStashLogger::Device::UDP }
end

context "when URI config is tcp" do
let(:uri_config) { tcp_uri_config }
it { is_expected.to be_a LogStashLogger::Device::TCP }
end

context "when URI config is unix" do
let(:uri_config) { unix_uri_config }
it { is_expected.to be_a LogStashLogger::Device::Unix }
end

context "when URI config is file" do
let(:uri_config) { file_uri_config }
it { is_expected.to be_a LogStashLogger::Device::File }
end

context "when URI config is redis" do
let(:uri_config) { redis_uri_config }
it { is_expected.to be_a LogStashLogger::Device::Redis }
end

context "when URI config is stdout" do
let(:uri_config) { stdout_uri_config }
it { is_expected.to be_a LogStashLogger::Device::Stdout }
end
end

end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ def connection_type
let(:io_device) { LogStashLogger::Device.new(type: :io, io: io)}

let(:redis_device) { LogStashLogger::Device.new(type: :redis, sync: true) }

let(:invalid_uri_config) { {uri: "non parsable uri" } }
let(:udp_uri_config) { {uri: "udp://localhost:5228" } }
let(:tcp_uri_config) { {uri: "tcp://localhost:5229" } }
let(:unix_uri_config) { {uri: "unix:///some/path/to/socket"} }
let(:file_uri_config) { {uri: "file://#{file.path}" } }
let(:redis_uri_config) { {uri: "redis://localhost:9999" } }
let(:stdout_uri_config) { {uri: "stdout://localhost" } }
end

0 comments on commit ea25fe0

Please sign in to comment.