joker-dmapi 0.0.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Joker DMAPI client library
2
2
 
3
- https://joker.com/faq/category/33/22-dmapi.html
3
+ Description of the API can be found at https://joker.com/faq/category/33/22-dmapi.html
4
4
 
5
5
  ## Installation
6
6
 
data/lib/joker-dmapi.rb CHANGED
@@ -1 +1 @@
1
- require "joker-dmapi/client"
1
+ require "#{File.dirname(__FILE__)}/joker-dmapi/client"
@@ -1,27 +1,33 @@
1
1
  require "net/http"
2
2
  require "addressable/uri"
3
- require "joker-dmapi/result"
4
- require "joker-dmapi/contact"
5
- require "joker-dmapi/domain"
3
+ require "#{File.dirname(__FILE__)}/result"
4
+ require "#{File.dirname(__FILE__)}/contact"
5
+ require "#{File.dirname(__FILE__)}/host"
6
+ require "#{File.dirname(__FILE__)}/domain"
6
7
 
7
8
  module JokerDMAPI
8
- VERSION = '0.0.1'
9
+ VERSION = '0.1.2'
9
10
 
10
11
  class Client
11
- URI = 'https://dmapi.joker.com:443/request/'
12
+ DEFAULT_URI = 'https://dmapi.joker.com:443/request/'
12
13
 
13
14
  include JokerDMAPI::Result
14
15
  include JokerDMAPI::Contact
16
+ include JokerDMAPI::Host
15
17
  include JokerDMAPI::Domain
16
18
 
17
- def initialize(username, password)
18
- @username, @password = username, password
19
+ def initialize(username, password, uri = DEFAULT_URI)
20
+ @username, @password, @uri = username, password, uri
19
21
  end
20
22
 
21
- def self.with_connection(username, password, &block)
22
- connection = self.new(username, password)
23
- yield connection
24
- connection.logout
23
+ def self.connection(username, password, uri = DEFAULT_URI, &block)
24
+ connection = self.new(username, password, uri)
25
+ if block_given?
26
+ yield connection
27
+ connection.logout
28
+ else
29
+ connection
30
+ end
25
31
  end
26
32
 
27
33
  def logout
@@ -32,14 +38,23 @@ module JokerDMAPI
32
38
  end
33
39
 
34
40
  def query(request, params = {})
35
- params['auth-sid'] = auth_sid
36
- response = request(request, params.inject({}) { |r, (key, value)| r[key.to_s.gsub('_', '-')] = value; r })
37
- check_status response
41
+ response = query_no_raise request, params
42
+ raise_response(response) unless response[:headers][:status_code] == '0'
38
43
  response
39
44
  end
40
45
 
46
+ def tlds
47
+ auth_sid unless @auth_sid
48
+ @tlds
49
+ end
50
+
41
51
  private
42
52
 
53
+ def query_no_raise(request, params = {})
54
+ params['auth-sid'] = auth_sid
55
+ request(request, params.inject({}) { |r, (key, value)| r[key.to_s.gsub('_', '-')] = value; r })
56
+ end
57
+
43
58
  def parse_line(line)
44
59
  parts = line.split /\s*:\s*/, 2
45
60
  if parts.length == 2
@@ -64,29 +79,29 @@ module JokerDMAPI
64
79
  end
65
80
 
66
81
  def request(request, params = {})
67
- uri = Addressable::URI.parse(URI + request.to_s)
82
+ request = request.to_s.gsub('_', '-')
83
+ uri = Addressable::URI.parse(@uri + request)
68
84
  uri.query_values = params
69
85
  http = Net::HTTP.new(uri.host, uri.port)
70
86
  http.use_ssl = true
71
- #http.verify_mode = OpenSSL::SSL::VERIFY_NONE
72
- puts ">> #{uri.request_uri}" if ENV['JOKER_DMAPI_DEBUG']
87
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
88
+ puts ">> #{uri}" if ENV['JOKER_DMAPI_DEBUG']
73
89
  parse_response http.request(Net::HTTP::Get.new(uri.request_uri)).body
74
90
  end
75
91
 
76
- def check_status(response)
77
- if response[:headers][:status_code] != '0'
78
- raise "#{response[:headers][:status_code]}: #{response[:headers][:error]}\n"
79
- end
80
- end
81
-
82
92
  def auth_sid
83
93
  if @auth_sid.nil?
84
94
  response = request(:login, username: @username, password: @password)
85
- check_status response
95
+ raise "Authentication error" unless response[:headers].has_key? :auth_sid
86
96
  @auth_sid = response[:headers][:auth_sid]
