class INWX::Domrobot

Constants

TYPE_JSON
TYPE_XML
URL_LIVE
URL_OTE

Attributes

api_type[RW]
client[RW]
debug[RW]
language[RW]
url[RW]

Public Class Methods

new() click to toggle source
# File lib/inwx-domrobot.rb, line 18
def initialize
  self.cookie = ''
  self.debug = false
  self.url = URL_OTE
  self.api_type = TYPE_JSON
end

Public Instance Methods

call(object, method, params = {}) click to toggle source
# File lib/inwx-domrobot.rb, line 82
def call(object, method, params = {})
  if api_type == TYPE_XML
    call_xml(object, method, params)
  else
    call_json(object, method, params)
  end
end
call_json(object, method, params = {}) click to toggle source
# File lib/inwx-domrobot.rb, line 101
def call_json(object, method, params = {})
  req = Net::HTTP::Post.new(api_type)

  json_params = { method: object + '.' + method, params: params }

  req.body = json_params.to_json
  req['Cookie'] = cookie
  res = client.request(req)

  save_cookie(res.response['set-cookie'])
  output_debug(req.body, res.body)

  JSON.parse(res.body)
end
call_xml(object, method, params = {}) click to toggle source
# File lib/inwx-domrobot.rb, line 90
def call_xml(object, method, params = {})
  client.cookie = cookie
  res = client.call(object + '.' + method, params)

  save_cookie(client.cookie)
  debug_sent = { method: object + '.' + method, params: params }
  output_debug(debug_sent.to_json, res.to_json)

  res
end
create_client() click to toggle source
# File lib/inwx-domrobot.rb, line 55
def create_client
  if api_type == TYPE_XML
    self.client = XMLRPC::Client.new(url, api_type, '443', nil, nil, nil, nil, true, 100)
  else
    self.client = Net::HTTP.new(url, 443)
    client.use_ssl = true
  end
end
get_secret_code(shared_secret) click to toggle source
# File lib/inwx-domrobot.rb, line 130
def get_secret_code(shared_secret)
  totp = ROTP::TOTP.new(shared_secret)
  totp.now
end
login(username = false, password = false, shared_secret = nil) click to toggle source
# File lib/inwx-domrobot.rb, line 64
def login(username = false, password = false, shared_secret = nil)
  create_client

  params = { user: username, pass: password, lang: language }
  ret = call('account', 'login', params)

  unless shared_secret.nil?
    secret_code = get_secret_code(shared_secret)
    ret = call('account', 'unlock', tan: secret_code)
  end

  ret
end
logout() click to toggle source
# File lib/inwx-domrobot.rb, line 78
def logout
  call('account', 'logout')
end
output_debug(sent, received) click to toggle source
# File lib/inwx-domrobot.rb, line 120
def output_debug(sent, received)
  if debug
    puts 'Sent:'
    puts JSON.pretty_generate(JSON.parse(sent))
    puts 'Received:'
    puts JSON.pretty_generate(JSON.parse(received))
    puts '-------------------------'
  end
end
set_debug(value) click to toggle source
# File lib/inwx-domrobot.rb, line 50
def set_debug(value)
  self.debug = value
  self
end
set_language(language = 'en') click to toggle source
# File lib/inwx-domrobot.rb, line 25
def set_language(language = 'en')
  self.language = language
  self
end
use_json() click to toggle source
# File lib/inwx-domrobot.rb, line 40
def use_json
  self.api_type = TYPE_JSON
  self
end
use_live() click to toggle source
# File lib/inwx-domrobot.rb, line 35
def use_live
  self.url = URL_LIVE
  self
end
use_ote() click to toggle source
# File lib/inwx-domrobot.rb, line 30
def use_ote
  self.url = URL_OTE
  self
end
use_xml() click to toggle source
# File lib/inwx-domrobot.rb, line 45
def use_xml
  self.api_type = TYPE_XML
  self
end