Skip to content

Instantly share code, notes, and snippets.

@stulentsev
Created October 22, 2012 10:37
Show Gist options
  • Save stulentsev/3930837 to your computer and use it in GitHub Desktop.
Save stulentsev/3930837 to your computer and use it in GitHub Desktop.
reg.ru task
require 'optparse'
require 'open-uri'
require 'json'
if ARGV.length == 0
puts "Usage: ruby regru.rb --domain NAME --username USER --password PWD"
puts "Defaults: "
puts " domain: google.com"
puts " username: test"
puts " password: test"
exit(0)
end
options = {
username: 'test',
password: 'test',
method: 'domain/check',
domain: 'google.com'
}
OptionParser.new do |opts|
opts.on('--username USER') do |user|
options[:username] = user
end
opts.on('--password PWD') do |pwd|
options[:password] = pwd
end
opts.on('--domain NAME') do |name|
options[:domain] = name
end
end.parse!
url = "https://api.reg.ru/api/regru2/#{options[:method]}?domain_name=#{options[:domain]}&username=#{options[:username]}&password=#{options[:password]}"
open url do |f|
body = f.read
json = JSON.parse body
if json['error_text']
puts "ERROR: #{json['error_text']}"
else
name = json['answer']['domains'][0]['dname']
status = json['answer']['domains'][0]['result']
puts "#{name} is #{status}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment