lib/uptimerobot/client.rb in uptimerobot-0.1.6 vs lib/uptimerobot/client.rb in uptimerobot-0.2.0
- old
+ new
@@ -1,8 +1,8 @@
class UptimeRobot::Client
- ENDPOINT = 'http://api.uptimerobot.com'
- USER_AGENT = "Ruby UptimeRobot Client #{UptimeRobot::VERSION}"
+ ENDPOINT = "https://api.uptimerobot.com/"
+ USER_AGENT = "Ruby UptimeRobot Client #{UptimeRobot::GEM_VERSION}"
METHODS = [
:getAccountDetails,
:getMonitors,
:newMonitor,
@@ -17,30 +17,32 @@
Faraday::Adapter::NetHttp,
Faraday::Adapter::Test
]
OPTIONS = [
- :apiKey,
+ :api_key,
:raise_no_monitors_error,
- :skip_unescape_monitor
+ :skip_unescape_monitor,
+ :debug
]
def initialize(options)
@options = {}
OPTIONS.each do |key|
@options[key] = options.delete(key)
end
- raise ArgumentError, ':apiKey is required' unless @options[:apiKey]
+ raise ArgumentError, ':api_key is required' unless @options[:api_key]
options[:url] ||= ENDPOINT
@conn = Faraday.new(options) do |faraday|
faraday.request :url_encoded
faraday.response :json, :content_type => /\bjson$/
faraday.response :raise_error
+ faraday.response :logger, ::Logger.new(STDOUT), bodies: true if @options[:debug]
yield(faraday) if block_given?
unless DEFAULT_ADAPTERS.any? {|i| faraday.builder.handlers.include?(i) }
faraday.adapter Faraday.default_adapter
@@ -67,18 +69,18 @@
request(method_name, params || {}, &block)
end
def request(method_name, params = {})
params.update(
- :apiKey => @options[:apiKey],
+ :api_key => @options[:api_key],
:format => 'json',
:noJsonCallback => 1
)
- response = @conn.get do |req|
- req.url "/#{method_name}"
- req.params = params
+ response = @conn.post do |req|
+ req.url "/#{UptimeRobot::API_VERSION}/#{method_name}"
+ req.body = URI.encode_www_form(params)
yield(req) if block_given?
end
json = response.body
validate_response!(json)
@@ -96,21 +98,21 @@
if @options[:raise_no_monitors_error]
raise UptimeRobot::Error.new(json)
else
json.update(
'total' => '0',
- 'monitors' => {'monitor' => []}
+ 'monitors' => []
)
end
else
raise UptimeRobot::Error.new(json)
end
end
end
def unescape_monitor!(json)
- json['monitors']['monitor'].each do |monitor|
- %w(friendlyname keywordvalue httpusername httppassword).each do |key|
+ json['monitors'].each do |monitor|
+ %w(friendly_name keyword_value http_username http_password).each do |key|
value = monitor[key] || ''
next if value.empty?
monitor[key] = CGI.unescapeHTML(value)
end
end