Skip to content

Instantly share code, notes, and snippets.

@nuheluxulu
Created April 19, 2019 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuheluxulu/4f9422c7d7953436aa60f992fa78bd19 to your computer and use it in GitHub Desktop.
Save nuheluxulu/4f9422c7d7953436aa60f992fa78bd19 to your computer and use it in GitHub Desktop.
# 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