Class: INWX::Domrobot

Inherits:
Object
  • Object
show all
Defined in:
lib/inwx-domrobot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDomrobot

Returns a new instance of Domrobot.



18
19
20
21
22
23
# File 'lib/inwx-domrobot.rb', line 18

def initialize
  self.cookie = ''
  self.debug = false
  self.url = URL_OTE
  self.api_type = TYPE_JSON
end

Instance Attribute Details

#api_typeObject

Returns the value of attribute api_type.



11
12
13
# File 'lib/inwx-domrobot.rb', line 11

def api_type
  @api_type
end

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/inwx-domrobot.rb', line 11

def client
  @client
end

Returns the value of attribute cookie.



11
12
13
# File 'lib/inwx-domrobot.rb', line 11

def cookie
  @cookie
end

#debugObject

Returns the value of attribute debug.



11
12
13
# File 'lib/inwx-domrobot.rb', line 11

def debug
  @debug
end

#languageObject

Returns the value of attribute language.



11
12
13
# File 'lib/inwx-domrobot.rb', line 11

def language
  @language
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/inwx-domrobot.rb', line 11

def url
  @url
end

Instance Method Details

#call(object, method, params = {}) ⇒ Object



82
83
84
85
86
87
88
# 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 = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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 = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# 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_clientObject



55
56
57
58
59
60
61
62
# 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) ⇒ Object



130
131
132
133
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/inwx-domrobot.rb', line 64

def (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

#logoutObject



78
79
80
# File 'lib/inwx-domrobot.rb', line 78

def logout
  call('account', 'logout')
end

#output_debug(sent, received) ⇒ Object



120
121
122
123
124
125
126
127
128
# 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


116
117
118
# File 'lib/inwx-domrobot.rb', line 116

def save_cookie(cookie)
  self.cookie = cookie unless cookie.nil?
end

#set_debug(value) ⇒ Object



50
51
52
53
# File 'lib/inwx-domrobot.rb', line 50

def set_debug(value)
  self.debug = value
  self
end

#set_language(language = 'en') ⇒ Object



25
26
27
28
# File 'lib/inwx-domrobot.rb', line 25

def set_language(language = 'en')
  self.language = language
  self
end

#use_jsonObject



40
41
42
43
# File 'lib/inwx-domrobot.rb', line 40

def use_json
  self.api_type = TYPE_JSON
  self
end

#use_liveObject



35
36
37
38
# File 'lib/inwx-domrobot.rb', line 35

def use_live
  self.url = URL_LIVE
  self
end

#use_oteObject



30
31
32
33
# File 'lib/inwx-domrobot.rb', line 30

def use_ote
  self.url = URL_OTE
  self
end

#use_xmlObject



45
46
47
48
# File 'lib/inwx-domrobot.rb', line 45

def use_xml
  self.api_type = TYPE_XML
  self
end