87
97
  @tlds = response[:body].split "\n"
88
98
  end
89
99
  @auth_sid
90
100
  end
101
+
102
+ def raise_response(response)
103
+ raise "\n\n" + response[:headers].inject([]) { |s, (key, value)| s << "#{key}: #{value}"}.join("\n") +
104
+ "\n\n" + response[:body] + "\n\n"
105
+ end
91
106
  end
92
107
  end
@@ -4,7 +4,7 @@ module JokerDMAPI
4
4
  CONTACT_ALLOWED = CONTACT_REQUIRED + [ :organization, :state, :fax ]
5
5
  CONTACT_LENGTH_LIMIT = %w(biz cn eu)
6
6
 
7
- # Returns the information about a contact
7
+ # Returns the information about a contact or <tt>nil</tt> if not exists
8
8
  #
9
9
  # Takes handler as string
10
10
  #
@@ -23,27 +23,33 @@ module JokerDMAPI
23
23
  # [<tt>:created_date</tt>] the date and time of contact created
24
24
  # [<tt>:modified_date</tt>] the date and time of contact modified
25
25
  def contact_info(handle)
26
- response = query 'query-whois', contact: handle
27
- result = {}
28
- response[:body].split("\n").each do |line|
29
- line.slice! /^contact\./
30
- line_parsed = parse_line(line)
31
- next if line_parsed.is_a? String
32
- key, value = line_parsed.first
33
- case key
34
- when :name then next if value == "- -"
35
- when :address_1, :address_2, :address_3 then
36
- result[:address] = [] unless result.has_key? :address
37
- result[:address] << value
38
- when :state then next if value == "--"
39
- when :organization then next if value == "-" or value.empty?
40
- when :created_date, :modified_date then
41
- result[key] = DateTime.parse value
42
- else
43
- result.merge! line_parsed
44
- end
26
+ response = query_no_raise :query_whois, contact: handle
27
+ case response[:headers][:status_code]
28
+ when '2303' then nil
29
+ when '0' then
30
+ result = {}
31
+ response[:body].split("\n").each do |line|
32
+ line.slice! /^contact\./
33
+ line_parsed = parse_line(line)
34
+ next if line_parsed.is_a? String
35
+ key, value = line_parsed.first
36
+ case key
37
+ when :name then next if value == "- -"
38
+ when :address_1, :address_2, :address_3 then
39
+ result[:address] = [] unless result.has_key? :address
40
+ result[:address] << value
41
+ when :state then next if value == "--"
42
+ when :organization then next if value == "-" or value.empty?
43
+ when :created_date, :modified_date then
44
+ result[key] = DateTime.parse value
45
+ else
46
+ result.merge! line_parsed
47
+ end
48
+ end
49
+ result
50
+ else
51
+ raise_response response
45
52
  end
46
- result
47
53
  end
48
54
 
49
55
  # Create new contact
@@ -66,7 +72,7 @@ module JokerDMAPI
66
72
  # [<tt>:proc_id</tt>] process ID (used at check result)
67
73
  # [<tt>:tracking_id</tt>] tracking ID
68
74
  def contact_create(fields)
69
- query 'contact-create', contact_prepare(fields)
75
+ query :contact_create, contact_prepare(fields)
70
76
  end
71
77
 
72
78
  # Check result of create contact
@@ -75,11 +81,15 @@ module JokerDMAPI
75
81
  # Returned contact's handle name (and delete result) or <tt>nil</tt> if don't ready
76
82
  def contact_create_result(proc_id)
77
83
  result = parse_attributes(result_retrieve(proc_id)[:body].split("\n\n", 1)[0])
78
- if result.has_key?(:completion_status) and result[:completion_status] == 'ack'
79
- result_delete proc_id
80
- result[:object_name]
81
- else
82
- nil
84
+ return nil unless result.has_key? :completion_status
85
+ case result[:completion_status]
86
+ when 'ack' then
87
+ result_delete proc_id
88
+ result[:object_name]
89
+ when 'nack' then
90
+ raise_response response
91
+ else
92
+ nil
83
93
  end
84
94
  end
85
95
 
@@ -117,8 +127,7 @@ module JokerDMAPI
117
127
 
118
128
  def contact_prepare(fields)
119
129
  raise ArgumentError, "Required fields not found" unless (CONTACT_REQUIRED - fields.keys).empty?
