Permalink
Browse files

sonar api with authentication

  • Loading branch information...
1 parent a34d9ec commit cf69849cc6c9ca3304421e01c3ab9eb58ebf7f59 @dstywho dstywho committed Oct 11, 2011
Showing with 47 additions and 6 deletions.
  1. +6 −0 config/sonar.yml
  2. +20 −6 lib/SonarApi.rb
  3. +21 −0 lib/SonarConfig.rb
View
@@ -0,0 +1,6 @@
+--- !ruby/object:SonarConfig
+#api_uri: "http://maven.calgbapps.org/sonar/api/"
+api_uri: "http://nemo.sonarsource.org/api/"
+require_authentication: false
+password: password
+username: username
View
@@ -1,25 +1,40 @@
require 'rubygems'
require 'addressable/uri'
require 'open-uri'
+require 'SonarConfig'
+require 'yaml'
+require 'ruby-debug'
class SonarApi
URI = Addressable::URI.parse "http://nemo.sonarsource.org/api/"
-
+
+ def self.config
+ f = File.new(Rails.root + "config/sonar.yml")
+ raise "config file missing" unless File.exists? f
+ SonarConfig.from_yaml f
+ end
def self.url(path, options={:format => 'json'})
options ||= {:format => 'json' }
options[:format] = 'json' if options[:format].nil?
- url = URI.join path.to_s
+
+ debugger
+ url = Addressable::URI.parse(config.api_uri).join path.to_s
url.query_values = options
url.to_s
end
+ def self.http_get(path,options={})
+ puts url(path,options)
+ open(url(path, options), :http_basic_authentication => config.http_basic_authentication).readlines[0]
+ end
+
def self.metrics(options={})
- open(url(:metrics, options)).readlines[0]
+ http_get(:metrics,options)
end
def self.resources(options={})
- open(url(:resources, options)).readlines[0]
+ http_get(:resources,options)
end
#metric and resource are keys or ids
@@ -31,8 +46,7 @@ def self.timemachine(resource, metrics=[], options = {})
options[:to_datetime] = (date + 1.day).iso8601 unless options[:date].nil?
options[:metrics] = metrics.join(',')
options[:resource] = resource.to_s
- puts url(:timemachine, options)
- open(url(:timemachine, options)).readlines[0]
+ http_get(:timemachine,options)
end
class << self
View
@@ -0,0 +1,21 @@
+require 'rubygems' #dont' do this
+require 'yaml'
+class SonarConfig
+ attr_accessor :api_uri, :require_authentication, :username, :password
+
+ def to_yaml
+ Yaml::dump self
+ end
+
+ def http_basic_authentication
+ if require_authentication
+ return [username,password]
+ else
+ return nil
+ end
+ end
+
+ def self.from_yaml(file)
+ YAML::load file
+ end
+end

0 comments on commit cf69849

Please sign in to comment.