class SetupUptimerobot

Constants

CONTACT_GROUP_IDS
HTTPS_MONITORING
STATUS_PAUSED

Public Class Methods

new(args) click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 11
def initialize(args)
  abort("No project name given.") if args.nil?
  @api_key = ask_for_api_key
  @url = validate_url(args)
end

Public Instance Methods

run() click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 17
def run
  robot_obj = monitoring_call(:new, create_robot_params)
  validate_new_project(robot_obj)
  response = monitoring_call(:edit, edit_robot_params(robot_obj["monitor"]["id"]))
  final_command_status(response)
end

Private Instance Methods

ask_for_api_key() click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 26
def ask_for_api_key
  say "This command will configure uptimerobot for your project."
  say "Please give in the api_key for uptimerobot"
  ask("API-key: ")
end
create_robot_params() click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 38
def create_robot_params
  {
    friendly_name: @url.host.downcase,
    url: @url.to_s,
    type: HTTPS_MONITORING,
    interval: 300,
    alert_contacts: CONTACT_GROUP_IDS
  }
end
edit_robot_params(uptimerobot_monitoring_id) click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 48
def edit_robot_params(uptimerobot_monitoring_id)
  { id: uptimerobot_monitoring_id, status: STATUS_PAUSED }
end
final_command_status(response) click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 52
def final_command_status(response)
  if response["stat"] == "ok"
    say "Successfully configured uptimerobot 🤩😎👍"
    say "The status for the monitored project has paused for now..."
    say "You can start it once your app is ready to go live 🤠"
  else
    say "Something went wrong during the configuration...😕. Uptimerobot returned '#{response["error"]["message"]}'"
  end
end
https_request(path, params = {}) click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 68
def https_request(path, params = {})
  uri = URI(path)
  req = request_body(uri, params)
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(req)
  end
end
monitoring_call(action, params) click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 84
def monitoring_call(action, params)
  response = https_request("https://api.uptimerobot.com/v2/#{action}Monitor", params)
  JSON.parse(response.body)
end
request_body(uri, params) click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 76
def request_body(uri, params)
  params[:api_key] = @api_key
  data = URI.encode_www_form params
  req = Net::HTTP::Post.new(uri)
  req.body = data
  req
end
validate_new_project(robot_obj) click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 62
def validate_new_project(robot_obj)
  return if robot_obj["stat"] == "ok"

  abort("An error occoured. Uptimerobot returned '#{robot_obj["error"]["message"]}'")
end
validate_url(args) click to toggle source
# File lib/renuo/cli/app/setup_uptimerobot.rb, line 32
def validate_url(args)
  abort("The url to be monitored was not given") if args.first.nil?
  abort("The url is invalid") unless args.first&.match?(URI::DEFAULT_PARSER.make_regexp)
  URI.parse(args.first)
end