120
- auth_sid
121
- raise ArgumentError, "TLD must be one of accepted" unless @tlds.include? fields[:tld]
130
+ raise ArgumentError, "TLD must be one of accepted" unless self.tlds.include? fields[:tld]
122
131
  if CONTACT_LENGTH_LIMIT.include? fields[:tld]
123
132
  [ :name, :organization, :city, :state ].each do |field|
124
133
  next unless fields.has_key? field
@@ -1,6 +1,6 @@
1
1
  module JokerDMAPI
2
2
  module Domain
3
- # Returns the information about a domain
3
+ # Returns the information about a domain or <tt>nil</tt> if not exists
4
4
  #
5
5
  # Takes FQDN as string
6
6
  #
@@ -26,37 +26,43 @@ module JokerDMAPI
26
26
  # [<tt>:modified_date</tt>] date and time of modification
27
27
  # [<tt>:expires</tt>] date and time of expiration
28
28
  def domain_info(domain)
29
- response = query 'query-whois', domain: domain
30
- result = {}
31
- response[:body].split("\n").each do |line|
32
- line.slice! /^domain\./
33
- line_parsed = parse_line(line)
34
- next if line_parsed.is_a? String
35
- key, value = line_parsed.first
36
- case key
37
- when :fqdn, :status then result.merge! line_parsed
38
- when :name, :organization, :city, :postal_code, :country, :owner_c_email, :email, :phone, :fax then
39
- result[:registrant] = {} unless result.has_key? :registrant
40
- result[:registrant].merge! line_parsed
41
- when :address_1, :address_2, :address_3 then
42
- result[:registrant] = {} unless result.has_key? :registrant
43
- result[:registrant][:address] = [] unless result[:registrant].has_key? :address
44
- result[:registrant][:address] << value
45
- when :reseller_line then
46
- result[:reseller_lines] = [] unless result.has_key? :reseller_lines
47
- result[:reseller_lines] << value
48
- when :created_date, :modified_date, :expires then
49
- result[key] = DateTime.parse value
50
- when :admin_c, :tech_c, :billing_c then
51
- result.merge! line_parsed
52
- when :nservers_nserver_handle then
53
- result[:nservers] = [] unless result.has_key? :nservers
54
- result[:nservers] << value
55
- else
56
- next
57
- end
29
+ response = query_no_raise :query_whois, domain: domain
30
+ case response[:headers][:status_code]
31
+ when '2303' then nil
32
+ when '0' then
33
+ result = {}
34
+ response[:body].split("\n").each do |line|
35
+ line.slice! /^domain\./
36
+ line_parsed = parse_line(line)
37
+ next if line_parsed.is_a? String
38
+ key, value = line_parsed.first
39
+ case key
40
+ when :fqdn, :status then result.merge! line_parsed
41
+ when :name, :organization, :city, :postal_code, :country, :owner_c_email, :email, :phone, :fax then
42
+ result[:registrant] = {} unless result.has_key? :registrant
43
+ result[:registrant].merge! line_parsed
44
+ when :address_1, :address_2, :address_3 then
45
+ result[:registrant] = {} unless result.has_key? :registrant
46
+ result[:registrant][:address] = [] unless result[:registrant].has_key? :address
47
+ result[:registrant][:address] << value
48
+ when :reseller_line then
49
+ result[:reseller_lines] = [] unless result.has_key? :reseller_lines
50
+ result[:reseller_lines] << value
51
+ when :created_date, :modified_date, :expires then
52
+ result[key] = DateTime.parse value
53
+ when :admin_c, :tech_c, :billing_c then
54
+ result.merge! line_parsed
55
+ when :nservers_nserver_handle then
56
+ result[:nservers] = [] unless result.has_key? :nservers
57
+ result[:nservers] << value
58
+ else
59
+ next
60
+ end
61
+ end
62
+ result
63
+ else
64
+ raise_response response
58
65
  end
59
- result
60
66
  end
61
67
 
62
68
  # Register new domain
@@ -77,7 +83,7 @@ module JokerDMAPI
77
83
  unless ([ :period, :registrant, :admin, :tech, :billing, :nservers ] - fields.keys).empty?
78
84
  raise ArgumentError, "Required fields not found"
79
85
  end
