mtgsy 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6eb2a4220e200d384fb285d46b9f7699fd7136ec
4
- data.tar.gz: 98b95a7837173072dfd5ebdd89de11e4218e245d
3
+ metadata.gz: 5eb81b3ca3aa7b5260a4ac6ffdaa495443a50c07
4
+ data.tar.gz: 3c3bc8c0a0606530490ba26b4957f685d3edf2d6
5
5
  SHA512:
6
- metadata.gz: c1a4f3bcc32b1af611d6b0abda76b08f6d78a6c6aa13aeb2b4ff1637fde0f858a1219a716f61d2c4d7bc3c1268def824058de6f236eb0f00b8c342ec8cfe7270
7
- data.tar.gz: 08b33dd5265662da725e49b6e7fc4cc0c94173289140a5a4f37de7a1ed0008b873c7e4218fd0a959c537396e367813f933ecff3ba531f6273923a9ccd9db1a91
6
+ metadata.gz: ceec4d977f42565e3623d94045ee0414ea21240d15fbde873aadd9fd673785af1cb817436d8526f9ddf6e1243a9083bec77fcf2292309e131611a00048141381
7
+ data.tar.gz: 592b20fd9d189d2356419eb84b467154d691c2aacf9e9c5c8198a59043a24b02c99a3ec34916f70b0924e6b55b4b41d6a55544db8392e637826e1027c507bc87
@@ -6,15 +6,18 @@ module Mtgsy
6
6
  def initialize(options = {})
7
7
  @agent = Mechanize.new
8
8
  @api_endpoint = "https://www.mtgsy.net/dns/api.php"
9
+ @record_types = [ 'A', 'AAAA', 'CNAME', 'HINFO', 'MX', 'NS', 'PTR', 'RP', 'SRV', 'TXT' ]
10
+
11
+ @debug = options[:debug] ? options[:debug] : false
9
12
 
10
13
  # attr_accessor
11
14
  @domainname = options[:domainname] ? options[:domainname] : nil
12
15
  @username = options[:username] ? options[:username] : nil
16
+ @ttl = options[:ttl] ? options[:ttl] : 900
13
17
  @aux = 0
14
- @ttl = 1800
15
18
 
16
19
  # attr_writer
17
- @password = options[:password] ? options[:password] : nil
20
+ @password = options[:password] ? options[:password] : nil
18
21
  end
19
22
 
20
23
  attr_accessor :domainname, :username, :aux, :ttl
@@ -28,33 +31,109 @@ module Mtgsy
28
31
  aux = options[:aux ] ? options[:aux].to_s : @aux.to_s
29
32
  ttl = options[:ttl ] ? options[:ttl].to_s : @ttl.to_s
30
33
 
31
- @agent.post(@api_endpoint, [
32
- [ 'command' ,command ],
33
- [ 'username' ,@username ],
34
- [ 'password' ,@password ],
35
- [ 'domainname' ,@domainname ],
36
- [ 'name' ,name ],
37
- [ 'type' ,type ],
38
- [ 'data' ,data ],
39
- [ 'aux' ,aux ],
40
- [ 'ttl' ,ttl ]
41
- ])
34
+ post_to_mtgsy([ command, name, type, data, aux, ttl ])
42
35
 
36
+ what_happened?(@agent, command)
43
37
  end
44
38
 
45
39
  def delete_record(options={})
46
40
  command = "deleterecord"
47
- puts "TODO: Delete record"
41
+ name = options[:name] ? options[:name].to_s : (raise "delete_record requires :name")
42
+ type = options[:type] ? options[:type].to_s : nil
43
+ data = options[:data] ? options[:data].to_s : nil
44
+ aux = options[:aux ] ? options[:aux].to_s : nil
45
+ ttl = options[:ttl ] ? options[:ttl].to_s : nil
46
+
47
+ post_to_mtgsy([ command, name, type, data, aux, ttl ])
48
+
49
+ what_happened?(@agent, command)
48
50
  end
49
51
 
50
52
  def update_record(options={})
51
53
  command = "updaterecord"
