Class: UptimeRobot::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/uptimerobot/client.rb

Constant Summary collapse

ENDPOINT =
'http://api.uptimerobot.com'
USER_AGENT =
"Ruby UptimeRobot Client #{UptimeRobot::VERSION}"
METHODS =
[
  :getAccountDetails,
  :getMonitors,
  :newMonitor,
  :editMonitor,
  :deleteMonitor,
  :getAlertContacts,
  :newAlertContact,
  :deleteAlertContact
]
DEFAULT_ADAPTERS =
[
  Faraday::Adapter::NetHttp,
  Faraday::Adapter::Test
]

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/uptimerobot/client.rb', line 21

def initialize(options)
  @apiKey = options.delete(:apiKey)
  @raise_no_monitors_error = !!options.delete(:raise_no_monitors_error)

  raise ArgumentError, ':apiKey is required' unless @apiKey

  options[:url] ||= ENDPOINT

  @conn = Faraday.new(options) do |faraday|
    faraday.request  :url_encoded
    faraday.response :json, :content_type => /\bjson$/
    faraday.response :raise_error

    yield(faraday) if block_given?

    unless DEFAULT_ADAPTERS.any? {|i| faraday.builder.handlers.include?(i) }
      faraday.adapter Faraday.default_adapter
    end
  end

  @conn.headers[:user_agent] = USER_AGENT
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/uptimerobot/client.rb', line 46

def method_missing(method_name, *args, &block)
  unless METHODS.include?(method_name)
    raise NoMethodError, "undefined method: #{method_name}"
  end

  len = args.length
  params = args.first

  unless len.zero? or (len == 1 and params.kind_of?(Hash))
    raise ArgumentError, "invalid argument: #{args}"
  end

  request(method_name, params || {}, &block)
end