80
- query 'domain-register', {
86
+ query :domain_register, {
81
87
  domain: domain,
82
88
  period: (fields[:period] * 12),
83
89
  owner_c: fields[:registrant],
@@ -104,7 +110,7 @@ module JokerDMAPI
104
110
  unless ([ :admin, :tech, :billing, :nservers ] - fields.keys).empty?
105
111
  raise ArgumentError, "Required fields not found"
106
112
  end
107
- query 'domain-modify', {
113
+ query :domain_modify, {
108
114
  domain: domain,
109
115
  admin_c: fields[:admin],
110
116
  tech_c: fields[:tech],
@@ -118,7 +124,7 @@ module JokerDMAPI
118
124
  # Takes <tt>domain</tt> and <tt>period</tt>
119
125
  # WARNING!!! <tt>period</tt> in YEARS
120
126
  def domain_renew(domain, period)
121
- query 'domain-renew', { domain: domain, period: (12 * period) }
127
+ query :domain_renew, { domain: domain, period: (12 * period) }
122
128
  end
123
129
 
124
130
  # Update registrant's info
@@ -127,7 +133,7 @@ module JokerDMAPI
127
133
  def domain_registrant_update(domain, fields)
128
134
  fields = contact_prepare(fields)
129
135
  fields[:domain] = domain
130
- query 'domain-owner-change', fields
136
+ query :domain_owner_change, fields
131
137
  end
132
138
  end
133
139
  end
@@ -0,0 +1,51 @@
1
+ require "resolv"
2
+
3
+ module JokerDMAPI
4
+ module Host
5
+ def host_info(host)
6
+ response = query_no_raise :query_whois, { host: host }
7
+ case response[:headers][:status_code]
8
+ when '2303' then nil
9
+ when '0' then
10
+ result = {}
11
+ response[:body].split("\n").each do |line|
12
+ line.slice! /^host\./
13
+ line_parsed = parse_line(line)
14
+ next if line_parsed.is_a? String
15
+ key, value = line_parsed.first
16
+ case key
17
+ when :fqdn then result[:host] = value
18
+ when :created_date, :modified_date then
19
+ result[key] = DateTime.parse value
20
+ else
21
+ result.merge! line_parsed
22
+ end
23
+ end
24
+ result
25
+ else
26
+ raise_response response
27
+ end
28
+ end
29
+
30
+ def host_create(host, fields)
31
+ host_request :ns_create, host, fields
32
+ end
33
+
34
+ def host_update(host, fields)
35
+ host_request :ns_modify, host, fields
36
+ end
37
+
38
+ private
39
+
40
+ def host_request(request, host, fields)
41
+ unless fields.has_key?(:ipv4)
42
+ raise ArgumentError, "Required fields not found"
43
+ end
44
+ p = { host: host }
45
+ p[:ip] = fields[:ipv4] unless fields[:ipv4].empty?
46
+ p[:ipv6] = fields[:ipv6] if fields.has_key? :ipv6
47
+ query request, p
48
+ end
49
+
50
+ end
51
+ end
@@ -5,21 +5,27 @@ module JokerDMAPI
5
5
  # Get <tt>proc_id</tt>
6
6
  # Returned <tt>true</tt> if done (result deleted)
7
7
  def complete?(proc_id)
8
- result = parse_attributes(result_retrieve(proc_id)[:body].split("\n\n", 1)[0])
9
- if result.has_key?(:completion_status) and result[:completion_status] == 'ack'
10
- result_delete proc_id
11
- true
12
- else
13
- false
8
+ response = result_retrieve(proc_id)
9
+ result = parse_attributes(response[:body].split("\n\n", 1)[0])
10
+ return false unless result.has_key? :completion_status
11
+ case result[:completion_status]
12
+ when 'ack' then
13
+ result_delete proc_id
14
+ true
15
+ when 'nack' then
16
+ raise_response response
17
+ else
18
+ false
14
19
  end
15
20
  end
16
21
 
17
22
  def result_retrieve(proc_id)
18
- query 'result-retrieve', { proc_id: proc_id }
23
+ query :result_retrieve, { proc_id: proc_id }
19
24
  end
20
25
 
21
26
  def result_delete(proc_id)
22
- query 'result-delete', { proc_id: proc_id }
27
+ query :result_delete, { proc_id: proc_id }
23
28
  end
29
+
24
30
  end
25
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joker-dmapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-15 00:00:00.000000000 Z
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
@@ -60,6 +60,7 @@ files:
60
60
  - lib/joker-dmapi/client.rb
61
61
  - lib/joker-dmapi/contact.rb
62
62
  - lib/joker-dmapi/domain.rb
63
+ - lib/joker-dmapi/host.rb
63
64
  - lib/joker-dmapi/result.rb
64
65
  homepage: https://github.com/kolodovskyy/joker-dmapi
65
66
  licenses: []