lib/pingdom/cli/core.rb in pingdom-cli-0.2.0 vs lib/pingdom/cli/core.rb in pingdom-cli-0.3.0
- old
+ new
@@ -5,62 +5,73 @@
module Cli
class Core
def initialize(config)
@config = config
- @url_base = "https://#{CGI::escape @config['user']}:#{CGI::escape @config['password']}@api.pingdom.com/api/2.0/"
- @header = {"App-Key" => @config['app_key']}
+ @url_base = "https://api.pingdom.com/api/2.0/"
+ @header = { "App-Key" => @config['app_key'] }
end
def checks
- response = RestClient.get(get_url('checks'), @header)
+ response = get_resource('checks').get
results = JSON.parse(response.body, :symbolize_names => true)
- results[:checks].each do |result|
+ results[__method__].each do |result|
result[:lasttesttime] = Time.at(result[:lasttesttime]) unless result[:lasttesttime].nil?
result[:lasterrortime] = Time.at(result[:lasterrortime]) unless result[:lasterrortime].nil?
result[:created] = Time.at(result[:created]) unless result[:created].nil?
end
- results
+ results[__method__]
end
def actions
- response = RestClient.get(get_url('actions'), @header)
- JSON.parse(response.body, :symbolize_names => true)
+ response = get_resource('actions').get
+ JSON.parse(response.body, :symbolize_names => true)[__method__]
end
def contacts
- response = RestClient.get(get_url('contacts'), @header)
- JSON.parse(response.body, :symbolize_names => true)
+ response = get_resource('contacts').get
+ JSON.parse(response.body, :symbolize_names => true)[__method__]
end
def probes
- response = RestClient.get(get_url('probes'), @header)
- JSON.parse(response.body, :symbolize_names => true)
+ response = get_resource('probes').get
+ JSON.parse(response.body, :symbolize_names => true)[__method__]
end
def reference
- response = RestClient.get(get_url('reference'), @header)
- JSON.parse(response.body, :symbolize_names => true)
+ response = get_resource('reference').get
+ JSON.parse(response.body, :symbolize_names => true)[__method__]
end
def reports_public
- response = RestClient.get(get_url('reports.public'), @header)
- JSON.parse(response.body, :symbolize_names => true)
+ response = get_resource('reports.public').get
+ JSON.parse(response.body, :symbolize_names => true)[__method__]
end
def settings
- response = RestClient.get(get_url('settings'), @header)
- JSON.parse(response.body, :symbolize_names => true)
+ response = get_resource('settings').get
+ JSON.parse(response.body, :symbolize_names => true)[__method__]
end
+ def credits
+ response = get_resource('credits').get
+ JSON.parse(response.body, :symbolize_names => true)[__method__]
+ end
+
def update(params)
- response = RestClient.put(get_url('checks'), params, @header)
+ response = get_resource('checks').put(params)
response.body
end
private
- def get_url(action)
- @url_base + action
+ def get_resource(action)
+ resource = RestClient::Resource.new(
+ @url_base + action,
+ :user => @config['user'],
+ :password => @config['password'],
+ :headers => { 'App-Key' => @config['app_key'] },
+ :verify_ssl => false
+ )
end
end
end
end