-
-
Save nuheluxulu/4f9422c7d7953436aa60f992fa78bd19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require_relative './api_response' | |
require_relative 'command' | |
require 'httparty' | |
# API for Namecheap | |
# https://www.namecheap.com/support/api/intro.aspx | |
class NamecheapApi | |
attr_reader :endpoint, :key, :username | |
def initialize(**opts) | |
@endpoint = opts[:endpoint] || 'https://api.sandbox.namecheap.com/xml.response' | |
@key = opts[:key] || Rails.application.credentials.sandbox[:namecheap][:key] | |
# @endpoint = "https://api.namecheap.com/xml.response" | |
default_username = Rails.application.credentials.sandbox[:namecheap][:username] | |
@username = opts[:username] || default_username | |
end | |
def call(cmd:, ip:) | |
# Call command factory and parse info | |
# run execute on command | |
# Executor.do(cmd, ip, endpoint, creds) | |
url = inspect_cmd(cmd: cmd, ip: ip) | |
response = HTTParty.get(url) | |
# message.eql? 'OK' | |
return ApiResponse.new(response.body) if response.code == 200 | |
raise 'Could not reach endpoint' | |
end | |
def inspect_cmd(cmd:, ip:) | |
"#{endpoint}?ApiUser=#{username}&ApiKey=#{key}&UserName=#{username}&Command=#{cmd}&ClientIp=#{ip}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment