Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

Commit

Permalink
Added client.data_source to receive meta information about a source
Browse files Browse the repository at this point in the history
  • Loading branch information
Hagen Rother committed Feb 20, 2013
1 parent f3a1471 commit ac1b96e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/druid/client.rb
Expand Up @@ -45,5 +45,28 @@ def data_source_uri(source)
return URI(uri) if uri
nil
end

def data_source(source)
uri = data_source_uri(source)
raise "data source #{id} (currently) not available" unless uri

meta_path = "#{uri.path}datasources/#{source.split('/').last}"

req = Net::HTTP::Get.new(meta_path)

response = Net::HTTP.new(uri.host, uri.port).start do |http|
http.read_timeout = (2 * 60 * 1000)
http.request(req)
end

if response.code == "200"
meta = JSON.parse(response.body)
meta.define_singleton_method(:dimensions) { self['dimensions'] }
meta.define_singleton_method(:metrics) { self['metrics'] }
meta
else
raise "Request failed: #{response.code}: #{response.body}"
end
end
end
end
18 changes: 18 additions & 0 deletions spec/lib/client_spec.rb
Expand Up @@ -48,4 +48,22 @@
client.data_source_uri('madvertise/mock').should == URI('mock_uri')
end

it 'should report dimensions of a data source correctly' do
stub_request(:get, "http://www.example.com/druid/v2/datasources/mock").
with(:headers =>{'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => '{"dimensions":["d1","d2","d3"],"metrics":["m1", "m2"]}')

client = Druid::Client.new('test_uri', :static_setup => {'madvertise/mock' => 'http://www.example.com/druid/v2/'})
client.data_source('madvertise/mock').dimensions.should == ["d1","d2","d3"]
end

it 'should report metrics of a data source correctly' do
stub_request(:get, "http://www.example.com/druid/v2/datasources/mock").
with(:headers =>{'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => '{"dimensions":["d1","d2","d3"],"metrics":["m1", "m2"]}')

client = Druid::Client.new('test_uri', :static_setup => {'madvertise/mock' => 'http://www.example.com/druid/v2/'})
client.data_source('madvertise/mock').metrics.should == ["m1","m2"]
end

end

0 comments on commit ac1b96e

Please sign in to comment.