52
- puts "TODO: Update record"
54
+ name = options[:name] ? options[:name].to_s : (raise "update_record requires :name")
55
+ type = options[:type] ? options[:type].to_s : nil
56
+ data = options[:data] ? options[:data].to_s : nil
57
+ aux = options[:aux ] ? options[:aux].to_s : nil
58
+ ttl = options[:ttl ] ? options[:ttl].to_s : nil
59
+
60
+ post_to_mtgsy([ command, name, type, data, aux, ttl ])
61
+
62
+ what_happened?(@agent, command)
63
+ end
64
+
65
+ def refresh!
66
+ command = "listrecords"
67
+ post_to_mtgsy([ command ])
68
+
69
+ @records_ALL = @agent.page.body.split('<br>')
70
+ @records_ALL.pop
71
+ @records_ALL.collect! { |i| i.split(",") }
72
+
73
+
74
+ @record_types.each do |x|
75
+ tmp_placeholder = []
76
+ @records_ALL.each do |record|
77
+ tmp_placeholder << record if record[RECORD_TYPE] == x
78
+ end
79
+ instance_variable_set("@records_#{x}", tmp_placeholder)
80
+ end
81
+ end
82
+
83
+ def records(type="ALL")
84
+ self.refresh! unless @records_ALL
85
+
86
+ eval("@records_#{type}")
87
+ end
88
+
89
+ def record_types?
90
+ @record_types
53
91
  end
54
92
 
55
93
  private
56
- def what_happened?(mechanize_instance)
57
- mechanize_instance.page.code
94
+ def post_to_mtgsy(params)
95
+ command = params[Mtgsy::POST_COMMAND]
96
+ name = params[Mtgsy::POST_NAME]
97
+ type = params[Mtgsy::POST_TYPE]
98
+ data = params[Mtgsy::POST_DATA]
99
+ aux = params[Mtgsy::POST_AUX]
100
+ data = params[Mtgsy::POST_TTL]
101
+
102
+ post_data = []
103
+ post_data << [ 'command' ,command ] if command
104
+ post_data << [ 'username' ,@username ] if @username
105
+ post_data << [ 'password' ,@password ] if @password
106
+ post_data << [ 'domainname' ,@domainname ] if @domainname
107
+ post_data << [ 'name' ,name ] if name
108
+ post_data << [ 'type' ,type ] if type
109
+ post_data << [ 'data' ,data ] if data
110
+ post_data << [ 'aux' ,aux ] if aux
111
+ post_data << [ 'ttl' ,ttl ] if ttl
112
+
113
+ p post_data if @debug
114
+
115
+ @agent.post(@api_endpoint, post_data)
116
+ end
117
+
118
+ def what_happened?(mechanize_instance, command)
119
+ mtgsy_return_code = mechanize_instance.page.body.match(/[0-9]+/)[0].to_i
120
+
121
+ case mtgsy_return_code
122
+ when 800
123
+ puts "OK"
124
+ when 400
125
+ $stderr.puts "No domain name specified"
126
+ when 100
127
+ $stderr.puts "Balance insufficcient"
128
+ when 300
129
+ $stderr.puts "Invalid login information supplied"
130
+ when 305
131
+ $stderr.puts "Domain not found"
132
+ when 310
133
+ $stderr.puts "Record not found / problem adding record"
134
+ when 200
135
+ $stderr.puts "Insufficcient information supplied"
136
+ end
58
137
  end
59
138
  end
60
139
  end
data/lib/mtgsy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mtgsy
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/mtgsy.rb CHANGED
@@ -2,4 +2,18 @@ require "mtgsy/version"
2
2
  require "mtgsy/api_client"
3
3
 
4
4
  module Mtgsy
5
+ # e.g. post_to_mtgsy([ command, name, type, data, aux, ttl ])
6
+ POST_COMMAND = 0
7
+ POST_NAME = 1
8
+ POST_TYPE = 2
9
+ POST_DATA = 3
10
+ POST_AUX = 4
11
+ POST_TTL = 5
12
+
13
+ # For the record types, e.g. ["vc32staging", "A", "50.56.9.191", "0", "123"]
14
+ RECORD_NAME = 0
15
+ RECORD_TYPE = 1
16
+ RECORD_DATA = 2
17
+ RECORD_AUX = 3
18
+ RECORD_TTL = 4
5
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mtgsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Barnett