Class: UptimeRobot::Client

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

Constant Summary collapse

ENDPOINT =
"https://api.uptimerobot.com/"
USER_AGENT =
"Ruby UptimeRobot Client #{UptimeRobot::GEM_VERSION}"
METHODS =
[
  :getAccountDetails,
  :getMonitors,
  :newMonitor,
  :editMonitor,
  :deleteMonitor,
  :getAlertContacts,
  :newAlertContact,
  :deleteAlertContact
]
DEFAULT_ADAPTERS =
[
  Faraday::Adapter::NetHttp,
  Faraday::Adapter::Test
]
OPTIONS =
[
  :api_key,
  :raise_no_monitors_error,
  :skip_unescape_monitor,
  :debug
]

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/uptimerobot/client.rb', line 28

def initialize(options)
  @options = {}

  OPTIONS.each do |key|
    @options[key] = options.delete(key)
  end

  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
    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)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/uptimerobot/client.rb', line 57

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